if its too loud, turn it down

Wednesday, August 19, 2009

Applescript to add selected tracks to a playlist

I created this because I am clumsy with the pointer. After selecting a large number of tracks in iTunes with command-click I inevitably accidentally click and lose my selections. With this, I just hit a hotkey and get prompted for the playlist I want to add the selections to. If the list doesn't exist, it will be created.

This is just one more way OF MANY to add tracks to a playlist. I am a huge efficiency guy, so I set this script to be run with a Quicksilver keyboard hotkey trigger.

Here's the script:
--make sure itunes is running
set itunesOK to my itunes_is_running()
set myMessage to ""

if itunesOK then
 tell application "iTunes"
  -- if no tracks selected, exit with message
  if selection is {} then
   set myMessage to "No tracks selected"
   set myReturn to my growlMessage(myMessage)
  end if
  --display prompt for playlist
  set myList to the text returned of (display dialog "Enter playlist to add selected tracks to" default answer "")
  --exit if they didn't enter anyting
  if the myList is "" then return
  set oldfi to fixed indexing
  set fixed indexing to true
  set newCount to 0
  set existsCount to 0
  set deadCount to 0
  --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
  --loop thru the selected tracks
  set sel to selection
  repeat with aTrack in sel
   set thisTrack to (get location of aTrack)
   set dbid to (get database ID of aTrack)
    --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 newCount to newCount + 1
     set existsCount to existsCount + 1
    end if
   on error
    set deadCount to deadCount + 1
   end try
  end repeat
  set myMessage to "Added " & newCount & " track(s) to " & myList & "."
  if existsCount > 0 then
   set myMessage to myMessage & " There were " & existsCount & " selected track(s) already on it. "
  end if
  if deadCount > 0 then
   set myMessage to myMessage & " There were " & deadCount & " selected track(s) that were not added due to error. "
  end if
  set myReturn to my growlMessage(myMessage)
  set fixed indexing to oldfi
 end tell
 --itunes not running, quit
 set myMessage to "iTunes is not running"
 set myReturn to my growlMessage(myMessage)
end if

--subroutine showing messages in growl (preferably)
--and if no growl, default dialog with timeout
to growlMessage(myMessage)
 --show our output message
 -- Check if Growl is running:
 set isRunning to my growl_is_running()
 --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_selected_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_selected_to_playlist"
  end tell
  tell currentApp
   display dialog myMessage giving up after 1
  end tell
 end if
end growlMessage

--sub checks if growl is running
on growl_is_running()
 tell application "System Events" to return (exists process "GrowlHelperApp")
end growl_is_running

--checks if itunes is running
on itunes_is_running()
 tell application "System Events" to return (exists process "iTunes")
end itunes_is_running
Name the file "add_selected_to_playlist.scpt" and save as a "Script" in /Users/YOU/Library/iTunes/Scripts/ and it will be available from the iTunes "Script" (script icon in the menu bar) pull-down menu.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.