AppleScript list manipulation examples

MacOS AppleScript list FAQ: How do I create lists in AppleScript?

There are several different ways to create AppleScript lists. Let's take a look at a few examples.

 

AppleScript list creation examples

Here's a quick example of a few different AppleScript lists. First, some favorite foods:

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

Next, some of those other foods:

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

Combining AppleScript lists

After you have a couple of AppleScript 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

AppleScript list items

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

AppleScript list count (list size)

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

set numItems to count allFoods