Getting the selected playlist in iTunes
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.
- The Biblyon Broadsheet
- Like adventurers of old you will delve into forgotten tombs where creatures of myth stalk the darkness. You will search uncharted wilderness for lost knowledge and hidden treasure. Where the hand-scrawled sign warns “beyond here lie dragons,” your stories begin.
More AppleScript
- Stack windows on top of each other
- If you want to stack multiple windows directly on top of each other, it’s easy to do in any well-behaved application, such as Nisus Writer Pro, Safari, Mail, and even older applications like AppleWorks 6 and Microsoft Word X.
- Converting FileMaker to Django
- Appscript and the Django API make it easy to transfer data from Mac OS X scriptable applications into a Django-powered database.
- appscript AppleScript translator
- Those of us who like Applescript but want a solid command-line scripting environment no longer have to muddle through when using appscript. We can write in AppleScript and then translate to appscript.
- Play this song backwards in iTunes
- Using AppleScript and Quicktime, you can play any song backwards. Find out what Fred Schneider was saying on “Detour Thru Your Mind”.
- Create a web browser in AppleScript Studio
- AppleScript Studio is a powerful means of putting a graphic user interface onto AppleScript applications. Interface Builder lets us use any of the simple and complex controls that any other application can use.
- Seven more pages with the topic AppleScript, and other related pages
More iTunes
- Cleaning iTunes track information
- Python and appscript make it easy to modify iTunes track information in batches—if you’re willing to get your hands dirty on the Mac OS X command line.
- Play this song backwards in iTunes
- Using AppleScript and Quicktime, you can play any song backwards. Find out what Fred Schneider was saying on “Detour Thru Your Mind”.
- Using appscript in Python to control GUI applications
- The appscript module for Python lets you control scriptable applications on the command line without having to coordinate your command-line script with your Applescript applications.
- Run a script on inserting an audio CD
- You can run a script every time you insert an audio CD to ensure that iTunes is ready for import.
- A simple iTunes sleep timer
- If you enjoy listening to music for a while before you go to sleep, but don’t want to have iTunes on all night, you can use AppleScript to make iTunes and your Mac do what you want it to do.
- Two more pages with the topic iTunes, and other related pages
