Analyzing a Script


Having looked through the script that you wrote in the previous example, you may be surprised to learn that AppleScript doesn’t know anything about the Finder’s operations. Although your recorded script contains commands that set the position, size, and view of a Finder window and selects an item in it, AppleScript doesn’t know anything about these or other Finder operations. In fact, AppleScript knows how to perform only the six following commands:

  • Copy

  • Count

  • Error

  • Get

  • Run

  • Set

AppleScript learns about moving and resizing Finder windows from the Finder. More generally, AppleScript learns about commands in a script from the application that the script controls. The application has a dictionary of AppleScript commands that work with the application. The dictionary defines the syntax of each command. AppleScript learns about more commands from scripting addition files on your computer. Each scripting addition file contains a dictionary of supplemental AppleScript commands.

Learning application commands and objects

Look at the sample script you created. The first statement says,

tell application "Finder"

To AppleScript, this statement means “start working with the application named Finder.” When AppleScript sees a tell application statement, it looks at the dictionary for the specified application and figures out what commands the application understands. For example, by looking at the Finder’s dictionary, AppleScript learns that the Finder understands the select command. The dictionary also tells AppleScript what objects the application knows how to work with, such as files and windows. In addition, an application’s dictionary tells AppleScript how to compile the words and phrases that you write in scripts into Apple event codes that the application understands.

After learning from the tell application statement which application it will send event messages to, AppleScript compiles the remaining statements to determine what Apple event messages to send. One by one, AppleScript translates every statement it encounters in your script into an Apple event message based on the application’s dictionary. When the script runs, the Apple event messages will be sent to the application named in the tell statement. The application will receive the messages and take the appropriate action in response to the Apple events.

AppleScript stops using the application dictionary when it encounters the end tell statement at the end of your script.

A complex script may have several tell application statements that name different scriptable applications. In each case, AppleScript starts using the dictionary of the application named by the tell application statement, compiles subsequent statements using this dictionary, and stops using this dictionary when it encounters the next end tell statement. Because AppleScript gets all the information about an application’s commands and objects from the application itself, you never have to worry about controlling a new application. As long as the application has a dictionary, AppleScript can work with it.

Inspecting a dictionary

Just as AppleScript can get information about an application’s commands and objects from its dictionary, so can you. Using Script Editor, you can display the AppleScript dictionary of an application to see what commands the application understands and what objects the commands work with. You can also look at the dictionaries of scripting addition files.

Displaying a dictionary window

In Script Editor, choose Open Dictionary from the File menu. A dialog appears, listing scriptable applications and scripting additions. Select an application or scripting addition file in the list and click Open. Script Editor displays a dictionary window for the application or scripting addition you selected, as shown in Figure 23-3.

click to expand
Figure 23-3: An AppleScript dictionary defines suites of commands and objects for a scriptable application or a scripting addition file.

Tip

In the Open Dictionary dialog, you can select several applications and scripting additions whose dictionaries you want to display (each in a separate window). To select adjacent items in the list, drag across or Shift-click the items. To select nonadjacent items, z-click each item that you want to select.

Looking at a dictionary window

The left side of a dictionary window displays a list of commands and objects. The commands are displayed in plain text, and objects are displayed in italics. Script Editor groups related commands and objects into suites and displays the names of suites in bold. You don’t have to worry about suites when you’re scripting.

The right side of a dictionary window displays detailed information about the term or terms selected on the left. To see a detailed description of one term, click it on the left side of the window. To see detailed descriptions of all terms in a suite, click the suite name.

Tip

You can also see descriptions of several adjacent terms by dragging across them on the left. You can see descriptions of nonadjacent terms by z-clicking each term on the left.

The description of a command briefly explains what the command does and defines its syntax. In the syntax definition, bold words are command words that you must type exactly as written. Words in plain text represent information that you provide, such as a value or an object for the command to work on. Any parts of a syntax definition that are enclosed in brackets are optional.

The description of an object very briefly describes the object and may list the following:

  • Plural Form. States how to refer to multiple objects collectively. For example, you can refer to a specific window or to all windows.

  • Elements. Enumerates items that can belong to the object. In a script, you would refer to item of object. For example, you could refer to a file named index.html in the folder named Sites as file "index.html" of folder "Sites".

  • Properties. Lists attributes of an object. Each property has a name, which is displayed in bold, and a value, which is described in plain text. Scripts can get and set property values, except that properties designated [r/o] (means read only) can’t be changed.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net