10.1. Record Notation
In AppleScript, you use the special
record data type to store database information,
much like how you use the list data type
to store ordered sets of information. To
create a record in
AppleScript, you use this
format:
set recordName to {fieldName:fieldData, otherFieldName:otherFieldData, ...}
You've already used this notation, although perhaps
without knowing it, for the
make command's
with properties option.
Record notation
is useful for far more than just AppleScript commands, though;
records are also convenient for storing clusters of information.
Say you wanted to create a database entry for a chicken (storing its
nickname, weight, and height). You could use this code to do the
deed:
set myChicken to {nickname:"Matthew", weightInPounds:20, heightInInches:26.5}
Then, if you wanted to access a specific piece of information from
the record, you would use the ubiquitous of
keyword:
set myChicken to {nickname:"Matthew", weightInPounds:20, heightInInches:26.5}
display dialog (nickname of myChicken) --Gets the data from myChicken record
But just seeing the name of the chicken probably
seems pretty boring to you. By changing that script around a little,
you can use AppleScript to pull information out of the record and
string it together in an amusing dialog box message:
set myChicken to {nickname:"Matthew", weightInPounds:20,
heightInInches:26.5}
display dialog (nickname of myChicken) & " the chicken weighs " & ¬
(weightInPounds of myChicken) & " pounds. Man, that's one ¬
big chicken!"
Now when you run the script, a dialog box appears to display the
nickname of myChicken (Matthew) and the
weightInPounds of myChicken (20). The result is
a message that says: "Matthew the chicken weighs 20
pounds. Man, that's one big
chicken!"
 |
Just like other AppleScript variables, you can also
set the values inside a record
using the
of keyword. For instance, this script:
|
|
set myWardrobe to {shirts:7, pants:2}
set the shirts of myWardrobe to 6
 |
updates the shirts value inside the
myWardrobe record with 6 instead of 7.
|
|
 |