Building Mac Applications Using REALbasic 4.5 for Mac OS XI have been quietly watching the development of the Macintosh platform for the past several years. Many years ago I worked as a Mac programmer doing C Programming using Code Warrior from Metrowerks. Back then the Mac OS 8.5 was not a very stable platform and doing development on Mac wasn't really a very pleasant task. You know, all the pointers stuff can really get you into trouble. It leads to this joke that MACINTOSH really actually stands for "Most Application Crash If Not The Operating System Hangs," and that the street name "Infinite Loop" in Apple's HQ at Cupertino, CA really means just that--that your application will always run into infinite loops.
How things have changed. With the year-old Mac OS X Jaguar in place, the venerable Mac OS has definitely come a long way. As my current work revolves mainly around the Windows platform, I have not really gotten the chance to do development work on the Mac since my last job as a Mac programmer. Recently, I was so inspired by my editor Brian Jepson at a conference in Singapore, that I went to buy an eMac. I wanted to see for myself how the development environment has changed in the Mac world and how an existing .NET developer can migrate (well, to explore other potential markets I must say) to the Mac platform. With this newfound excitement, I went in search of the languages and tools that most Mac developers use. This article will detail my journey into the Mac programming world from a .NET developer's perspective. Hopefully, I can serve as your tour guide for those of you making the foray into Mac development.
I mostly use VB.NET in my work on the .NET platform, and so when I went searching for a VB equivalent in the Mac world, I was delighted to find REALbasic from REAL Software. As the name implies, REALbasic is a development environment on the Mac platform that uses the modern version of the Basic programming language.
|
|
Building Mac applications using REALbasic is very similar to using Visual Studio .NET to build Windows application. You build your user interface by dragging and dropping controls to a window and then write codes to stitch the controls together. A bonus point of REALbasic is that you can target your application to run on both the Mac and the Windows platform.
REALbasic runs on Mac OS X, in addition to System 8.1 and above.
Before you take the plunge and spend your hard earned money, take some time to download the 30-day trial version of REALbasic. If you have downloaded the REALbasic 4.5.1 trial edition, you are now ready to go.
In this article, I am going to build a very simple text editor that illustrates several concepts in REALbasic programming. Using this text editor you can change the color and font of text as well as save and load text documents. Coincidentally, the documentation that comes with REALbasic also contains a similar example. If you want to have a quick overview of REALbasic, read this article. When you have more time to delve into the details, I strongly recommend that you read the excellent tutorial provided by REALbasic.
|
|
Launch REALbasic and you will see four windows:
Let's examine each window in more detail. The Project Window contains all the parts that comprise your application. By default, the Project Window will contain a default window and a menu.
|
|
The Controls Palette is similar to that of the Toolbox in Visual Studio .NET. In it, you can find the many controls that make up the user interface of a typical Macintosh application.
|
|
The Windows Editor shows the window that make up your application. You can drag and drop controls onto your window and add as many windows to your project as required.
|
|
The Properties Window shows the various properties associated with the selected control. You can then modify the value of each property using the Properties Window.
|
|
|
|
|
Take some time to explore the various properties associated with each control. If you are familiar with Visual Studio .NET, then you will feel really at home. For example, to make your window re-sizable, you check the GrowIcon property of the window. To anchor the controls to a window when it is re-sized, you have the options to anchor it against the left, right, top or bottom by checking the LockLeft, LockRight, LockTop or LockBottom properties respectively.
When you are done adding the controls to your window, it is time to do some coding. As you may have guessed it right, double-clicking on the controls brings up the Code Editor:
|
|
On the left of the Code Editor you find the Browser containing the various grouping of items such as Controls, Events, etc. A Column Divider separates the Browser and the code area. For our sample application, the first set of codes that we need to write is to show the contextual menu when the user right-clicks on a selected piece of text:
|
|
So, in the Code Editor, locate the EditField control and type in the following code under the MouseDown event:
|
|
The IsCMMClick() function returns a true if the user clicks on a control while pressing the Control key. Essentially it is used to determine if a Contextual Menu should be displayed. If it does, display Contextual Menu 1:
|
|
The above codes add in the Font and Color menu items to the ContextualMenu control. When the user clicks on one of the items in the contextual menu, it triggers the Action event:
|
|
If the user selects the Font menu item, the second ContextualMenu will be displayed. If the user clicks on the Color item, the SelectColor() function will display the standard Color picker:
|
|
Once the color is selected, it is applied to the EditField control.
|
|
When ContextualMenu 2 is loaded, it will first display all the fonts available on the system using the Font() function. When a particular font is selected, it is then applied to the EditField control (through the Action event):
|
|
The next control that we need to program is the Slider control. When the user clicks on the Slider control, we want the size of the selected text to be changed, and so we insert the following codes in the ValueChanged event of the Slider control:
|
|
We are now left with two more controls to code: the Save and Load button. Instead of coding them directly, I want to create two methods so that these two functionalities can be re-used. To do so, go to the Edit menu and select New Method, and type in the name of the method you are creating. You can also specify any input parameters and the output of your method. For my case, I don't need any of these.
|
|
Insert the codes for the LoadFile method:
|
|
Insert the codes for the SaveFile method:
|
|
Note that in the LoadFile() method, you used the GetOpenFolderItem function to load a file of type "application/text" from the disk. In order for the application to load the correct file type, you need to add in the file type by clicking on the Edit menu and selecting File Types:
|
|
Now that we have defined our two methods, we can simply link the Load and Save buttons to the two methods by calling the method names:
|
|
That's it. You can now run the application by pressing Command-R.
|
When you are moving to a new development platform, you always have this problem on knowing which function to use and what the parameters are for each function. Fortunately, REALbasic has done a very good job in helping programmers get started quickly.
For example, after I have typed the word "selectcolor," REALbasic immediately knows that this is a defined function and displays an information window telling me the expected parameter as well as the return type:
|
|
Similarly, whenever I start typing, REALbasic will anticipate the word that I intend to type. It will append an ellipse "..." after the word to signify that it has identified a list of possible words that I am going to type. For example, after I have typed the word "edit," I can press the Tab button to see a list of words that I can choose from. This is very much like the Intellisense in Visual Studio .NET. I find this feature very helpful, and it has no doubt eased my entry to Mac programming.
|
|
Another very helpful feature is its online Language Reference. Simply type a keyword, and you can view the explanation as well as sample codes. Best of all, you can drag and drop the sample codes (the dotted rectangle box) directly onto your application.
|
|
With the application built, it is now time to deploy it. REALbasic allows you to deploy your applications to be deployed as a:
To deploy the application, click on the File menu and select Build Settings.
|
|
You can select the various platforms that you want to deploy to and the detailed settings of each platform:
|
|
To build the application, select Build Application. If the build process is successful, you will see two icons on your desktop:
|
|
You can now run the application by double-clicking on the icon. Of interest is the EXE file, which is meant to run on Windows. I tried running the application on Windows 2000 Professional:
|
|
It works similarly to the way it works on the Mac platform. However, the user interface seems a little bit unpolished. I think I really cannot complain, though, since the application works as promised on Windows. For large projects, this feature is definitely useful, but I have yet to work on a moderately large-scale project to see if the multi-platform support is usable.
If you are an experienced Visual Studio (or Visual Studio .NET) programmer, you will definitely feel comfortable with REALbasic. The syntax of the language is very similar to Visual Basic, and the online help reference is a real lifesaver. REALbasic has also recently announced that it will release a version of REALbasic for Windows, which means that you are now use REALbasic on Windows and target Mac users at the same time. For more information, check out REALbasic announcement
Wei-Meng Lee (weimenglee.blogspot.com) is a technologist and founder of Developer Learning Solutions, a technology company specializing in hands-on training of the latest Microsoft technologies.
Read more Developing for Mac OS X columns.
Return to the Mac DevCenter.
Copyright © 2007 O'Reilly Media, Inc.