Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Getting the selected playlist in iTunes

Jerry Stratton, September 30, 2007

I’m providing a soundtrack for an upcoming Gods & Monsters adventure, and I’m keeping the soundtrack up-to-date in iTunes, of course. During the editing process I'll be needing to copy it every time I change it, in a specific format, into the adventure’s word processing document. This is a perfect application for AppleScript and the Script Menu.

This AppleScript script will copy either the currently selected tracks, or all tracks in the currently-selected playlist, to a list of the format “song name (artist name/album name)”. The list will be in the clipboard ready to be pasted into any document.

[toggle code]

  • copy {} to songlist
  • tell application "iTunes"
    • --if some tracks are selected, use only those tracks
    • --otherwise, use all tracks in the selected playlist
    • if selection is {} then
      • copy view of front window to selectedLibrary
      • copy every track of selectedLibrary to selectedTracks
    • else
      • copy selection to selectedTracks
    • end if
    • repeat with trackinfo in selectedTracks
      • copy {name, artist, album} of trackinfo to {songname, artistname, albumname}
      • set the end of songlist to songname & " (" & artistname & "/" & albumname & ")"
    • end repeat
  • end tell
  • --combine the list into a one-per-line string
  • if songlist is not {} then
    • set text item delimiters to return
    • set the clipboard to songlist as string
  • end if

This list will look like this:

The Wind Cries Mary (The Jimi Hendrix Experience/Are You Experienced?)
Stoned Soul Picnic (Laura Nyro/Eli and the Thirteenth Confession)
Pride Of Man (Quicksilver Messenger Service/Classic Masters)
I Can See For Miles (The Who/The Wh0 Sell Out)
The Weight (The Band/The Last Waltz)

The most time-consuming part of this (and the reason I’m putting this up for others to hopefully more easily find) was getting the currently selected playlist rather than the currently playing playlist. The current selection only contains tracks. You can get the playlist that the current selection is in if there is a current selection, but obviously not if there is no track currently selected.

It looks like the best way to get the currently selected playlist is through the view of the topmost window. This should always be the currently selected playlist.

The OS X scripts menu

You can turn on the Script Menu by using the AppleScript Utility in the AppleScript folder in your Applications folder.

Put this in the folder that opens when you choose “Open iTunes Scripts Folder” from the scripts menu. It will appear every time you’re in iTunes.

  1. <- Django and NULL
  2. Django Taskpaper ->