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

Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »

Price Scanner via MacPrices.net

Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
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
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more
New promo at Visible: Buy a new iPhone, get $...
Switch to Visible, and buy a new iPhone, and Visible will take $10 off their monthly Visible+ service for 24 months. Visible+ is normally $45 per month. With this promotion, the cost of Visible+ is... Read more
B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for $100 off Apple’s new MSRP, only $899. Free 1-2 day delivery is available to most US addresses. Their... Read more
Take advantage of Apple’s steep discounts on...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply 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
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
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
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.