Mac OS X Unleashed

Mac OS X Unleashed

By John Ray and William C. Ray

Script Editor

The easiest way to get started with AppleScript is with the AppleScript editor. Besides being a context-sensitive programming editor, it also acts as a script recorder. A user can open the Script Editor, click record, and generate an AppleScript by using the editor to monitor his actions while interacting with an AppleScriptable application.

Apple has made remarkable strides in making Mac OS X 10.1 scriptable. Applications such as the Finder and Terminal are now fully scriptable. The Script Editor will serve as your primary entry and testing point for any AppleScript development—either recorded or entered by hand.

Basic Usage

Launch the Script Editor (Applications/AppleScript/Script Editor) to begin scripting. The basic editor window is shown in Figure 21.1.

21fig01.jpg

Figure 21.1 The Script Editor is used when editing or recording AppleScripts.

The Script Editor is composed of script recording and editing controls, which include:

Start using the editor by clicking the Record button and moving a few Finder windows around. As you move and open windows, the script will build in the editor window. Click Stop to finish off the code block and prepare it for execution. Figure 21.2 displays a script that has just finished generating.

21fig02.jpg

Figure 21.2 Click Record to monitor your actions and build an AppleScript, and then click Stop to finish the script.

Scripting Dictionary

Obviously, the biggest draw to AppleScript is the capability to create scripts from scratch. Recording is a good way to get a quick start, but can't be used to generate anything truly useful. The basic AppleScript syntax will be covered later in this chapter. Even this, however, is useless without knowledge of what commands an application can accept. Luckily, you can view a scripting dictionary that shows the functions and properties offered by a given piece of software.

To access a scripting dictionary for any application, choose Open Dictionary from the File menu. A list of the available scriptable applications is displayed, as demonstrated by Figure 21.3.

21fig03.jpg

Figure 21.3 Choose from the available scriptable applications.

Be aware that some applications might not be shown—the Browse button at the bottom of the window opens a standard File Open dialog box for choosing an arbitrary file. After picking an application, from the default or browse view, a dictionary window should appear, as seen in Figure 21.4.

21fig04.jpg

Figure 21.4 The dictionary documents the available AppleScript functions.

Along the left side of the dictionary window is a list of the AppleScript functions that are provided. These functions are divided into categories, depending on their purpose. These categories are called suites. To display the syntax for a given item, click its name in the list. Highlighting a suite name displays a description of the functions within that grouping, and a complete view of the syntax for each.

AppleScript abstracts the parts of an application into objects within classes. These objects have properties that can be set or modified to effect changes to the object. The properties can also be retrieved with get to return results for evaluation. For example, the Finder has a file object with a file type property. The following script gets and displays the type for an arbitrary file:

1: tell application "Finder"
2:     set thisFile to (choose file with prompt "Pick the file to examine:")
3:     set theType to get the file type of thisFile
4:     display dialog theType
5: end tell

Line 1 sends instructions to the Finder. Line 2 sets a variable called thisFile to point to a file. The choose command opens a file selection dialog box for visually selecting a file. Line 3 sets a variable called theType to the results of a command that gets the file type of the file reference by thisFile. Line 4 displays a dialog box containing the contents of theType. Finally, line 5 stops talking to the Finder.

This example introduces the structure you will see in most AppleScript programs. The tell, set, and get statements form the basis of scripts. The objects and the parameters that can be modified, however, will have to be looked up in the application's dictionary.

Results

When an AppleScript function returns a result, it is stored in a special temporary variable called result. This can be used to access a value without the need for additional variables. For example, lines 3 and 4 of the preceding script could be changed to


   get the file type of thisFile
display dialog the result

To display the contents of the result container within the Script Editor, choose Show Result (Command+L) from the Controls menu. Mac OS X will open a small window that contains the current value of result.

Syntax Highlighting

The Script Editor automatically highlights and formats AppleScript as you type. To change the default font styles and sizes, choose AppleScript Formatting from the Edit menu. The format dialog box can be seen in Figure 21.5.

21fig05.jpg

Figure 21.5 AppleScript Formatting controls enable the user to adjust the appearance of the Script Editor.

Choose one of the lines in the formatting dialog box, and use the Font and Style menus to pick a new appearance for the item. To reset to the built-in defaults, click Use Defaults.

Script Tracing

To trace the execution of a script as it runs, use the Event Log (Command+E). This log keeps track of the events (commands) sent to an application and, if the Show Event Results button is checked, displays the results that are returned, immediately. Figure 21.6 shows the Event Log after replaying a simple script to get the location of a Finder window.

21fig06.jpg

Figure 21.6 The Event Log can be used to monitor script execution.

Saving

After creating a script that functions the way you want, you can save it for double-click execution whenever you'd like. Choose Save As or Save As Run-Only from the File menu. The Run-Only option should be used to protect the script from future edits. Figure 21.7 displays the Save As dialog box.

21fig07.jpg

Figure 21.7 Save a script for later execution.

There are three possible file formats for scripts:

When saving the script as an Application, there are two additional options in the lower-left corner of the dialog box. Choose Stay Open to have a script remain open after running, or Never Show Startup Screen to remove the AppleScript startup screen when the program runs.

Share ThisShare This

Informit Network