You'll get your Mac news here from now on...

Help TMO Grow

Subscriber Login

Advertising Info

The Mac Observer Express Daily Newsletter

More Info

Site Navigation

Columns & Editorials
Mac Links

by Stephen Swift

AppleScript Lists
June 5th, 2002

This marks the 8th installment of AppleScript: The Macintosh Autopilot. If you find any of this tutorial not making sense, check out the some of the earlier installments.

Before I begin, I would like to mention a new feature I'm adding to these AppleScript lessons: downloadable script files. When you see a script icon, click on it to download the script, which are .sit files. When you decompress them, you should be able to open them with the script editor. For this lesson, I've created one script file that contain all the script examples. If in the future, you want to download each script separately, let me know. If you have any comments on this new feature, please let me know. Thanks! And now onto today's lesson!

To-do lists, grocery lists, top ten lists... lists are everywhere, so it's probably no surprise that AppleScript has its lists as well. How else would you be able to store or group data easily? You can't. You might be able to find another solution, but it won't be as elegant as using a list. Lists are an important feature of AppleScript and they are one of the easiest objects to use.

What do lists look like?

A list is a collection of data. Lists are enclosed by braces. Commas separate the values in a list. Lists can contain any sort of value including strings, variables, numbers, dates, Booleans (true or false), and even other lists. For Example:

{"hello", variable, 5, date "Monday, April 1, 2002", true, {3, "another", "list"}}

When are lists used?

Let's start out with a basic use of the list. Dialog Boxes can only contain a maximum of 3 buttons. What if you have 4, 5, or even 10 options for the user to choose from? You can use a list. (Note: this will not work with Mac OS 8.1 or below.)

Open the Script Editor and choose "Open Dictionary" from the file menu. Press the Go to "Scripting Additions" folder button. Now select "Standard Additions." A window will open with a list of commands to choose from on the left side. If you click on a command, a description of the command will appear on the right side of the window.

Standard Additions Dictionary

Select "choose from list" from the left side (it's under "user interaction"). On the right side will list all the components of this command. The items in bold are the commands, the blue underlined items are the values that are connected with the commands and the italicized gray text are the comments about each command. (Note: You must have set up your AppleScript formatting settings as I did in the first AppleScript column). Using the dictionary, let's create a dialog box that allows the user to choose from a list.

The required command is "choose from list." The value that is connected with this command is a list. The script will look like this:

choose from list {"Mac OS 8.5", "Mac OS 8.6", "Mac OS 9", "Mac OS X"}

Run that script and see what happens. Now let's get a little fancy. What if we wanted to give the user some instructions? We would use the sub-command (commands contained in our main command) "with prompt." The value connected with this sub-command is a string. This line would look like this:

with prompt "Select Your Operating System"

Now let's have the dialog box pick an operating system as the default option. We would use the sub-command "default items" connected to a list of items." This line would look like this:

default items {"Mac OS X"}

Of course, most people running Mac OS X have both Mac OS X and Mac OS 9 on their computers, so we really should have both selected. We can do this, because this sub-command allows for more than one item in its list. However, first we have to allow our dialog box to allow multiple selections. We can do this with the sub-command "multiple selections allowed." The line would look like this:

with multiple selections allowed

Now we can modify our "default items" line to this:

default items {"Mac OS 9", "Mac OS X"}

Now if the user did not want to select any item on the list, we have to add another sub-command called "empty selection allowed." Without this command, the "OK" button will remain grayed out until the user selects something. To allow this, add this line:

with empty selection allowed

Other features of this command include the option of customizing the "OK" and "cancel" buttons. Using the sub-commands provided, we can change the names with this code:

Thus, a complete "choose from list" dialog box with all the bells and whistles looks something like this:

choose from list {"Mac OS 8.5", "Mac OS 8.6", "Mac OS 9", "Mac OS X"} with prompt "Select Your Operating System" default items {"Mac OS 9", "Mac OS X"} OK button name "I'm Done" cancel button name "I'm Not Talking" with multiple selections allowed and empty selection allowed

Run this script. Now let's look at the result. If the user has selected items, a list such as {"Mac OS 9", "Mac OS X"} will be in the result window. If the user did not select any items then an empty list, {}, will appear. If the user selected cancel, "false" appears in the result window.

Silly List Tricks

To get the number of items in a list you would use this command:

count {"a", "b", "c", 1, 2, 3}
--result: 6

This comes in handy when you use repeat loops based on lists (next lesson). If you just wanted to count the number of integers in a list you would use the command:

count integers in {"a", "b", "c", 1, 2, 3}
--result: 3

To get one item in a list you would use the command:

item 3 of {"a", "b", "c", 1, 2, 3}
--result: "c"

Again, this is useful when it comes to repeat loops. If you didn't care which item was returned you could use this command:

some item of {"a", "b", "c", 1, 2, 3}
--random item of the list

If you wanted multiple items, you could use the command:

items 4 thru 6 of {"a", "b", "c", 1, 2, 3}
--result: {1,2,3}

If you wanted to remove the first item of a list, you could use the command:

rest of {"a", "b", "c", 1, 2, 3}
--result: {"b", "c", 1, 2, 3}

If you wanted to sort the list from last to first you could use the command:

reverse of {"a", "b", "c", 1, 2, 3}
--result: {3, 2, 1, "c", "b", "a"}

To change a list to a string, use this command:

{"a", "b", "c", 1, 2, 3} as string
--result: "abc123"

Of course, each item of the list has to have the ability to change into a string individually for this command to work, but I'll talk more about that later.

To add an item to a list, use this command:

{"This"} & {"is", "a", "list"}
--result: {"This", "is", "a", "list"}
{"is", "a", "list"} & {"This"}
--result: {"is", "a", "list", "This"}

That is probably enough to get you started with list manipulation. There isn't much else one can do without the script becoming complicated. These tips might not seems useful right now, but when you need to manipulate a list you'll be grateful to have this reference.

A Special Type of List

You may encounter what as known as a record in some scripts. It may appear to be a list, but it acts differently. Lists and records are cousins. What makes a record special is that each item has a name (called a label).

What do Records look like?

A record consists of many properties (please don't confuse these properties with the variable type of properties) separated by a comma and enclosed in brackets. The syntax of a record property is very similar to the syntax that of a variable property. There is a name and a value separated by a colon. For Example:

{name:"Stephen", height:74.5, weight:175}

Think of an AppleScript record like a record in a database. There are categories in each record, each with its own value.

What are they useful for?

I don't want to get too in-depth on this yet, but records are useful for storing and retrieving data. If I wanted the value of the property "name" in my record, I would use the code:

set AppleScript_Guru to {name:"Stephen", height:74.5, weight:175}
name of AppleScript_Guru
--result: "Stephen"

Pretty neat. Records are especially useful when it comes to using an application's dictionary (I'll talk more about that later).

The Last Word

Strings, lists, and records are called "value classes" in AppleScript lingo. This translates in English to an object (in this case) data that contains a value. This becomes important when we are working with an application's dictionary.

This Lesson Covered:

  • What lists are and how to use them
  • How to create a "choose from list" dialog box using an AppleScript dictionary
  • How to manipulate the items in a list
  • What records are and how to use them
Next Time:
  • Learn all about repeat loops (and how they relate to lists!)
  • Learn how to dissect a dictionary using records
  • More info about manipulating data and some limitations
More Info

Join the AppleScript gang at the Coder's Corner in The Mac Observer Forums! We discuss AppleScript related news and updates, extract really cool and useful AppleScripts, ask questions, and give answers. It's a lot of fun and a great resource for all your scripting needs! Also, if you have a befuddling AppleScript problem, please send it to and I'll be more than willing to help you out!

Comments or Questions? Is this column going to slow or fast for you? Do you want to script something, but don't know how? Do you need something explained or have a question about a script?  My E-mail address,, is open 24 hours a day, 7 days a week.

Most Recent AppleScript Columns

Break Your Script Into Pieces with Subroutines
June 26th

Repeat Yourself With AppleScript
June 12th

AppleScript Lists
June 5th

AppleScript Archives

Back to The Mac Observer For More Mac News!

Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News


[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Browse the software section for over 17,000 Macintosh applications and software titles.

Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .