List

A list is a collection, corresponding roughly to what many other languages would call an array—it’s an ordered set of values. These values are its items. Each value can be of any datatype (including a list).

A literal list is delimited by curly braces. Its contents can be literal values, variable names, or any other expressions that AppleScript can evaluate meaningfully; they are separated by commas. The literal empty list is just a pair of curly braces. So:

set empty to {}
set pep to {"Mannie", "Moe"}
set pep3 to "Jack"
set pep to pep & {pep3} -- {"Mannie", "Moe", "Jack"}

You can assign a list of values to a literal list of variable names or other references as a shorthand for performing multiple assignments. The assignments are performed pairwise in order: item 1 to item 1, item 2 to item 2, and so on. If the list of values is too long, the extra values are ignored; if it’s too short, there’s a runtime error. (See Section 7.1 and Section 10.2.2.) For example:

tell application "Finder"
        set {oldname1, oldname2} to {name of folder 1, name of folder 2}
        set {name of folder 1, name of folder 2} to {"f1", "f2"}
end tell

When you use set (as opposed to copy) to set a variable to a value that is a list, you set the variable by reference. This means that the list is not copied; the variable’s name becomes a new name for the list, in addition to any names for the list that may already exist. The same is true when a list is passed as a parameter to a handler. This special treatment ...

Get AppleScript: The Definitive Guide 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.