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)
   return
  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
  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
  
  --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)
   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 newCount to newCount + 1
    else
     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
else
 --itunes not running, quit
 set myMessage to "iTunes is not running"
 set myReturn to my growlMessage(myMessage)
 return
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
 else
  tell currentApp
   activate
   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.

Friday, August 7, 2009

Applescript to add 1 to play count of selected tracks

This script solves an issue related to recorded radio programs and iTunes Smart Playlists. Let's say for example you ripped an episode of "Crap from the Past", and you've set up these episodes to be automatically be added to a Smart Playlist called "programs". This Smart Playlist has a rule to exclude programs with a play count of "1" or greater (i.e. programs you've already listened to), to keep the list clean.

BUT...you don't like this particular episode. Maybe Ron "Boogiemonster" Gerber chose a theme like "Charity Songs from the 80s" or something...and you don't want it appearing in your Smart Playlist. Podcasts have a handy right-click option of "Mark as Not New" which solves the issue, but regular tracks do not. This script forces the play count to 1 so that it can be excluded from your Smart Playlist. Niche issue, I know, but a real concern for me!

Doug at Doug's Appplescripts for iTunes has already addressed this issue with the very nice "Add or Subtract Play Count", which is great...but it prompts you for a number. Since I'm all into efficiency and I only ever need to increase the play count by 1, I've hacked the script so that there is no prompt. I've also added error/success output with Growl because it's less obtrusive.
set myMessage to ""
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)
 else
  --loop thru selected tracks and add 1 to their play count
  set sel to selection
  repeat with aTrack in sel
   -- skip tracks without played count property
   if aTrack's class is file track or aTrack's class is URL track then
    tell aTrack
     set curPlayCount to (get played count)
     set played count to curPlayCount + 1
    end tell
   end if
  end repeat
  
  --success message
  set myMessage to "Added 1 to play count of selected tracks"
  set myReturn to my growlMessage(myMessage)
 end if
 
end tell

--subroutine showing messages in growl (preferably)
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_play_count" all notifications allNotificationsList ¬
    default notifications enabledNotificationsList ¬
    icon of application "Script Editor"
   
   -- Send a notification:
   notify with name "Notification 1" title "Add Play Count Message" description myMessage application name "add_play_count"
  end tell
 else
  tell currentApp
   activate
   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
Name the file "add_play_count.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. I use this often, so I set up a Quicksilver keyboard hotkey trigger for it. Enjoy!