DekGenius.com
[ Team LiB ] Previous Section Next Section

10.6 Properties and Elements

Two objects may stand in a relationship where one is an attribute of the other. It is this relationship of attribution that is specified by the chain of ofs and tells. Attributes are defined in terms of classes. (The term "attribute" is of my own devising, because the official AppleScript documentation lacks any comprehensive term for "property or element.")

For example, a list has a length attribute and an item attribute—these are facts about any list because they are facts about the list class. That is what makes this code legal:

set L to {"Mannie", "Moe", "Jack"}
length of L -- 3
item 1 of L -- "Mannie"

Recall this code from Chapter 3:

tell application "FrameMaker 7.0"
        tell document "extra:applescriptBook:ch02places.fm"
                tell anchored frame 43
                        get inset file of inset 1
                end tell
        end tell
end tell

That code works because, in FrameMaker, the application has a document attribute, a document has an anchored frame attribute, an anchored frame has an inset attribute, and an inset has an inset file attribute. As we saw in Chapter 3, working out the chain of attributes so as to refer successfully to a desired object is a major part of working with AppleScript. An application's dictionary is supposed to help you with this, though it often falls short (Chapter 19). AppleScript's own dictionary is not typically visible, so this book describes the attributes of the built-in datatypes (Chapter 13).

An attribute is either a property or an element. A property is an attribute that this type of object has exactly one of. An element is an attribute that this type of object may have any number of, and in order to refer to one (or more) you have to say which one(s) you mean.

In the example about lists, length is a property; every list has a length, and that's the end of that. But item is an element; a list might not have any items, and if it does have some, it can have any number of them. To speak of an item or items we have to say which one(s) we mean, as in item 1. Similarly, in the FrameMaker example, inset file is a property but inset is an element.

A script object's top-level entities are effectively properties in this sense; that is why they can be referred to using the of operator. This is a bit confusing, because the word "property" is often used as shorthand for a script property, whereas script properties, script objects, and handlers can all be top-level entities of a script object. If there is ambiguity, we can call properties (of the sort under discussion in this section) object properties, to distinguish them from script properties. A script object cannot have elements.

Some properties are read-only. This means you can get but not set their value. For example:

tell application "Finder"
        get startup disk
        set startup disk to disk "second" -- error
end tell

Elements in general are read-only; you can't say set folder 1 to .... However, you can set an element's properties (except those that are read-only, of course), and applications often implement verbs permitting to you create and manipulate elements.

When you get a property or element, the value returned will be of some particular class. If this is one of AppleScript's built-in datatypes, what you get will usually be a copy. So, for example:

tell application "Finder"
        set s to name of disk 1
        set s to "yoho"
end tell

That code has no effect upon the name of the disk. A string came back and was stored in the variable s, and you then set the variable s to some other value, throwing away the string the Finder gave you.

But when the class of the returned value is an object type defined by and belonging to the application you're targeting, the value will usually be a reference (Chapter 11). Such a reference is a complete target. You can send a message to it, and you can get an element or property of it. You are not in control of what this reference looks like, and the way it looks may surprise you, but you shouldn't worry about that; it's a valid reference and a complete target, and that's all you should care about. For example:

tell application "Finder"
        set d to disk of folder 1
end tell
— startup disk of application "Finder"

What I may have expected to see as a result of asking for this property doesn't matter; I must have faith that the Finder has given me a reference to what I asked for. To justify this faith, I can proceed to target this reference:

tell application "Finder"
        set d to disk of folder 1
end tell
get name of d -- "xxx"

That works. Since d is a complete reference to a Finder object, I can target it; in the last line, the get message is sent to the Finder, and the name of the disk comes back. The term name is understandable outside of a tell block targeting the Finder because it is defined within AppleScript itself (see Chapter 19).

    [ Team LiB ] Previous Section Next Section