6.1. Working with Lists

A list is simply an ordered set of data, much like an array in other programming languages. You refer to each value in the list as an item. To write a list in your program, you simply write your items, separating each by a comma. The entire list of items is enclosed in a pair of curly braces. For example, the following defines a list containing three integer items:

{12,   17,   1989}

The first item of the list is the integer 12, the second item is the integer 17, and the third item is the integer 1989. Naturally, you can assign a list to a variable in your program like so:

set lindasBD to {12,   17,   1989}

This simply stores the three-item list in the variable lindasBD.

Here's another example of a list containing integer and real values:

set someConstants to (3.14159,  2.718282,  344,  299792458}

The first constant in this list is presumably the value of π, which you know is already defined in AppleScript with the predefined constant pi. So you can rewrite your list as follows:

set someConstants to (pi,  2.718282,  344,  299792458}

If you have the following variable definitions in your program:

set e to 2.718282
set speedOfSound to 344
set speedOfLight to 299792458

you can write the same list this way:

set someConstants to (pi, e,  speedOfSound,  speedOfLight}

You can also use expressions when writing list items. For example, here's a list containing the values π, π/2, and π/4:

set piValues to (pi, pi/2, pi/4}

The expressions are evaluated, and the results become the ...

Get Beginning AppleScript® 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.