oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Building Applications with AppleScript and FaceSpan

by Matt Neuburg, author of AppleScript: The Definitive Guide

AppleScript is primarily a scripting language; it is intended to let the user communicate with existing applications. Still, having developed a scripting solution with AppleScript, a user might naturally wish to wrap a standard application interface around it. So, how can a user take advantage of AppleScript in order to write a stand-alone application?

In my book, AppleScript: The Definitive Guide, attention is given (at the end of Chapter 2, and as the main subject of Chapter 24) to this very question. But at the time of the book's publication, FaceSpan for Mac OS X did not yet exist, so it was not considered in the repertory of tools one can use for building an application with AppleScript. Now FaceSpan 4.0 has emerged, so in this article, I'll briefly describe how FaceSpan may be used to construct the example applications in chapters 2 and 24 of my book.

What Is FaceSpan?

FaceSpan 4.0 is a small (7MB) Cocoa application in which you build a Cocoa application, using AppleScript as a programming language. You "draw" your interface in a window editor, adding interface elements to a window and specifying some of their physical behavior in an Info Panel (the palette labeled "window" in the lower right of the following figure). Then you write AppleScript code saying what should happen when the user interacts with those interface elements to generate an event (starting up the application, choosing a menu item, pressing a button, and so forth).

Related Reading

AppleScript: The Definitive Guide
By Matt Neuburg

FaceSpan uses the same underlying dictionary as AppleScript Studio for referring to and communicating with the parts of the interface (the AppleScriptKit dictionary). Indeed, working in FaceSpan is much like working in AppleScript Studio, except that you are spared having to operate in two different applications: in AppleScript Studio, you "draw" your application's interface in Interface Builder and you write and test your code in Xcode, but FaceSpan is completely self-contained. Some further differences between the two environments will be noted as we go along.

The biggest difference, of course, is that AppleScript Studio is free, whereas FaceSpan costs $200 (or $90 for a limited version that builds applications that require the presence of FaceSpan itself in order to run). The question of whether FaceSpan possesses a sufficient superiority over AppleScript Studio to justify this pricing is a matter for the free market to decide; in other words, only time will tell.

The Disk Lister Example

Let's start with the "disk lister" example on pp. 31-36 of my book. This, you recall, is intended simply to illustrate AppleScript driving an external application (the Finder) from within a stand-alone application. Our application merely displays the names of all mounted drives (or partitions) in a table view in a window. My book shows how to do this using AppleScript Studio, REALbasic, and Cocoa/Objective-C; now we'll add FaceSpan to our box of tools.

Start up FaceSpan and ask for a New Project; call it "Disk Lister," and use the default template. In a moment, the Project Window appears, as shown in the following figure:

Double-click the "main" window listing to open it so we can design our window. Show the Objects Palette if it isn't showing already, open the Tables section, and drag a table view into the main window (see the figure below).

Show the Info Panel if it isn't showing already, and double-click the table view in the "main" window so that the Info Panel is talking about the table view, not the scroll view that contains it. Set the number of columns to 1, and select the column header so that the Info Panel is now talking about the table column. Give that column a title "Your Disks" and an identifier "disks" (see figure below).

Now, adjust the size of the window and the table view as you like them. So much for designing the interface.

Now we'll write the code. In the Project Window, open the script provided for you, Project Script.applescript. You'll find that there is already a launched handler:

on launched theApplication
    open window "main"
end launched

Modify this by adding the very same code that appears on page 32 of my book:

on launched theApplication
    open window "main"
    tell application "Finder" to set L to (name of every disk)
    set ds to make new data source at end of data sources
    set tv to table view 1 of scroll view 1 of window 1
    set col to make new data column at end of data columns of ds ¬
        with properties {name:"disks"}
    repeat with aName in L
        set aRow to make new data row at end of data rows of ds
        set contents of data cell "disks" of aRow to aName
    end repeat
    set data source of tv to ds
end launched

That's all. Press Command-R to save the project and run the built application. After a moment's delay, the Disk Lister application appears and displays its window (see figure below).

A nice feature of FaceSpan is the Object Browser (see figure below) that appears in the Scripting Help drawer attached to script windows. It lists objects in the application's interface, and you can double-click a listing to insert a reference to that object in your code. For example, if we didn't know that we can access our table view as "table view 1 of scroll view 1 of window 1," the Object Browser would tell us how to do it (using names instead of index numbers).

If you were writing this application and you didn't know how to use table views and data sources, you'd read the FaceSpan manual; you would also consult the dictionary, which is displayed in a window (shown below) that looks a little different from how the Script Editor or Xcode presents it.

Pages: 1, 2, 3

Next Pagearrow