Script to add current playing track to iTunes Playlist

Sharing and discussing custom actions for LaunchBar
Post Reply
sabdal
Rank 1
Rank 1
Posts: 33
Joined: Wed Jun 18, 2008 4:49 pm

Script to add current playing track to iTunes Playlist

Post by sabdal » Mon Oct 15, 2012 10:10 pm

I frequently like to add tracks to playlists while I'm listing to all my old music I have heard in a while (I have a smart list that plays the song that has not been played for the longest, first). When I like a song, I use this script to add it to the list of my choice. I saved it as an action and then invoke from launchbar.

I got the script here: http://hints.macworld.com/article.php?s ... 9085206245

There are a few deficiencies:

I deleted the growl part because it was dated and didn't work
You have to know the name of the playlist.

If anybody cares to improve it, please do! I know next to nothing about applescript...just enough to figure out how to delete the growl portion. The original script (URL Above) has the growl portion if that is helpful to anybody.

Thanks....

Code: Select all

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


Post Reply