AppleScript_ The Definitive Guide - Matt Neuburg [109]
We have already, in several examples, taken advantage of this ability to obtain a reference and operate on it. References will be fully discussed in Chapter 12. But for now I must warn you: a reference is not a copy! So what you do to it will truly be done in the real world. For example:
tell application "Finder"
set d to disk of folder 1
end tell
set name
of d to "yoho" --Don't say this unless you mean it!
That code really will change the name of your hard disk, even though you're not explicitly targeting the Finder.
Element Specifiers
Referring to a property is easy; you just use the name of the property. For example:
get version of application "Finder" -- 10.4.2
Referring to an element is harder. An object can have any number of each class of element, so you must say which one(s) you mean. To do this, you use an element specifier (or just specifier for short—AppleScript also calls this a key form). A specifier has two components: the name of a class and some way of picking out the right one(s). AppleScript has eight built-in forms of specifier, and these are the only ones you are allowed to use. The next eight sections describe those eight specifier forms.
(Actually, there are actually nine element specifiers. I don't discuss middle because it is rarely used. Plus, a reference to a property is actually a form of specifier, so I guess that makes ten. The variety of specifier forms makes a specifier quite an interesting and complicated part of an Apple event. The repeated pattern involving the four terms form, want, seld, and from in Example 3-1 denotes a specifier.)
In real life, it will rarely be open to you to use just whichever specifier form you please on a particular occasion. Given a certain application, object, and class of element, only certain specifier forms will work, and experimentation is the best guide as to which ones they are. An application's dictionary is supposed to help you here, but it might not, or might not be accurate (see "Defective Element Specifiers" in Chapter 20).
Name
An element may have a name, which is some kind of string. (I say "some kind of" because it might, for example, be Unicode text, which is not the same class as string.) To specify an element by name, say the class followed by the name:
tell application "Finder" to get disk "main"
You may insert the keyword named between the class and the name, but I never do.
Typically, there is also a name property, so that you can learn, based on some other element specifier, how to specify a particular element by name:
tell application "Finder" to get name of disk 2 -- "gromit"
Index
Elements are usually ordered, and numbered in accordance with this ordering. The number is an index . The first element has index 1, the next element has index 2, and so forth. The last element can be referred to by index -1, the next-to-last by index -2, and so forth. (If you want to know just how many elements of this class there are, you have to find out in some other way, such as count.)
To specify an element by index, say the class followed by the index number:
tell application "Finder" to get disk 2
You may insert the keyword index between the class and the number, but I never do. Instead of a cardinal number, you're allowed to use a wide variety of English-like ordinal numeric literals followed by the class name. So you can say such things as 1st disk, third disk, last disk, front disk, and back disk.
There is sometimes also an index property, so that you can learn, based on some other element specifier, how to specify a particular element by index, but this is not implemented anywhere near as often as one would like, and is sometimes buggy:
tell application "Finder" to get index of disk 2 -- 3, for heaven's sake
ID
Elements may have a unique and unchanging ID, which is often a number but needn't