if its too loud, turn it down

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!

No comments:

Post a Comment

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