Team LiB   Previous Section   Next Section
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 scripters"}.

Records can have expressions or variables as property values, as in the following example (however, you cannot use variable values for property names):

set myVar to "A variable"

set twoRec to {calc:(2 + 2.5), var:myVar} as record (* returns {calc:4.5, 

var:"A variable"} *)

set twoRec to {calc:(2 + 2.5), var:myVar,myVar: 7} as record (* returns 

{calc:4.5, var:"A variable",myVar: 7} and doesn't evaluate the myVar variable 

at the end of the record*)

You cannot use two-word property names when creating your own record. You will have to use capital letters or underscore characters to create more descriptive property names:

set climberName to {FirstName: "Edmund", last_name: "Hillary"}.

as opposed to:

set climberName to {First Name: "Edmund", Last Name: "Hillary"}.
    Team LiB   Previous Section   Next Section