AppleScript “list” FAQ: Can you share some simple macOS AppleScript list examples?
Sure. As I’ve been doing a lot of work with Mac speech recognition software lately I’ve been working a lot of with AppleScript lists. Let’s take a look at some common AppleScript list examples (and programming needs).
How to create an AppleScript list
A basic, static AppleScript list can be created like this:
set groceryList to {"eggs", "milk", "bread"}
This creates an AppleScript object named groceryList
, which contains the list you see in the curly braces.
(In a few moments you’ll also see how to obtain an AppleScript list in Mac Finder and iTunes/Music examples.)
How to use an AppleScript list
Once you have a list in AppleScript you’ll want to be able to do something with it. This next example shows how to retrieve one item from an AppleScript list:
set myList to {"Problem", "There was a problem", "Bummer"} set theChoice to item 2 of myList say theChoice
This next example shows how to do something with every item in your list (how to iterate through the list):
set myList to {"Problem", "There was a problem", "Bummer"} repeat with theItem in myList say theItem end repeat
This example shows how to get a random item from your list:
set myList to {"Problem", "There was a problem", "Bummer"} set theChoice to some item of myList say theChoice
In that AppleScript list example I’ve chosen an item from the list at random using the AppleScript “some item” command.
Using ‘repeat’ to iterate over an iTunes list
Here’s a more complicated example where I show how to do something for each item in a list, in this case interacting with an AppleScript list I retrieve from iTunes:
tell application "iTunes" activate set results to (every file track of playlist "Library" whose artist contains "Tony Bennett") repeat with t in results play t end repeat end tell
In this example the variable results
contains the list that is returned from iTunes.
A lot of times that’s all the power you need, but if you want to create an AppleScript list, add items to the list, then loop through the list, you’re going to need a little more power.
Using ‘repeat’ with a Mac Finder list
Here’s an AppleScript list example that (a) creates a list of all files on your desktop, (b) gets the filename for each file by looping through the original list of file objects, then (c) displays the list in a dialog that you can view:
set listOfNames to {} tell application "Finder" set filelist to every file of the desktop repeat with currentFile in filelist set currentFileName to (the name of currentFile) copy currentFileName to the end of listOfNames end repeat end tell choose from list listOfNames
AppleScript list notes
Some cool things to note from these AppleScript list examples:
set filelist to every file of the desktop
uses the Finder to create a list of file objectsset currentFileName to (the name of currentFile)
gets the name portion of the current filecopy currentFileName to the end of listOfNames
appends the current filename to the end of the listchoose from list listOfNames
displays the list of names in a dialog