if its too loud, turn it down

Sunday, June 14, 2009

Quick add currently playing track to an iTunes playlist

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.

  • Prompts you for the playlist name
  • Will create the playlist if it doesn't exist
  • Will not add the track if it's already on the designated playlist
  • Designed to be executed with a hotkey "trigger" event (keyboard shortcut) with Quicksilver or some other shortcut program. See how-to on adding keyboard hotkeys with Quicksilver
  • Unobtrusively shows the result with a growl message
When invoked with the hotkey, the script will prompt you for the playlist name like this:

Quick Add Prompt

Here is the applescript (updated 7/13/09 with "itunes_is_running" subroutine suggested by Doug Adams):

set currentApp to current application
tell currentApp
 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 running
set okflag to my itunes_is_running()

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
   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 {}
   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
   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
 tell currentApp
  display dialog myMessage giving up after 1
 end tell
end if

on itunes_is_running()
 tell application "System Events" to return (exists process "iTunes")
end itunes_is_running

--this is commented out because I found this to be too slow
--put the focus back on the app we were using before we called this script with quicksilver
--quicksilver became the frontmost app because it's what executed this script
--tell application "System Events"
-- keystroke tab using (command down)
-- set app_name to name of the first process whose frontmost is true
-- keystroke tab using (command down)
--return app_name
--end tell

1 comment: