Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [131]

By Root 1418 0


A record is an unordered collection of name-value pairs. Each value may be of any type. Records are passed to a few important commands, such as make, and are returned by scriptable applications and scripting additions as a way of providing a "table" of information (see Chapter 21). They are useful for passing as parameters to handlers because they can contain any number of items. AppleScript provides some operators for testing the contents of a record and for concatenating records to form a new record (see Chapter 15).

A literal record looks like a literal list except that each item has a name. The name is separated from the corresponding value with a colon:

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

There is no empty record as distinct from the empty list; the empty list is treated as the empty record for purposes of containment and concatenation. A record has no item elements, its items cannot be referred to by index number, and you can't talk about the beginning or end of a record.

You can assign a record of values to a literal record of variable names or other references as a shorthand for performing multiple assignment. The assignments are performed pairwise by name, independently. If the record of values includes names that aren't in the record of variables, the extra values are ignored; if it's missing any names that are in the record of variables, there's a runtime error. See "List," earlier in this chapter. For example:

local who, town

set {who:who, town:town} to {town:"Ojai", who:"Matt"}

{who, town} -- {"Matt", "Ojai"}

When you use set (as opposed to copy) to set a variable to a value that is a record, you set the variable by reference. This means that the record is not copied; the variable's name becomes a new name for the record, in addition to any names for the record that may already exist. The same is true when a record is passed as a parameter to a handler. This special treatment is in common between lists, records, dates, and script objects, the four datatypes that can be mutated in place. (See "Set by Reference" in Chapter 7 and "Pass by Reference" in Chapter 9.) For example:

set R2 to {who:"Matt", town:"Ojai"}

set R1 to R2

set who of R2 to "Jaime"

R1 -- {who:"Jaime", town:"Ojai"}

In a literal record, a variable representing a list, record, date, or script object is itself set by reference . For example:

set L to {"Mannie", "Moe"}

set R to {pep:L}

set end of R's pep to "Jack"

item 3 of L -- "Jack"

The only mutation you can perform in place on an existing record is to replace the value of an individual item by assignment. Other changes in the record require generation of a new record. So, for example, if you wish to add to a record an item with a name that doesn't already exist in that record, you must make a new record using concatenation:

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

set who of R to "Jaime"

R -- {who:"Jaime", town:"Ojai"}

set R to R & {friend:"Steve"}

R --{who:"Jaime", town:"Ojai", friend:"Steve"}

There is no penalty for concatenating an item with a name that already exists in a record , but it has no effect. For example:

set R to {who:"Matt", town:"Ojai"} & {who:"Jaime"}

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

Clearly, order is all-important here, and you can use this fact to your advantage. Suppose you want to assign a friend value within a record. If the record already has such an item, you can do it by assignment. If it doesn't, you can do it by concatenation . But what if you don't know whether it has such an item or not? You can do it regardless by concatenating in the opposite order:

set R to {who:"Jaime", town:"Ojai"}

set R to {friend:"Steve"} & R

R -- {friend:"Steve", who:"Jaime", town:"Ojai"}

set R to {who:"Jaime", town:"Ojai", friend:"Matt"}

set R to {friend:"Steve"} & R

R --{friend:"Steve", who:"Jaime", town:"Ojai"}

A record can recurse, for the same reason that a list can:

set R to {who:"Matt", town:"Ojai", cycle:null}

set cycle of R to R

who of cycle of cycle of cycle of cycle of R -- "Matt"

You can make records mutually recurse; you can even

Return Main Page Previous Page Next Page

®Online Book Reader