TweetFollow Us on Twitter

REALbasic Development

Volume Number: 16 (2000)
Issue Number: 4
Column Tag: Programming

Rapid Development with REALbasic

by Erick J. Tejkowski

Extend REALbasic to suit your needs

Introduction

Long the "Holy Grail" of computing, video is finally coming of age in the computer world, but Mac users have known this for some time. QuickTime has been around for awhile now and so has sophisticated video hardware for the Mac. With the explosive growth of the web in recent years, hundreds have wired their web servers with a variety of video camera devices, broadcasting to the worldviews of local scenery, photos from home, or pictures of the family pet.

Luckily, Apple has been involved in one way or another with desktop video from the start. With pioneering technologies in software and hardware, multimedia has usually appeared first, not to mention the best, on Macintosh desktops.

This article will continue in that tradition and describe how to make a web-based camera that will periodically post photos to a web site for the world to see. The reader will also discover how to incorporate these abilities into their own designs, including stop-motion photography, home surveillance, and other fun experiments.

Requirements

Since this project is a bit more hardware related than usual, a brief discussion about the programs requirements might be in order. First, there is a question of video hardware. For a camera, any typical video composite signal is fine. Video hardware that produces composite video usually has S-Video and/or RCA jacks like those on your home stereo. Since most people have a video camcorder these days, we will focus on them. Web cams also make a fine choice, as do digital cameras (if they have composite video output). If you have no camera, but would still like to experiment, a VCR will do as a video source. Unfortunately, QuickCams© will not work for this project, nor will USB devices. These have more sophisticated software requirements, and as such, do not fit into this beginner's style article.

In your computer, you will need a video-input card. Any AV Mac will do, as will as any third party video cards available on the market. Again, it will need a composite video input to interface with the camera described above. To set up your hardware, simply connect the composite video output of the camera to the composite video input of the video card.

To create the software, a few decisions had to be made. To better illustrate the beauty of REALbasic's simplicity, it was decided that the reader should be able to create a working application with only a modicum of effort and expense. Thus, to expedite the capture of video, we will use Apple's own video technology. The Apple Video Player, available with most versions of the MacOS including compatible 68K versions, will bear the responsibility of capturing the video. True capture capabilities can be added to REALbasic, but they are not free and are a bit more complex to program. They will be discussed later, however, for those programmers wishing to expand their video horizons. To interface with the Apple Video player, the REALbasic application will use AppleScript. Since AppleScript is installed with the operating system, this requirement should also not be much of a problem for most users.

The final consideration before embarking on this project was the subject of FTP. Once the photos are captured, we will want them to be transferred to a web site for viewing by a web browser. FTP is not really a beginner's topic. Although several classes already exist that will help you in your FTP endeavors, this project will remove the added complexity of adding FTP to your application. Furthermore, the solution will preclude the need to purchase space on a web server. Like it or not, many beginners do not have FTP accounts at their disposal. How will all of these objectives be met? (.... Enter drum roll....) iDisk! In case you haven't heard, iDisk is part of the greater iTools services offered by Apple on their web site. Registration affords you an email account, 20MB of disk space on the server, personalized greeting cards, and a host of other features. The best part, however, may be that it's completely free! For those of you who have not made the jump to MacOS 9 (a requirement for registration), check the reference section at the end of this article for suggestions about how to use iTools with older operating systems.

For this project, we will utilize iDisk. iDisk is a 20MB partition on Apple's web servers for you to store files. On the disk, you can store text, video, pictures, and more just as you normally do with web servers. iDisk is a bit different though in that you can also mount your iDisk directly on the desktop! The mounted iDisk acts just like a hard drive in the Finder. You can copy, delete, and "Get Info" to your heart's content. Now, the issue of FTP merely becomes a matter of saving a file to a disk, something that can be accomplished in REALbasic with only one line of code. The operating system takes care of the rest for us. To reiterate, Listing 1 details what you will need for the project. One final note to make is that this project can even be completed using a demo version of REALbasic, though your applications will expire after five minutes of use.

Listing 1. Project Requirements


	A video camera.
	A Mac with a video input card.
	REALbasic version 1 or 2
	AppleScript
	Apple Video Player
	Apple iTools/iDisk Account

Building the Project

The code for our video capture will be built in two parts. To begin, we will create an AppleScript that will tell the Apple Video Player to take a photo. Next, we will build a REALbasic project that uses the script as well as adds functionality of its own. The REALbasic project will be responsible for controlling how often the video capture takes place, converting the PICT file to a JPEG, and finally creating the web page and saving it to the iDisk.

AppleScript

To start, launch the AppleScript Script Editor and enter the code in Listing 2. The script is fairly self-explanatory with two possible exceptions. First, the entire script is enclosed within an on run{x} . . . end run handler. The handler is necessary for the script to work with REALbasic. To test the script in the Script Editor it is necessary to remove or comment out the on run handler. The {x} variable is used to indicate the path for the destination file. By default, the Apple Video Player creates PICT files only. This is not really a problem, because REALbasic can help us. To make the photo compatible with the web and to slim down the file size, we will convert the PICT file into a JPEG from within the REALbasic application.

The second topic that might require some explanation is the replacing yes statement. This means that when the file is saved it will replace a file with the same name. For the purposes of this project, this will suffice. A new photo will be captured periodically and replace the previous photo. If you would like to take a series of photos then the code would read replacing yes.

Listing 2. The Apple Video Player AppleScript.


on run (x)
	tell application "Apple Video Player"
		save video in x replacing yes
	end tell
end run

It should be noted that the Apple Video Player has a relatively extensive list of scriptable features. For example, instead of capturing a photo periodically, the script could easily be set to capture QuickTime movies. Be sure to take a look at the Apple Video Player dictionary by selecting the File...Open Dictionary and menu in the Script Editor to custom tailor the script to your needs. Before closing the Script Editor, be sure to save the file as a Compiled Script, naming it something functional like CapturePhoto.

REALbasic

The final step in the video surveillance system is to build a REALbasic project. Begin by starting the REALbasic application. A new project is created automatically. Go to the Finder and drag the AppleScript created earlier into the REALbasic Project window. Next, open Window1 and create two variables by selecting the Edit..New Property menu. Name the variables as shown in Listing 3. Create a new method called CreateWebPage and enter the code also shown in Listing 3.

Listing 3. Window1 Properties and Methods.


	Properties
	timersetting as integer
	thepict as picture
	
	Methods
	Window1.CreateWebPage:
	Sub CreateWebPage()
	Dim f As FolderItem
	Dim TStream As TextOutputStream
	Dim d as date
	Dim myURL as string
	
	d=new date
	//create the web page here
	//with the current time on it
	//replace "myidisk" below with your own iDisk name!!!
	f=GetFolderItem("myidisk:Sites:mysnapshot.html")
	
	TStream=f.CreateTextFile
	TStream.WriteLine "<html><head>"
	TStream.WriteLine "<title>My Surveillance Camera
	  </title>"TStream.WriteLine "</head>"
	TStream.WriteLine "<body>"
	TStream.WriteLine "Current Time:"+d.abbreviateddate+" "+
	  d.longtime+"<br>"//this code belongs to the above line
	
	//replace "myidisk" below with your own iDisk name!!!
	myURL="http://"
	myURL=myURL+"homepage.mac.com"
	myURL=myURL+"/myidisk/.Pictures/MySnapshot.JPG"
	
	TStream.WriteLine "<img src="+chr(34)+myurl+chr(34)+">"
	TStream.WriteLine "</body></html>"
	TStream.Close
	
	End Sub

Creating the Interface

The next step in creating the project is to create the interface. From the Project Window open Window1. From the toolbar, drag three PushButtons onto Window1. Name them ManualButton, StartButton, and StopButton respectively. Next, drag a PopupMenu onto the window. With the PopupMenu selected, change the InitialValue property in the Properties Window to match the values in Figure 1.


Figure 1.PopupMenu1.InitalValue Property Editor

Finally, drag a Canvas and a Timer control onto Window1. Leave the default names for these controls as they are. Set the Mode property of Timer1 to 0 (i.e. zero=off) and change the dimensions of Canvas1 to 320 (Width) and 240 (Height). By now, your window might look something like Figure 2.


Figure 2.Window1 layout

The Code

The final step in creating the REALbasic project is to add some code to each of the controls. The ManualButton will allow the user to take a photo, create a web page, and post them both online. The StartButton initiates the same thing, but does it at regular intervals. The frequency of these intervals is decided by the PopuMenu1 selection. Once the image has been captured using the Apple Video Player it will be displayed in the Canvas1 control. Finally, Timer1 is the control that performs the periodic functions initiated by the StartButton. As before, it is essential that you alter the pathnames to fit your iDisk account. Listing 4 shows the remaining source code for the project.

Listing 4. Interface Source Code


	Window1.Canvas1.Paint:
	Sub Paint(g As Graphics)
	//first fill the canvas with black
	g.foreColor=rgb(0,0,0)
	//then draw the picture in the canvas
	g.drawpicture thepict,0,0
	End Sub

	Window1.ManualButton.Action:
	Sub Action()
	Dim f,ff as folderItem
	Dim i as string
	Dim d as date
	f=GetFolderItem("MySnapshot.PICT")
	ff=GetFolderItem("myidisk:Pictures:MySnapshot.JPG")
	i=CapturePhoto(f.absolutepath)
	thepict=f.openaspicture
	canvas1.refresh
	ff.SaveAsJPEG thepict
	CreateWebPage
	End Sub

	Window1.PopupMenu1.Open:
	Sub Open()
	me.listindex=0
	End Sub
	Window1.StartButton.Action:
	Sub Action()
	//disable this StartButton
	//and enable the StopButton
	me.enabled=false
	StopButton.enabled=true
	//Assign the photo timer's period here
	Select Case PopupMenu1.ListIndex
	Case 0
	// timer period is in millisecs.
	//thus 1 min=60 sec.*1000
	timer1.period=60000
	Case 1
	timer1.period=60000*5
	Case 2
	timer1.period=60000*10
	Case 3
	timer1.period=60000*30
	Case 4
	timer1.period=60000*60
	End
	//Start the photo timer
	timer1.mode=2
	End Sub

	Window1.StopButton.Action:
	Sub Action()
	//disable this button
	//and enable on the other
	me.enabled=false
	StartButton.enabled=true
	//stop the timer here
	timer1.mode=0
	End Sub

	Window1.Timer1.Action:
	Sub Action()
	Dim f,ff as folderItem
	Dim i as string

	f=GetFolderItem("MySnapshot.PICT")
	ff=GetFolderItem("myidisk:Pictures:MySnapshot.JPG")
	//capture the photo with our AppleScript
	i=CapturePhoto(f.absolutepath)
	//now open the PICT that the AppleScript created
	thepict=f.openaspicture
	canvas1.refresh
	ff.SaveAsJPEG thepict
	CreateWebPage
	End Sub

Once the interface has been built and the appropriate code entered, select the File..Build Application menu and create a Macintosh version of the project called "MyVideoCapture.

Using the Software

To begin using the software, launch the "MyVideoCapture" application. Next launch Apple Video Player and turn on the camera. Adjust the software and hardware settings so that an image appears in the window. Finally, mount the iDisk disk on the desktop. This step is normally accomplished through a web interface, but it is helpful to create an alias to the iDisk once it has been mounted. Later, when you wish to mount the disk, you can simply double-click the alias. To test the application, click on the ManualButton. If everything goes smoothly, a picture will be captured, it will appear in the "MyVideoCapture" window. The picture will then be converted to a JPEG image and a web page will be created. The resulting image and html file will then be saved to the iDisk. Voila' ! Web-based video surveillance in less than an hour! If something did not work properly, do not panic. Go back and recheck your code. Are all of the file paths correct? Was all code entered correctly? Were all Properties and Methods added to the project? If all else fails, the source code can always be downloaded from MacTech's web site. The completed application is shown in action in Figure 3.


Figure 3.The completed application.

More Ideas

Hopefully this project will spawn some of your own ideas. For example, instead of photos the project could capture QuickTime movies for posting to the web. Be sure to take into account the length of transfer time involved with saving to an iDisk account. The current project worked fine over a 33.6 modem, but would likely fail if QuickTime movies were involved. It would simply take longer than one minute to transfer a QuickTime movie. In this instance, perhaps a video capture time of fifteen minutes would work better.

Another thing to consider is that iDisks mounted to the desktop will automatically unmount after a period of inactivity in the neighborhood of five minutes. If the application is set to take photos every thirty minutes, the disk might be unmounted by the time the transfer is to take place. One possible work around is to incorporate another timer into the project. The timer would be set to repeat infinitely at a rate of about four minutes. Every four minutes, the timer could create or destroy a the same text file repeatedly on the iDisk. This would ensure that the iDisk stays mounted.

Besides posting photos to the web, the application could easily be altered for fun stop-motion movies. Point the camera out the window, set the timer, and start recording. At the end of the day import the folder full of photos and create a QuickTime movie. This will no doubt amaze and impress your friends!

Conclusion

This article attempted to show how well REALbasic functions as a rapid development environment. Using existing Apple technologies together with a home camcorder and a copy of REALbasic, we were able to construct a functional web camera with features matching some commercial products. Another interesting point to mention is how nicely REALbasic compliments AppleScript. AppleScript could have been used exclusively to do much of this project, but REALbasic makes it very easy to construct an interface and control the AppleScript with much less code.

References

  • Essence Software, VideoPlug
    http://www.essencesw.com/
    VideoPlug is a REALbasic plugin that allows you to add video capture elements to REALbasic applications. This can be used to replace the Apple Video Player functionality in the project.
  • REALbasic
    http://www.realbasic.com
    http://www.realsoftware.com
  • Apple
    http://www.apple.com
    This is where you register for an iTools account.
  • MacFixit
    http://www.macfixit.com
    This site offers numerous suggestions for using iDisk with older operating systems as well as how to automatically mount the iDisk. Do a search for the keyword "iDisk".
  • AppleShare Client
    http://www.apple.com/appleshareip/text/downloads.html
    Download and install this client to mount the iDisk through the Chooser. Be certain to explicitly follow the instructions for installation. Different versions of the Mac OS require different installations.

Erick J. Tejkowski is a web developer with The Zipatoni Company in St. Louis, Missouri. He can be reached by email at ejt@norcom2000.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »

Price Scanner via MacPrices.net

New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.