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

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.