Chapter 6. Creating Lists


Whether you're a car salesman or a carpenter, you use lists every day. Navigating to your Applications folder, scheduling your events for the day, or just shopping for groceriesall require a list of some sort. And just like those lists you make throughout the day, an AppleScript list contains an ordered sequence of items.

AppleScript lists can include just about anything you wantthe test scores of several students, for example, or some important to-dos you have to remember. You create lists in AppleScript by surrounding your items in curly bracketsand separating each item with a commalike this:

{"Feed cat", "Eat breakfast", "Clean out earwax"} --Three strings in a list

Lists don't have to just store strings, though; you can put numbers, file aliases, and even dates into your lists, like this:

{"Dude", 53.87, alias ":Applications:TextEdit.app", current date}

There'd rarely be a case when you'd want to store a number, file alias, and date within the same list, but you can if you want. A far more common occurrence, though, would be to create a list of just numbers (for tracking test scores) or just aliases (for keeping track of your favorite folders), for example.

A list may contain only a single item, too. You might create a single-item list if you want to note your accomplishments on a particularly uneventful day, for example:

{"Went to sleep"}

Or you can have a list with no items in itan "empty list." That's useful if you need to set aside a list for later usesay, a list of birthday presentsbut you aren't ready to fill in the list yet (perhaps because you haven't received any presents, for example). In any case, you symbolize an empty list with two curly braces: {}.

Looking for another list anomaly? Lists can contain other lists, making what are called "nested" lists. That's basically what a spreadsheet is: a list of rows, where each row is itself a list of cells.

To create a nested list, you'd type something like this:

set twoDList to {{"Name:", "Age:", "Home State:"}, ¬ {"John", 42, "Nebraska"}, ¬ {"Julie", 12, "South Dakota"}, ¬ {"Bob", 29, "Virginia"}}

This twoDList has four items, but each item is itself a list with three items. In essence, you've created a four row by three column spreadsheet.

Of course, lists would be all but useless if you couldn't preserve them for later use in your script. Thankfully, you can preserve a list in a variable, using the set command to store every single one of the list's items. That way, you can keep track of all your favorite cheeses, for instance:

set cheeseList to {"Cheddar", "Swiss", "Gorgonzola"}

The example scripts from this chapter can be found on the AppleScript Examples CD (see Sidebar 2.1 for instructions).



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net