Name
record
Allowed coercions
list (but all the names from the name/value pairs are thrown out) |
Syntax
set theRec to {name: "AppleScript in a Nutshell", subject:¬
"AppleScript"} as recordDescription
A
record value type is close to what a Perl
programmer knows as a hash or associative array and what a Java
programmer would recognize as the HashMap class. This is a powerful
data type that lets you store name/value or property/value pairs in a
variable. These values are then accessible by the property name (not
the item number). For instance:
get name of theRec
from the preceding syntax example returns “AppleScript in a Nutshell.” But:
get item 1 of theRec
raises an error; you just cannot use the latter reference method.
Examples
You can find out how many property/value pairs there are in a record
by getting its length property, as in:
length of theRec
(which returns 2). You can change values by referring to the property
name (unless the record is a read-only application
property). You can also add to a record by
concatenating another record to it:
get length of theRec -- returns 2
set subject of theRec to "AppleScript language"
set theRec to theRec & {users:"Mac scripters"}
get theRec (* returns {name:"AppleScript in a Nutshell", subject:"AppleScript
language", users:"Mac scripters"} *)You can coerce a record to a
list type, but the record (now
a list) will lose all of the property names. For
example:
get theRec as list
will return:
{"AppleScript In a Nutshell", "AppleScript language", "Mac ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access