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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom 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.