Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [165]

By Root 1581 0
as they don't exist within AppleScript itself, AppleScript cannot know without a dictionary that the programmer isn't just talking nonsense. At the same time, AppleScript uses the dictionary to resolve the terms into their corresponding Apple event form; otherwise, it wouldn't know what actual Apple event messages to send to the scriptable application at runtime. And it uses the dictionary when decompiling, to translate those Apple event terms back into English-like words for display to the programmer.

The AppleScript programmer

The AppleScript programmer studies a human-readable display of a dictionary to learn what English-like terms, beyond those built into the AppleScript language itself, may be used when targeting a particular application, or in the presence of a particular scripting addition. Studying a dictionary to figure out how to use AppleScript to get an application to do your bidding is a major part of the typical AppleScript programming experience. Such study must often be combined with experimentation when the dictionary is insufficiently informative.

The dictionary mechanism pervades the life of the AppleScript programmer in several ways:

The dictionary is used by AppleScript for resolution of terminology . Therefore you have to make certain that your script points AppleScript properly to any required dictionaries, and that it can find these dictionaries at key moments (compilation, decompilation, and runtime).

The dictionaries of scriptable applications and scripting additions invade the available namespace. This invasion creates clashes between terminology used in one dictionary and another, or between one dictionary and the terms that you, the programmer, would like to use. The result is a high probability that something you'd like to say will be misinterpreted or forbidden.

The dictionary is usually your only documentation on how to communicate with a given application. But a dictionary, in and of itself, by the very nature of its format, is almost certainly going to fulfill this function inadequately. To make matters worse, dictionaries are often badly written or downright misleading. You therefore face the paradox of being dependent upon the dictionary for information that may be insufficient, hard to understand, or wrong.

This chapter discusses all these aspects of dictionaries. See also Chapter 3 for the format in which a dictionary is stored and for the implications when a needed dictionary is missing. See "Target" in Chapter 11, and "Tell" and "Using Terms From" in Chapter 19, for details on how the target is determined, how messages are sent, and how AppleScript decides what application's dictionary it will use to resolve terminology. In Chapter 21 I'll talk more about what scripting additions and their dictionaries add to the mix, and in Chapter 27 we'll actually construct a small dictionary to make an application scriptable. Also, see Appendix A for an example of a real programmer struggling valiantly with a real dictionary.

Resolution of Terminology


Example 20-1 exhibits some common patterns of terminology usage. With it, we'll model AppleScript's interaction with dictionaries during the process of compilation.

Example 20-1. Simple terminology resolution

tell application "Finder"

set c to display dialog ((count folders) as string)

end tell

Compilation of code like Example 20-1 proceeds in two distinct stages:

The tell block causes AppleScript to locate a particular application and load its dictionary.

The terms inside the tell block are resolved.

Let's consider these stages one at a time.

Loading the Dictionary


As AppleScript's compiler encounters a tell block (or a terms block) targeting a literal application, it attempts to locate this application and load its dictionary. If the compiler can't find the application, it will ask the user where it is; if the user cancels out of this process, refusing to choose an application, AppleScript will not compile the script (see "Missing External Referents" in Chapter 3).

AppleScript will proceed

Return Main Page Previous Page Next Page

®Online Book Reader