Wednesday 27 March 2013

Record Spotify Tracks for Offline listening (Mac)

I recently stumbled upon a YouTube video that showed how to record Spotify songs on your Mac for offline listening. The easiest way is to use an application called 'Audio Hijack Pro' records audio natively from different sources/applications.
After installing this application, you can record Spotify radio as a single track with a press of a button and then manually split the tracks using any audio splitter/ tool.
I wanted to automate this process and a few more clicks on Google I came across a non-working script which gave me some direction into how this can be achieved.
The script attached below will fire off Spotify and 'Audio Hijack Pro' and when the user selects to record, it will split the songs adding some basic ID3 tags along the way. Have fun.

 (* Script to record and tag spotify tracks, by Lloyd Moore *)  
 (* Lloyd's script didn't work for me initially but after few changes it worked! *)  

 tell application "Spotify"  
      repeat  
           try  
                play  
                set currentTrack to (current track)  
                set trackName to (name of current track)  
                tell application "Audio Hijack Pro"  
                     set theSession to my getSession()  
                end tell  
                set loop to true  
                repeat while loop  
                     if trackName is not equal to name of current track then  
                          try  
                               set trackName to name of current track  
                               set trackArtist to artist of current track  
                               set trackAlbum to album of current track  
                               my recordTrack(theSession, trackName, trackArtist, trackAlbum)  
                          on error  
                               set loop to false  
                          end try  
                     end if  
                     delay 1  
                end repeat  
           end try  
           delay 1  
      end repeat  
 end tell  
 on getSession()  
      tell application "Audio Hijack Pro"  
           set sessionName to "spotifySession"  
           try  
                set theSession to first session whose name is sessionName --By name  
           on error  
                set theSession to (make new application session at end of sessions)  
           end try  
           set name of theSession to sessionName  
      end tell  
      return theSession  
 end getSession  
 on recordTrack(theSession, trackName, trackArtist, trackAlbum)  
      tell application "Audio Hijack Pro"  
           tell theSession  
                split recording  
                set output folder to "~/Desktop"  
                set output name format to "%tag_title -- %tag_artist"  
                set title tag to trackName  
                set artist tag to trackArtist  
                set album tag to trackAlbum  
           end tell  
      end tell  
 end recordTrack