Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Record Spotify using Hijack Pro
- -- script version: 1.0
- -- author : rbg
- -- Global variables
- global track_name
- global track_artist
- global track_album
- global track_nr
- global track_disc
- global track_year
- global track_artwork
- global track
- global changed
- global album_changed
- global spotify_up
- global spotify_ad
- global spotify_stopped
- global hijack_recording
- global hijack_dir
- global hijack_base_dir
- global posix_hijack_dir
- global state
- global script_just_started
- -- configuration:
- property polling_interval : 1
- property create_folders : false
- property save_artwork : false
- -- Function to check if an application is running
- on isRunning(appName)
- tell application "System Events" to (name of processes) contains appName
- end isRunning
- -- Init
- on run
- set script_just_started to true
- set changed to false
- set album_changed to false
- set spotify_ad to false
- set spotify_stopped to missing value
- set hijack_recording to false
- set hijack_dir to missing value
- set hijack_base_dir to hijack_dir
- set posix_hijack_dir to missing value
- set track_name to missing value
- set track_artist to missing value
- set track_album to missing value
- set track_nr to missing value
- set track_disc to missing value
- set track_year to missing value
- set track_artwork to missing value
- -- Check if Spotify and HiJack are running
- if isRunning("Audio Hijack Pro") then
- -- TODO Run Audio Hijack first
- log "Audio Hijack Pro is running."
- tell application "Audio Hijack Pro"
- --start hijacking
- try
- set Spotify to first session whose hijacked is true
- on error errorMsg
- log "ERROR: No Hijack Pro session is hijacking. Exiting."
- return "ERROR: No Hijack Pro session is hijacking. Exiting."
- end try
- tell Spotify
- set hijack_dir to output folder
- set hijack_base_dir to output folder
- set posix_hijack_dir to POSIX file hijack_base_dir
- log "Hijack Pro is writing to: " & hijack_base_dir
- end tell
- end tell
- end if
- if isRunning("Spotify") then
- log "Spotify is running."
- tell application "Spotify"
- if sound volume is less than 100 then
- log "Setting volume in Spotify"
- set sound volume to 100
- end if
- if shuffling is true then
- log "WARNING: shuffle is enabled."
- end if
- if repeating is true then
- log "Disaling repeating"
- set repeating to false
- end if
- end tell
- end if
- repeat
- idle
- delay polling_interval
- end repeat
- end run
- -- Infinite Loop One
- on idle
- -- Check if Spotify and Skype are running
- if isRunning("Audio Hijack Pro") then
- if isRunning("Spotify") then
- --log "checking …"
- -- Get track from Spotify
- tell application "Spotify"
- if player state is stopped then
- set spotify_stopped to true
- else
- set spotify_stopped to false
- end if
- -- sanity check
- if player state is not stopped and class of current track is track then
- set album_folder to artist of current track & " - " & album of current track
- -- is current track Spotify ad?
- if artist of current track is "Spotify" then
- set spotify_ad to true
- else
- set spotify_ad to false
- end if
- if album_changed is true and spotify_ad is false then
- -- save artwork to file in next polling step
- if save_artwork then
- set album_art_file to hijack_dir & "/albumArt.tiff"
- set pos_file to POSIX file album_art_file
- set fileRef to (open for access pos_file with write permission)
- try
- write track_artwork to fileRef
- close access fileRef
- on error errorMsg
- try
- close access fileRef
- end try
- error errorMsg
- end try
- end if
- end if
- -- check if album has changed:
- if track_artist is not artist of current track or track_album is not album of current track then
- log "Album changed to " & album_folder
- set album_changed to true
- -- need to create new folder?
- if create_folders and spotify_ad is not true then
- log "Creating new folder " & album_folder & " in " & hijack_base_dir
- tell application "Finder" to make new folder at folder posix_hijack_dir with properties {name:album_folder as text}
- set hijack_dir to hijack_base_dir & "/" & album_folder
- end if
- else
- set album_changed to false
- end if
- -- Check if track has changed:
- if track_name is not name of current track or track_artist is not artist of current track then
- set changed to true
- set track_name to name of current track
- set track_artist to artist of current track
- set track_album to album of current track
- set track_nr to track number of current track
- set track_disc to disc number of current track
- --not supported by hijack:
- set track_artwork to artwork of current track
- --not supported by spotify: set track_year to year of current track
- log "track changed to [" & track_nr & "] " & track_artist & " - " & track_name
- set surl to id of current track
- log surl
- else
- set changed to false
- end if
- else
- -- should i stop hijack?
- log "Spotify playback stopped."
- set state to "stopped"
- end if
- end tell
- -- Send track info to Hijack if track has changed
- if changed then
- tell application "Audio Hijack Pro"
- set Spotify to first session whose hijacked is true
- tell Spotify
- set title tag to track_name
- set artist tag to track_artist
- set album tag to track_album
- set track number tag to track_nr
- set disc number tag to track_disc
- set year tag to track_year
- -- not supported: set artwork to track_artwork
- if create_folders then
- set output folder to hijack_dir
- end if
- end tell
- -- stop recording on Spotify ad
- if spotify_ad is true or spotify_stopped is true then
- log "Stopping recording in HiJack Pro"
- stop recording Spotify
- set hijack_recording to false
- else
- -- resume recording after ad
- if hijack_recording is false then
- log "Starting/resuming recording in HiJack Pro"
- start recording Spotify
- set hijack_recording to true
- else
- -- do not split in 1st loop run
- if script_just_started is not true then
- log "Sending split command to Hijack Pro."
- split recording Spotify
- end if
- end if
- end if
- end tell
- end if
- set spotify_up to true
- --log "spotify_up = true"
- -- If Spotify was just quit:
- else if spotify_up then
- -- do anything?
- set changed to false
- set spotify_up to false
- log "spotify_up = false"
- end if
- end if
- set script_just_started to false
- return polling_interval
- end idle
- -- Install exit handler
- on quit
- -- restore output folder
- tell application "Audio Hijack Pro"
- set Spotify to first session whose hijacked is true
- tell Spotify
- set output folder to hijack_base_dir
- end tell
- end tell
- continue quit
- end quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement