Quick add currently-playing track to an iTunes playlist

Jul 13, '09 07:30:00AM

Contributed by: midget2000x

Oftentimes when I'm listening to music in my library, randomly or in the "recently added" list, I'll think to myself "wow -- that's a great love song, I need to add that to my 'lovesongs' playlist..." But I am too lazy (or busy) to switch to iTunes and drag it over. So I created an AppleScript that's triggered by a keyboard shortcut to add the currently-playing iTunes track to a playlist I specify.

Features:

When invoked with the hotkey, the script will prompt you for the playlist name in a pop-up dialog; type the playlist's name then click OK and you're done. Here's the script:
set currentApp to current application
tell currentApp
activate
set myList to the text returned of (display dialog "Enter playlist to add track to" default answer "")
end tell

--exit if they didn't enter anyting
if the myList is "" then return

--make sure itunes is installed
property okflag : false
tell application "Finder"
if (get name of every process) contains "iTunes" then ¬
 set okflag to true
end tell

if okflag then
set myMessage to ""
tell application "iTunes"
 set oldfi to fixed indexing
 set fixed indexing to true
 set thisTrack to (get location of current track)
 set dbid to (get database ID of current track)

 --see if the playlist exists
 if exists user playlist myList then
  --do nothing for now
 else
  make new user playlist with properties {name:myList}
 end if
 set currentList to playlist myList

 --see if the track exists on the playlist
 set currentIDs to {}
 try
  if exists (track 1 of currentList) then -- if there are some tracks - at least one -- get their ids
   copy (get database ID of every track of currentList) to currentIDs -- list
  end if
 on error errText number errnum
  if errText does not contain "Invalid index." then
   error errstr number errnum
  end if
 end try

 --add the track to playlist or show error
 if currentIDs does not contain dbid then -- if id not already present add the track
  add thisTrack to currentList
  set myMessage to " Track added to playlist " & myList
 else
  set myMessage to " Selected track already on playlist " & myList
 end if
 set fixed indexing to oldfi
end tell
end if

--show our output message
-- Check if Growl is running:
tell application "System Events"
set isRunning to ¬
 (count of ¬
  (every process whose name is "GrowlHelperApp")) > 0
end tell

--Only display growl notifications if Growl is running:
if isRunning = true then

tell application "GrowlHelperApp"
 -- Make a list of all notification types:
 set the allNotificationsList to ¬
  {"Notification 1", "Notification 2"}

 -- Make a list of the default enabled notifications:
 set the enabledNotificationsList to ¬
  {"Notification 1"}

 -- Register the script with Growl
 -- using either "icon of application"
 -- or "icon of file":
 register as application ¬
  "add_to_playlist" all notifications allNotificationsList ¬
  default notifications enabledNotificationsList ¬
  icon of application "Script Editor"

 -- Send a notification:
 notify with name "Notification 1" title "Track add output" description myMessage application name "add_to_playlist"
end tell
else
tell currentApp
 activate
 display dialog myMessage giving up after 1
end tell
end if
[robg adds: I haven't tested this one.]

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090709085206245