Apple Events are a way to send messages into applications, messages that can originate from the desktop, the system, or other applications. AppleScript is a particular language designed to be both readable and to send Apple Events to any application that can receive them. In keeping with the Mac OS tradition of ease-of-use, the AppleScript language is designed to be as “natural” as possible, just as the graphical user interface is designed to look and feel like a desktop. AppleScript programs are generally easy to read. The language is based largely on HyperCard’s HyperTalk language, extended to refer not only to the HyperCard world of cards and stacks, but theoretically to any document. To this end, the AppleScript team introduced the AppleEvent Object Model (AEOM), which specified the objects any particular application “knew” about.
Generally, AEOM defined a number of objects, like “document”, character, or “paragraph”, and the actions that could be done to them, like “cut” and “close”. The system also defined ways to refer to properties of objects, so one could refer to the “third paragraph of the document ‘Good Day’”, or the “color of the last word of the front window”. AEOM uses an application dictionary to associate the Apple Events with human-readable terms, allowing the translation back and forth between human-readable AppleScript and coded Apple Events. To discover what elements of a program are scriptable, dictionaries for supported applications may be viewed. (In the Xcode and Script Editor applications, this is under File â†’ Open Dictionary.)
To designate which application is meant to be the target of a message, AppleScript informs the application using a “tell block”:
tell application "iTunes" quit end tell
Alternatively, the tell block may be expressed in one line by using an infinitive:
tell application “iTunes” to quit
Though for events in the “Core Suite” (activate, open, reopen, close, print, and quit), the application may be supplied as the direct object to intransitive commands:
quit application “iTunes”
The concept of an object hierarchy can be expressed using nested blocks:
tell application "QuarkXpress" tell document 1 tell page 2 tell text box 1 set word 5 to "Apple" end tell end tell end tell end tell
The concept of an object hierarchy can also be expressed using nested prepositional phrases:
pixel 7 of row 3 of TIFF image “my bitmap”
Many applications do not store information on anything but the lowest level objects they deal with. A word processor almost certainly has an object that understands all of the characters in a document, but may not have objects representing collections like pages, paragraphs or whole documents. Since the developers of AppleScript intended it to be added to existing applications without too much redesign by the authors of the applications, they also provided a second way to write the means to access aggragate elements. These are called iterators, and using them requires that the developer keep track of only the current location of an object and also provide a means for finding the “next one”. For instance, paragraph objects could be supported with a single pointer to the current paragraph, and a method that finds the next one by looking for a return character. Words are supported by paying attention to non-printing characters in the same way.
Note the similarities between the AEOM (Apple Events Object Model) and the considerably more recent DOM (Document Object Model) system used in XML. Both recognize and can break down a document into a hierarchy of objects, and offer the programmer a standardized iterative method to access the contents at any level. Differences between the systems lie primarily in the user-level syntax, with AppleScript introducing a number of different ways to refer to any particular object. For instance, AppleScript includes syntax for ordinal counting, “the first paragraph”, as well as cardinal, “paragraph one”. Likewise numbers can be referred to as text or numerically: “five”, “fifth” and “5” are all supported. In a list, beginning is before any item in the list and end is after any item in the list. Collections can be addressed with words like “containing” and “is contained in”.