AppleScript_ The Definitive Guide - Matt Neuburg [101]
tell application "Finder"
tell folder 1
h( ) -- error: Finder got an error: Folder 1 doesn't understand the h message
end tell
end tell
tell application "Finder"
using terms from application "iPhoto"
start slideshow -- Finder got an error: Can't continue start slideshow
end using terms from
end tell
tell application "Finder"
h( ) -- error: Finder got an error: Can't continue h
end tell
The get and set commands (and also sometimes copy) are treated in a special way: they are not messages in the sense just mentioned, but are instead somehow short-circuited so that they always work. The reason is, I suppose, that otherwise it might be possible for code or a target application to break or interfere with them and then nothing would work at all.
The message-target metaphor is also made somewhat problematic by the existence of scripting additions (see Chapter 21). Scripting additions cannot be targeted, but the commands they implement are available everywhere.
Attributes
The second way in which values are object-like in AppleScript is that they can have attributes . A attribute is a named value belonging to the object. The most intuitive way to understand attributes is through the notion "has." A list has a length; a string has characters; a folder has a name; an iTunes track has an artist.
There are two important things to understand about attributes. First, attributes are values like any other values, which means they are themselves objects. There is thus a relationship between two objects as owner and attribute. The object that is the attribute may itself have attributes, and so we can end up with a vast chain or tree linking many objects. This structure is called an object model and is crucial to your understanding of how to talk to a scriptable application. Every scriptable application has an object model, which functions as your map of that application's world.
Second, attributes are accessible only through their owner. For example, iTunes has a huge object model representing all the tracks in all your playlists, and all the various attributes of those tracks; but there is only one object to which you have direct access—the iTunes application. All the rest of iTunes's object model belongs to iTunes, and to access any object in it, you have to ask iTunes. If this reminds you of how script objects and their top-level entities work, it should (see "Top-Level Entities" in Chapter 8).
Class
Every value is of some fixed and definite type . I usually refer to this as its datatype , but the AppleScript term for speaking of a value's type is class . You can use the term class to inquire of any value what its datatype is:
class of 7 -- integer
class of "howdy" -- string
class of {"Mannie"} -- list
class of class of 1975 --class
As the last line shows, even something's class is a value and therefore has to have a class, namely class.
Tip
Strictly speaking, the datatype of terms like integer and class is not really class but rather type class . It's as if every value has an attribute called class whose value is itself a type class. However, AppleScript will never tell you that the class of anything is type class. You may see a class referred to as type, though, in a dictionary or error message:
2 as boolean -- error: Can't make 2 into type boolean
You can assign a value of any class to any variable, but at a given moment a variable has only one value and that value has only one class (so it is customary to speak of a variable's class, meaning the class of the value it has at that moment).
It is the class of something that determines what messages can be sent to it and what attributes it has. For example, a list has a length because it is a list. I am guaranteed that I can ask any list for its length and get back a meaningful answer. I am also guaranteed that I can send the count message to any list and get back a meaningful response (the same as its length, actually).
The classes of many of the objects you'll be interested in when you're working with AppleScript will be classes