AppleScript tip: list examples

By Alvin J. Alexander, devdaily.com

A simple AppleScript list can be created like this:

set groceryList to {"eggs", "milk", "bread"}

A lot of times that's all the power you need, but if you want to create a list, add items to the list, then loop through the list, you're going to need a little more power. Here's an AppleScript 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

Some cool things from this example:

  • set filelist to every file of the desktop uses the Finder to create a list of file objects
  • set currentFileName to (the name of currentFile) gets the name portion of the current file
  • copy currentFileName to the end of listOfNames appends the current filename to the end of the list
  • choose from list listOfNames displays the list of names in a dialog

devdaily logo