AppleScript_ The Definitive Guide - Matt Neuburg [184]
Defects in the Object Model
An application's object model is a hierarchy, essentially equivalent to the chain of ofs allowing you specify any of the application's objects (see "Properties and Elements," earlier in this chapter). Clearly this hierarchy requires a starting point; there must be some ultimate, top-level object in terms of which all others may be specified. In an Apple event, that top-level object is null( ). But in AppleScript there is no way to express this null( ) explicitly; it is simply supplied for you as the end of the chain, whenever you specify an object. (You can see this in Example 3-1.) Therefore you must rely on convention. And the convention is that the application class should correspond to the top-level null( ) object. This makes sense, as in a tell block the application is ultimately the only thing you can get a reference to in order to start referring to its objects. That's why your starting place when you study a dictionary is usually its application class. You assume that this is the top of the object model hierarchy, and you look to see what properties and elements it has, in the expectation that these will lead you wherever you want to go.
The object model, however, may not be a simple hierarchy. A number of things can undermine this straightforward picture.
Secret shortcuts
Here are the elements of iTunes's application class: browser window, encoder, EQ preset, EQ window, playlist window, source, visual, window. You'll notice that none of these is your library, and none of them is a playlist. Yet these are the things you're most likely to want to refer to. So how are you expected to talk about them? You can spend ages mucking about in the dictionary looking for some way to reach playlist level from here—but it turns out that you could have asked for a playlist directly all along. Indeed, it turns out your library is a playlist:
tell application "iTunes"
get playlist "library" -- library playlist id 40 of source id 35
end tell
Notice the discrepancy between what you said to iTunes and the reference iTunes handed you. In theory a playlist is an element of a source, and a source is an element of the application, and that's the path you should have to travel to reach a playlist. But in actual fact there's a shortcut that lets you jump right from the application to a playlist. That's excellent, but the dictionary never told you so.
This book has been full of examples of the Finder doing the same sort of thing:
tell application "Finder"
get folder 1 -- folder "Mannie" of desktop
end tell
As you can see, folder 1 is not the first folder at the top of your startup disk, as you might expect: it's the first folder on your desktop. Again, the dictionary never told you about this.
Your main source in iTunes (called the Library source), and the desktop in the Finder, are implicit subcontainers supplied when you speak of certain elements without qualification. The application gives you a shortcut for reaching certain places in its object model with traversing the whole path explicitly. But this fact isn't documented in the dictionary.
Such shortcuts needn't start at the top level. Consider the following:
tell application "Microsoft Entourage"
get email address 1 of contact 1 -- "matt@tidbits.com"
get label of email address 1 of contact 1 -- home
end tell
The first line makes it appear that "email address 1 of contact 1" is a string, but that makes the second line anomalous, as a string has no label property. The truth is that the email address class has a contents property; when you ask for an email address