Every scriptable application defines an AppleScript object model to specify the classes of objects a scripter can work with in scripts, the accessible properties of those objects, and the inheritance and containment relationships for those classes. Inheritance allows a class to access the properties of its ancestors. Containment specifies where an object resides within the hierarchy of objects in the running application.
The objects in the object model often correspond closely to object classes in the application that implement scripting support, but there is no requirement that they do so. For example, a text application might provide script access to words in a document, but it would not be efficient for the application to maintain a corresponding object for each word.
Application classes have attributes, to-one relationships, and to-many relationships. AppleScript classes in the object model have properties and elements—properties are synonymous with attributes and to-one relationships, while elements are synonymous with to-many relationships. For more information on these and related terms, see the “Glossary.”
Figure 1-1 shows the object-model containment hierarchy for a specific document (shown in Figure 1-2) in the Sketch application. On the left are the objects a scripter uses to work with the application. On the right are the objects that Sketch uses to represent its object model.
In this object model, documents are elements of the application object and graphics are elements of document objects, while the name of a document is a property of the document. For a script to access an object in the hierarchy, it must locate the object within its containment hierarchy. (In the application, the orderedDocuments array is a to-many relationship of the NSApplication object.)
Consider, for example, the following sample script:
tell app "Sketch" to set the x position of rectangle 1 of document "SketchDocOne" to 25 |
This script specifies a rectangle, in a document, in the application. If applied to the following Sketch document, it would set the horizontal (x) position for whichever of the two rectangle shapes is first in the document's ordered list of graphics.
A script can ask for rectangle 1 of document "SketchDocOne" without having to specify either graphics or documents (though they are part of the hierarchy shown above) because an AppleScript class definition implicitly specifies the relationships of its contained elements.
Sketch's object model uses inheritance for its graphics objects. All such objects (rectangle, circle, image, line, and text area) inherit from a common ancestor (graphic). Thus the term graphic 1 of document "SketchDocOne" might specify the same object as rectangle 1 of document "SketchDocOne", depending on the ordering of the objects in the graphics array.
Note: Cocoa applications typically follow the Model-View-Controller (MVC) design pattern, where model objects encapsulate and manipulate the data used by the application. It is generally recommended that you support scriptability through your model objects, which tend to be more persistent. For more information, see “Concentrate Scriptable Behavior in Model Objects.”
Don't confuse the AppleScript object model with MVC model objects: the former is a structure you define; the latter are objects in your application that may or may not be part of your AppleScript object model.
Last updated: 2008-03-11