[ Team LiB ] |
2.1 Script EditorBy script editor I don't mean just Apple's Script Editor application—though that is in fact an example of a script editor. I mean a dedicated application intended as a general environment for the user to create, edit, compile, develop, and run AppleScript code. Typically such a program will have the following features:
(Terms in that list that may be strange to you are explained in Chapter 4. That's where I'll formally introduce dictionaries, explain what it means for an application to be recordable, describe the standard AppleScript file formats, and talk about the OSA, or Open Scripting Architecture.) Apple Computer provides a script-editing application of this kind, called (logically enough) Script Editor. It's free, and is part of the default installation of Mac OS X. It is located in /Applications/AppleScript. Figure 2-1 shows a very short script being edited in Script Editor. The script has been compiled using the Compile button, which appears at the center of the toolbar at the top of the window; thus the script is "pretty-printed" with syntax coloring. The script has also been run, using the Run button in the toolbar; the result is shown in the lower half of the window. The script asks the Finder for the names of all mounted volumes. Technically, the response is a list of strings; the curly braces indicate a list, and the double quotes indicate strings (Chapter 13). Figure 2-1. Script EditorThe lower pane of the window consists of three tabs; the second tab, the Result tab, is showing in Figure 2-1. Another tab, Event Log, keeps a record of all outgoing commands and incoming replies—that is, of all lines of AppleScript that equate to Apple events sent to other applications, and the replies returned by those applications. The Event Log is operative only if the Event Log tab is selected when a script is run; but another window (not shown), the Event Log History window, can be set to operate even when not open. The first tab, Description, is a place where the user can enter a comment to be stored with the script. At the top of the window is a popup menu where the user can choose the installed OSA language to be used for compiling and running the script, and another popup menu for navigating among handlers (subroutines) within the script. Also shown is the Result History window, which logs the result of every execution of every script. Both the Event Log History window and the Result History window are particularly useful while developing and testing a script. The Script Editor offers some helpful shortcuts for entering commonly used text such as control structures (Chapter 12) and built-in commands. One is the contextual menu that appears when you Control-click in the window; it gives access to various utility scripts. The Script Editor is itself scriptable, and these utility scripts drive it to modify the text appearing in the script window. You can modify these scripts, and can add utility scripts of your own; they live in /Library/Scripts/Script Editor Scripts. Another text-entry shortcut is the Script Assistant; when this feature is turned on, the Script Editor performs autocompletion as you type, and you can accept or choose among its offerings with the Edit Complete menu item (default shortcut F5). Figure 2-2 shows an application's dictionary as displayed by the Script Editor—in this case, the dictionary of the Finder, the application targeted by the example script in Figure 2-1. The window behaves as a kind of browser. The classes and events (the nouns and verbs constituting the application's vocabulary) are clumped into groups on the left; when a particular class or event is selected, the information for it is displayed on the right. Here, the information for the disk class is displayed. (Dictionaries, and how to interpret the information about them displayed in a window such as this, are the subject of Chapter 19.) Figure 2-2. A dictionary in Script EditorAnother free script editing program is Smile. It provides an excellent working environment, including splendid text-editing and navigation facilities, full scriptability, and some remarkable features to help you in developing scripts, including:
(Four-letter codes, and AppleScript's dictionary, are discussed in Chapter 19.) A commercial alternative is Late Night Software's Script Debugger. Its primary advantage, as the name suggests, is that it provides a full-fledged AppleScript debugger, making it a genuine development environment. It is completely scriptable, provides many shortcuts for entering and navigating code, and includes:
Figure 2-3 shows a script that has been edited, compiled, and run in Script Debugger (Version 3.0.6). At the top of the first window is a pane displaying the properties associated with this script. (Properties are a kind of global variable, discussed in Chapter 7 and Chapter 9.) The second window displays the result of the script, in a hierarchical format. The first line states explicitly the type of the result—it's a list. The values of the list's items are displayed on subsequent lines, with icons indicating their types. Figure 2-3. Script DebuggerFigure 2-4 shows the Finder's dictionary as shown in Script Debugger, in one of three available views. In this particular view, the hierarchy of actual current Finder objects is presented. Thus one can see directly that among the Finder's top-level elements are its disk objects. The listing for one disk object has been opened to show its attributes (Chapter 10); among these one can see its various elements and their counts, such as the fact that it has three folder objects, and its various properties, such as its name property, whose value is shown. In contrast to Figure 2-2, which is an abstract display of facts about Finder disk objects in general, Figure 2-4 is a concrete display of the Finder's actual disk objects at this moment; this a very powerful and informative way to explore the repertory of things one can say to a scriptable application. Figure 2-4. A dictionary in Script DebuggerYou don't have to feel confined to one particular script editor; compiled scripts are a standard format, so any script editor can read the files of any other. (Though as of this writing this is a bit less true than it used to be, because Apple has just introduced two new script formats, the script bundle and the application bundle, and the other editors have not yet caught up.) So if you find yourself wishing to switch among script editors, it's more or less effortless to do so. Some script editor program will probably be the main way you'll work with AppleScript. |
[ Team LiB ] |