AppleScript tip: basic list manipulation

By Alvin J. Alexander, devdaily.com

I'm frequently asked how to create lists in AppleScript. Here's a quick example of a few different lists. First, some favorite foods:

set favoriteFoods to {"cookies", "cake", "cereal"}

Next, some of those other foods:

set moreFoods to {"broccoli", "carrots", "lettuce"}

After you have a couple of lists like that you can easily combine them into a new list:

set favoriteFoods to {"cookies", "cake", "cereal"}
set moreFoods to {"broccoli", "carrots", "lettuce"}

set allFoods to favoriteFoods & moreFoods

After that you can extract items from your list with the item operator:

set cake to item 2 of allFoods

You can also grab several items from the list like this:

set badCombination to items 2 through 5 of allFoods

And finally, you can count the number of items in a list using the count operator:

set numItems to count allFoods

devdaily logo