Advertisement
Guest User

spotify hijack

a guest
Jul 25th, 2012
8,578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Record Spotify using Hijack Pro
  2. -- script version: 1.0
  3. -- author :        rbg
  4.  
  5. -- Global variables
  6. global track_name
  7. global track_artist
  8. global track_album
  9. global track_nr
  10. global track_disc
  11. global track_year
  12. global track_artwork
  13. global track
  14. global changed
  15. global album_changed
  16. global spotify_up
  17. global spotify_ad
  18. global spotify_stopped
  19. global hijack_recording
  20. global hijack_dir
  21. global hijack_base_dir
  22. global posix_hijack_dir
  23. global state
  24. global script_just_started
  25.  
  26. -- configuration:
  27. property polling_interval : 1
  28. property create_folders : false
  29. property save_artwork : false
  30.  
  31. -- Function to check if an application is running
  32. on isRunning(appName)
  33.     tell application "System Events" to (name of processes) contains appName
  34. end isRunning
  35.  
  36. -- Init
  37. on run
  38.     set script_just_started to true
  39.     set changed to false
  40.     set album_changed to false
  41.     set spotify_ad to false
  42.     set spotify_stopped to missing value
  43.     set hijack_recording to false
  44.     set hijack_dir to missing value
  45.     set hijack_base_dir to hijack_dir
  46.     set posix_hijack_dir to missing value
  47.     set track_name to missing value
  48.     set track_artist to missing value
  49.     set track_album to missing value
  50.     set track_nr to missing value
  51.     set track_disc to missing value
  52.     set track_year to missing value
  53.     set track_artwork to missing value
  54.    
  55.     -- Check if Spotify and HiJack are running
  56.     if isRunning("Audio Hijack Pro") then
  57.         -- TODO Run Audio Hijack first
  58.         log "Audio Hijack Pro is running."
  59.         tell application "Audio Hijack Pro"
  60.             --start hijacking
  61.             try
  62.                 set Spotify to first session whose hijacked is true
  63.             on error errorMsg
  64.                 log "ERROR: No Hijack Pro session is hijacking. Exiting."
  65.                 return "ERROR: No Hijack Pro session is hijacking. Exiting."
  66.             end try
  67.            
  68.             tell Spotify
  69.                 set hijack_dir to output folder
  70.                 set hijack_base_dir to output folder
  71.                 set posix_hijack_dir to POSIX file hijack_base_dir
  72.                 log "Hijack Pro is writing to: " & hijack_base_dir
  73.             end tell
  74.         end tell
  75.     end if
  76.     if isRunning("Spotify") then
  77.         log "Spotify is running."
  78.         tell application "Spotify"
  79.             if sound volume is less than 100 then
  80.                 log "Setting volume in Spotify"
  81.                 set sound volume to 100
  82.             end if
  83.             if shuffling is true then
  84.                 log "WARNING: shuffle is enabled."
  85.             end if
  86.             if repeating is true then
  87.                 log "Disaling repeating"
  88.                 set repeating to false
  89.             end if
  90.         end tell
  91.     end if
  92.    
  93.     repeat
  94.         idle
  95.         delay polling_interval
  96.     end repeat
  97. end run
  98.  
  99. -- Infinite Loop One
  100. on idle
  101.     -- Check if Spotify and Skype are running
  102.     if isRunning("Audio Hijack Pro") then
  103.         if isRunning("Spotify") then
  104.             --log "checking …"
  105.             -- Get track from Spotify
  106.             tell application "Spotify"
  107.                 if player state is stopped then
  108.                     set spotify_stopped to true
  109.                 else
  110.                     set spotify_stopped to false
  111.                 end if
  112.                
  113.                 -- sanity check
  114.                 if player state is not stopped and class of current track is track then
  115.                     set album_folder to artist of current track & " - " & album of current track
  116.                    
  117.                     -- is current track Spotify ad?
  118.                     if artist of current track is "Spotify" then
  119.                         set spotify_ad to true
  120.                     else
  121.                         set spotify_ad to false
  122.                     end if
  123.                    
  124.                     if album_changed is true and spotify_ad is false then
  125.                         -- save artwork to file in next polling step
  126.                         if save_artwork then
  127.                             set album_art_file to hijack_dir & "/albumArt.tiff"
  128.                             set pos_file to POSIX file album_art_file
  129.                             set fileRef to (open for access pos_file with write permission)
  130.                             try
  131.                                 write track_artwork to fileRef
  132.                                 close access fileRef
  133.                             on error errorMsg
  134.                                 try
  135.                                     close access fileRef
  136.                                 end try
  137.                                 error errorMsg
  138.                             end try
  139.                         end if
  140.                     end if
  141.                    
  142.                     -- check if album has changed:
  143.                     if track_artist is not artist of current track or track_album is not album of current track then
  144.                         log "Album changed to " & album_folder
  145.                         set album_changed to true
  146.                        
  147.                         -- need to create new folder?
  148.                         if create_folders and spotify_ad is not true then
  149.                            
  150.                             log "Creating new folder " & album_folder & " in " & hijack_base_dir
  151.                             tell application "Finder" to make new folder at folder posix_hijack_dir with properties {name:album_folder as text}
  152.                             set hijack_dir to hijack_base_dir & "/" & album_folder
  153.                         end if
  154.                     else
  155.                         set album_changed to false
  156.                     end if
  157.                    
  158.                     -- Check if track has changed:
  159.                     if track_name is not name of current track or track_artist is not artist of current track then
  160.                         set changed to true
  161.                         set track_name to name of current track
  162.                         set track_artist to artist of current track
  163.                         set track_album to album of current track
  164.                         set track_nr to track number of current track
  165.                         set track_disc to disc number of current track
  166.                         --not supported by hijack:
  167.                         set track_artwork to artwork of current track
  168.                        
  169.                         --not supported by spotify: set track_year to year of current track
  170.                         log "track changed to [" & track_nr & "] " & track_artist & " - " & track_name
  171.                         set surl to id of current track
  172.                         log surl
  173.                        
  174.                     else
  175.                         set changed to false
  176.                     end if
  177.                 else
  178.                     -- should i stop hijack?
  179.                     log "Spotify playback stopped."
  180.                     set state to "stopped"
  181.                 end if
  182.             end tell
  183.            
  184.             -- Send track info to Hijack if track has changed
  185.             if changed then
  186.                 tell application "Audio Hijack Pro"
  187.                     set Spotify to first session whose hijacked is true
  188.                    
  189.                     tell Spotify
  190.                         set title tag to track_name
  191.                         set artist tag to track_artist
  192.                         set album tag to track_album
  193.                         set track number tag to track_nr
  194.                         set disc number tag to track_disc
  195.                         set year tag to track_year
  196.                         -- not supported: set artwork to track_artwork
  197.                        
  198.                        
  199.                         if create_folders then
  200.                             set output folder to hijack_dir
  201.                         end if
  202.                        
  203.                     end tell
  204.                    
  205.                     -- stop recording on Spotify ad
  206.                     if spotify_ad is true or spotify_stopped is true then
  207.                         log "Stopping recording in HiJack Pro"
  208.                         stop recording Spotify
  209.                         set hijack_recording to false
  210.                     else
  211.                         -- resume recording after ad
  212.                         if hijack_recording is false then
  213.                             log "Starting/resuming recording in HiJack Pro"
  214.                             start recording Spotify
  215.                             set hijack_recording to true
  216.                         else
  217.                             -- do not split in 1st loop run
  218.                             if script_just_started is not true then
  219.                                 log "Sending split command to Hijack Pro."
  220.                                 split recording Spotify
  221.                             end if
  222.                         end if
  223.                     end if
  224.                    
  225.                 end tell
  226.             end if
  227.             set spotify_up to true
  228.             --log "spotify_up = true"
  229.            
  230.             -- If Spotify was just quit:
  231.         else if spotify_up then
  232.             -- do anything?
  233.             set changed to false
  234.             set spotify_up to false
  235.             log "spotify_up = false"
  236.         end if
  237.     end if
  238.     set script_just_started to false
  239.     return polling_interval
  240. end idle
  241.  
  242. -- Install exit handler
  243. on quit
  244.     -- restore output folder
  245.     tell application "Audio Hijack Pro"
  246.         set Spotify to first session whose hijacked is true
  247.         tell Spotify
  248.             set output folder to hijack_base_dir
  249.         end tell
  250.     end tell
  251.    
  252.     continue quit
  253. end quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement