Name

list

Allowed coercion

string, if the data type of each item in the list can legally be coerced to a string

Tip

A single-item list (such as {44}) can be coerced to any data type that the item could be coerced to if it were not in a list. For example, {44} could be cast or coerced to a string, because 44 is an integer, and AppleScript permits that data type to be coerced to a string.

Syntax

set theList to {"Mercury","Mars",pi,3.14} as list

Description

An AppleScript list is close to what other languages such as Perl or Java would call an array. In AppleScript, you can store items of any data type in a list, even other lists. You can mix data types among list items (store strings, numbers, and other objects in the same list). The items as a group are surrounded by curly braces and separated by commas. For example:

set theList to {"Mercury","Mars",pi,3.14}

includes two strings, the pi predefined variable (which AppleScript will evaluate to about 3.14159265359), and a real number.

A list is a data type that you will encounter often in AppleScript. Several AppleScript commands return lists, such as getting every item in a container (e.g., get every folder of desktop) You can use the following properties with a list:

class

Always returns list. Test a return value or variable to find out if it is a list by using this property, as in:

class of theList
length

Returns the number of items in a list, as in:

length of theList
rest

Returns a value of type list containing every item but the first one. ...

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.