Articles Archive
Articles Search
Director Wiki
 

REALbasic 2.0.2

October 25, 1999
by Zac Belado

I read a recent Time interview with Steve Jobs where he called the Apple II a computer for software hackers. Back in the 70s, existing systems were, he conjectured, too complicated for those souls who simply wanted to write code and not build the machine. Modern operating systems are at a similar level point. The level of complexity required of the code one has to write to get the OS to simple things like draw a window or a menu is sufficiently high that it keeps some people from experimenting with modern programming. C and C++ aren't hard, but the task of learning and mastering the API calls required to build an app's GUI is. That's why products like REALbasic are such great tools.

REALbasic is a Macintosh "clone" of Microsoft's Visual Basic. It is a visual programming environment for the Macintosh that uses a highly modified form of the BASIC language and allows user to build applications quickly because REALbasic does the GUI dirty work by building menus, dialogs and windows for you. This allows you to concentrate on building the code that drives the app.

Basic?

Before you recoil in horror at the thought of BASIC, let me assure you that the BASIC you may have learned as a youngster is not the same BASIC that is used in REALbasic (or even Visual Basic). There are no line numbers and no GOSUB commands. BASIC, as it is used in REALbasic, is a very modern, flexible language. As well, the programmers at REALsoftware have added features that you would expect to only find in C++ and Java such as method overloading.

REALbasic also supports the same object model and dot notation that you may have seen in Visual Basic. Properties of objects (such as controls or classes) are accessed by attaching the property to the object reference (usualy the control or class name or variable name) with a preceeding dot.

So the width of an editField is

editField1.width

Here is a sample from an application I worked on recently:

Function processMessage(message as string) As string
  
  dim tempText as string
  dim thisLine as string
  dim i as integer
  
  for i = 1 to countFields(message, chr(13))
    thisLine = nthfield(message, chr(13), i)
    tempText = tempText + ">" + thisLine + chr(13)
  Next
  
  return tempText
  
Exception err as OutOfBoundsException
  // we're at the end of the text
  return tempText
  
End Function

As you can see, the code bears more similarity to languages like Lingo than it does older incarnations of BASIC. The language is strongly typed (all variables have to be declared and declared as a particular type), allows you the do things like throw and trap exceptions, and has full support for custom classes (including sub-classing UI controls).

Visual?

Like most visual development environments (like Director) REALbasic allows you to build an application by dragging UI elements and controls (like listboxes, buttons and text labels) onto a window. You then set the properties for the controls (size, font, position) either manually or by using a property inspector to enter the values or select them from a range of options. In a visual environment, you spend time on the code underneath the controls and very little time building the controls themselves.

What sort of tools?

REALbasic ships in two versions, Professional and Standard. The Standard version differs from the professional in only two points; the Pro version has a Windows cross-compiler and database access tools. In every other circumstance the two are identical. REALbasic ships with 32 tools including buttons, bevel buttons, checkboxes, contextual menus, disclosure triangles, popup menus, tab panels, net sockets (for things like HTTP connections), serial connection and animated sprites. The Professional versions adds one tool, a database query, to the toolbar. Tools are added to a project by dragging them to the window on which you want them to display.

REALbasic even adds small guides that display when the tool you are currently dragging is horizontally or vertically level with the nearest control, or when the tool is as close to the window border as Apple's HIG (Human Interface Guidelines) allow. This makes laying out the controls in your application very simple and goes the extra step to ensure that the app will look good, something that is lacking in many visual development systems.

REALbasic also includes the ability to add elements such as bitmap images, AppleScripts, plugins (similar to Xtras), sounds, resources and Apple external libraries to your projects.

Building an app

When you start a new project REALbasic creates a default project that has one window and one menu in the project window.


Click on the image to see an larger version

If you want, why not download the demo and play along.

If you double click on the window icon, REALbasic displays that window (which will have no controls on it) and you can then start adding controls to it. Let's make a very basic app that has three controls: a button, a text entry field and a scrolling text field.

Start by dragging an editField control onto the window. It's a bit small so we can grab the control's handles and drag it so it spans the window. Notice that while you're dragging, RB will pop in a guide to show you when the field is at the same height as the control was originally. Very handy.

Now we drag a button control and place it underneath and to the left of the editField. While the control is still selected we can change the text that it displays "add text" by editing the caption field in the Property Inspector. If the Property Inspector isn't visible, simply select it from the Window menu.

Finally, we can drag a scrolling field (called a listBox in REALbasic) onto the window. We can place it in the top left corner and then drag its control handles to make it comfortably fill the rest of the window.

Now let's add some code.

Double click the button control. This will bring up the code browser which will automatically expand to display the control you selected. The code browser displays 5 different categories:


Click on the image to see an larger version

Each control has a set of default events (you can add your own if you sub-class the control). The button control has 7 events associated with it and the code browser opens up a code editing window that has the declarations for the button's most obvious event, Action, already selected. You can't actually change the event declarations (the "Sub Action()" text) which very neatly avoids hassles caused by mistyping event names.

What we want to add to the button is a small snippet of code that will take the text from the editField, if there is any, and then add it to the text of the listbox. If we select the editField and then look in the property inspector we will see that the the control has a name property and its current (default) name is "EditField1". If we want to tell RB to refer to any of that control's properties we do so by using its name.

Let's start out by declaring a variable to store the text. We do this by using the dim statement.

dim thisVar as string

Notice that as you type "di" RB auto-suggests the word "dim" by supplying the rest of the text as grayed out letters. Simply hit TAB to complete the word. The "as string" tells REALbasic what data type the variable is. Unlike Director, REALbasic demands that you not only declare what type of data a variable will store but also that you won't try to do something like put an integer into a string variable. This is what is referred to as a strongly-typed language.

So now that we have a variable declared, let's try to put some data into it.

thisVar = editField1.text

Notice as you type that RB suggest the variable name "thisVar" and the control name "editField1" as you type. REALbasic will do this for any control, local variable or global variable declared in a project.

Now let's check to make sure it's not empty, and then we'll add it to the listBox

if thisVar <> "" then
  listBox1.addrow thisVar
end if

The listbox control has a method called addrow that simply adds a new row to the listbox. So to add text to the listbox we simply call that method and supply the variable as a parameter.

Save the file and then select Run (Cmd-R) from the Debug menu. RB will compile all the project's code and then try to run it. If there were no errors (and at this point there shouldn't be), a window with all the controls we added will appear. Add some text to the edit field, click the "Add Text" button and the text will appear in the listbox. Enter some new text, click the button again and a new line will appear.

It's certainly not Photoshop but it does illustrate the speed in which you can build an application.

Not that this simplicity means that you don't have powerful tools available for your use. REALbasic also includes some features that you don't even find in programs like Director.

OOP

RB includes some very useful OOP tools. In keeping with the design methodology of RB, these features are implemented in a fashion that makes them easy to use. The functionality isn't bogged down in "theory."

API Access

RB allows you to make direct calls to the Macintosh Toolbox using Declare statements. Those familiar with Visual Basic will recognise the format:

dim s as string
dim i as integer
#if TargetMacOS then
Declare Function SpeakString lib "SpeechLib"
(SpeakString as pstring) as Integer
#endif
s=editField1.text
#if TargetMacOS then
i=SpeakString(s)
#else
MsgBox "Speech is supported only on Macintosh!"
#endif

While they look rather arcane, they are a very useful and powerful way of accessing native methods from the Toolbox. The example above demonstrates using a Toolbox call to the Speech Manager to get it to say a string of data. If RB hasn't implemented a new feature or doesn't allow you to directly access a particular Toolbox call, then you can, with a bit of work, get direct access to it yourself. This certainly isn't something that you will need on a regular basis, but its inclusion is very fortuitous.

Cross Platform compilation

That's right. RB (in the Professional version) allows you to build Win32 applications on your Mac. It isn't foolproof, and certain Mac-only window types and features do not translate across to Windows. But you can, if you want, take your Mac code and produce Windows binaries. And to make matters even better, the apps made in REALbasic do not need the 2.5 - 5 MB of runtime files that Visual basic apps need. If you look back at the API call example above you will also see that it demonstrates another useful feature in RB -- conditional compilation. You can mark off certain features to be included in the project only if they are on the Mac or Windows. This makes your cross platform projects easier to manage.

AppleScript

One of the great features of the Mac is AppleScript. RB not only lets you embed AppleScripts in your projects (accessing them in a manner similar to function calls) but you can also add events to your application to make it scriptable. RB includes a built-in method to trap AppleEvents, allowing you to test and respond to them as needed.

Function HandleAppleEvent(Event as AppleEvent,
eventClass as String, EventID as String) as Boolean

A recent application that I worked on used 4 custom AppleScripts to send program data to various email clients. Since many applications, even the ones from Microsoft, support AppleEvents this makes it easy to add support for new applications and events without having to dramatically alter your code. When Microsoft released Outlook Express for the Mac I was able to add support for it to my application by adding a new AppleScript and modifying a few UI elements.

Database Access

REALbasic also has (again, only in the Professional version) the ability to connect to and write to databases. REALbasic has its own native format, but it will also allow you to connect to Oracle, MS SQL Server, 4D Server, Dtf, PostgreSQL, OpenBase or any ODBC-compliant database. You can either manually open the database or use a database control to access the data.

dim db as Database
.
dim rec as DatabaseRecord
rec = new DatabaseRecord
.
rec.Column("id") = "09"
rec.Column("name") = "Clark Kent"
rec.Column("jobtitle")="Reporter"
db.InsertRecord("employees",rec)

If you've created and accessed database files in Visual Basic the format of the commands will be similar to what you are already used to.

The database engine in RB supports SQL statements for direct access as well as a form of "bound" controls. Bound controls are elements such as listboxs that are linked to a field in a database table. For example, if you had a table with fields for name, address and phone number, you could bind a listBox to the name field and the database engine in RB would manually update the display in that control every time you advanced to a new record in the database.

The database engine is the most interesting and the most pernicious element in REALbasic. It is very powerful and, at the same time, rather difficult to work with. Not only because of some bugs in the code but also because the commands for database access aren't as easy to use and remember as the rest of the commands in REALbasic.

The good

REALbasic shines in its ability to quickly generate applications. The program is deep (the constructor, destructor and other OOP features for instance) but not at the cost of usability. It is expandable through AppleScript, custom plugins (RB's version of Xtras), Apple's own Libraries and API calls. And the Pro version allows you to build Windows apps. REALsoftware also doesn't charge you for manuals and CDs if you don't want them. All the docs are available as eDoc or PDF files so if you want to save some trees (and cash) by not buying the docs, you don't need to.

The bad

REALbasic isn't as fast as C code. This is slightly alleviated by the ability to make custom API calls and use plugins, but processor intensive apps will be slower in RB than in C. RB is a new environment (compared to the other available development environments) and it still has some "teething" problems in the form of small bugs. The database engine isn't as easy to use, or bug free, as it could be and the UI has some annoying bugs. The debugging tools are improved from the first release of the application, but they are still slightly less useful that the tools in an app like Director. The app also needs quite a bit of screen space...but then what development environment doesn't?

The conclusion

For simpler to intermediate tasks, RB truly shines. You can quickly build a Mac (or even Windows) app without fussing with the almost pointless arcana that you are forced to pore through in C. For more advanced applications (and even database access), RB has some rough edges that make development in it a bit more difficult. But that is a very qualified statement. RB is an exceptional tool and succeeds in that it makes programming on your Mac as much fun as working with your Mac. Despite its flaws (and what app doesn't have them) it is a great tool that users of all ability levels will find useful...and even fun.

Pricing

Standard Edition Package only $149.95 (Academic price $109.95)*
Professional Edition Package only $349.95 (Academic price $229.95)*

*Includes CD with over 80 MB of examples and Complete printed documentation (Language Reference, Developers Guide, and Tutorial)

Documentation, tutorial, and examples also available separately.

Demo versions, docs and more information is availble at REALsoftware's website
http://www.realsoftware.com/

Just as this article went to press O'Reilly books released REALbasic: the Definitive Guide.

Zac Belado is a programmer, web developer and rehabilitated ex-designer based in Vancouver, British Columbia. He currently works as an Application Developer for a Vancouver software company. His primary focus is web applications built using ColdFusion. He has been involved in multimedia and web-based development, producing work for clients such as Levi Straus, Motorola and Adobe Systems. As well, he has written for the Macromedia Users Journal and been a featured speaker at the Macromedia Users Convention.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.