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 record

Description

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 ...

Get AppleScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.