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
- Find all parent mailboxes in macOS Mail
- The macOS Mail app seems to want to hide the existence of mailboxes and any sense of hierarchical storage. These two AppleScripts will help you find the full path to a selected message and open the message’s mailbox.
- JXA and AppleScript compared via HyperCard
- How does JXA compare to the AppleScript solution for screenshotting every card in HyperCardPreview?
- Using version control with AppleScripts
- AppleScripts aren’t stored as text, which makes it impossible to track changes in AppleScript files using version control software such as Mercurial or Git.
- Save clipboard text to the current folder
- Use the Finder toolbar to save text on the current clipboard directly to a file in the folder that Finder window is displaying.
- Adding parenthetical asides to photograph titles on macOS
- Use Applescript to append a parenthetical to the titles of all selected photographs in Photos on macOS.
- 17 more pages with the topic AppleScript, and other related pages
More iTunes
- Catalina vs. Mojave for Scripters
- More detail about the issues I ran into updating the scripts from 42 Astounding Scripts for Catalina.
- Catalina: iTunes Library XML
- What does Catalina mean for 42 Astounding Scripts?
- A present for Palm
- Palm needs a little help understanding XML.
- 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”.
- Five more pages with the topic iTunes, and other related pages