Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [15]

By Root 1638 0

Figure 2-4 shows Script Debugger's unique Explorer view, displaying the hierarchy of actual current Finder objects. The Finder's disk objects are listed among its top-level elements, and the listing for the first disk object has been opened further, drilling down the hierarchy two additional levels. The code needed to refer to the currenly selected object is displayed at the bottom of the window. At the right, a drawer charts the containment hierarchy in graphical form. This concrete display of a scriptable application's actual objects at a given moment is a very instructive and helpful means to understanding the repertory of things one can say to it.

Internally Scriptable Application


Some applications implement automation not through AppleScript but by means of some other language (possibly one unique to that application) that effectively operates entirely within that application. Such an application is internally scriptable . But even though such an application does not use AppleScript for its internal scripting, the developers might still wish it to be able to communicate with other applications. That means Apple events, and AppleScript is a convenient way (convenient both for the developers and for the end user) to construct and send Apple events. The internal scripting language can most likely operate on text, so a typical approach is to give

Figure 2-3. Script Debugger in action

Figure 2-4. Explorer view in Script Debugger

it the ability to treat text as AppleScript code (by compiling and running it). Even though you can use AppleScript code in an internally scriptable application, you wouldn't want to develop it there, as there is no provision for editing and testing your code, displaying a target application's dictionary, and so forth. Thus you'll usually develop your code in a script editor application and then copy it into the internally scriptable application.

A good example is the database application FileMaker Pro . It has an internal scripting language that can execute text as AppleScript. This text can be static or constructed dynamically ("calculated"). Figure 2-5 shows a case in point, with FileMaker Pro being used to communicate via AppleScript with Apple's Mail program. The idea is that you might like to store your contacts in a true database program, but then you might like to create and send an email message to one of them in a true email client (see "Combining Specialties" in Chapter 1). Thus, the FileMaker window (in front) displays a database of contacts; pressing the "To" button causes Mail to create a new email message (in back) using the email address from the current FileMaker record.

Figure 2-5. FileMaker talking to Mail

The FileMaker script triggered by pressing the "To" button consists of a single step, telling FileMaker to treat a piece of calculated text as AppleScript (entered by way of FileMaker's usual annoying cascade of modal dialogs, as shown in Figure 2-6):

"set theAddress to \"" & fm address book::email & "\"

tell application \"Mail\"

tell (make new outgoing message)

set visible to true

make new to recipient at end of to recipients ¦

with properties {address:theAddress}

end tell

end tell"

Figure 2-6. Specifying a calculated AppleScript step in a FileMaker script

The code is particularly ugly (and difficult to write), because it isn't AppleScript: it's the instructions, in FileMaker's internal calculation language, for constructing a string that will be treated as AppleScript. This string consists of three pieces—two literal strings, surrounded by quotation marks, and a reference to the current record's email field. The three strings are joined by FileMaker's concatenation operator (&, just as in AppleScript). But the constructed string must itself include quotation marks around the contents of the email field and around the name of the Mail application; to indicate these within a string literal, they have to be "escaped" by preceding them with a backward slash (\). This accounts for the rather bizarre appearance of the first and second lines

Return Main Page Previous Page Next Page

®Online Book Reader