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.
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: