Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [132]

By Root 1632 0
make a list and record that mutually recurse. Does anyone have an aspirin?

Record Properties


The following are the properties of a record:

length

The number of items in the record. This property is read-only. You can get the same information by sending the record the count message.

The names of the items

Every name of every item is a property of the record.

Please pretend now that I'm jumping up and down, waving a big red flag and screaming: the names of a record's items are properties. The names are not strings; the names are not any kind of variable or value. They are effectively tokens created by AppleScript at compile time, like the names of variables. Actually, these tokens are created in two different ways. If the name is an existing property or class name, the token is its four-letter code, and the item is stored as a property. Otherwise, it is a string and is called a user property; internally, user property -value pairs are stored as a list (called a 'usrf') of alternating names and values.

Thus, a record consisting of two predefined properties and two user properties actually contains three items internally: the two predefined properties, followed by a list of four items (the name and value of each user property). For example:

set R to {name:"Matt", character:"impeccable", town:"Ojai", age:51}

It happens that name is a property and character is a class implemented in AppleScript itself, so these are tokenized as their four-letter codes, 'pnam' and 'cha ' respectively. But town and age are user properties, so the third item of storage in the record is a 'usrf' list containing the strings "town" and "age" and their values:

{

'pnam':"Matt",

'cha ':"impeccable",

'usrf':[

"town",

"Ojai",

"age",

51

]

}

You can avoid having AppleScript treat existing property names as properties by putting those names in pipes (vertical bars). If you do this, you must put that name in pipes to refer to that item of the record later on, as otherwise AppleScript thinks you're talking about a property, which isn't defined for this record.

set R to {|name|:"Matt", |character|:"impeccable", town:"Ojai", age:51}

get |name| of R -- "Matt"

get name of R --error: Can't get name of...

When you talk to a record, it is the target, and its item names are used to interpret the vocabulary you use. The first thing AppleScript does is look to see whether any of this vocabulary is the name of an item of the record. That's why you can't assign to a nonexistent item in a record—the name you're using for that item is meaningless. No terminological confusion normally arises, because the context is clear. So:

set town to "Ojai"

set R to {name:"Matt", town:null}

set town of R to town -- no problem

Of course, you can confuse AppleScript if you set your mind to it. This code just sets the existing value of the variable town to itself; the record is untouched:

set town to "Ojai"

set R to {name:"Matt", town:null}

tell R

set town to town

end tell

R -- {name:"Matt", town:null}

But you know how to fix that—right? (Hint: see Chapter 11.)

set town to "Ojai"

set R to {name:"Matt", town:null}

tell R

set its town to town

end tell

R -- {name:"Matt", town:"Ojai"}

There is no built-in way to obtain a list of the names of the items of a record. A record has no such introspective abilities. You (a human being) can see the names of the items of a record in AppleScript's display of the record's value. But your code can't see this information; the names of the items are not values of any kind, and cannot easily be turned into values. I have seen many elaborate attempts to work around this problem, but I'm not going to show you any of them. This is a big shortcoming of AppleScript itself, and it needs to be fixed on the level of AppleScript itself. Until it is, you can get assistance from Late Night Software's free List & Record Tools scripting addition. For example:

set R to {name:"Matt", town:"Ojai"}

get property IDs of R -- {"pnam"}

get user property names of R --{"town"}

It is possible to fetch or assign to

Return Main Page Previous Page Next Page

®Online Book Reader