Skip to content

Instantly share code, notes, and snippets.

@zamboOlino
Last active November 9, 2018 17:15
Show Gist options
  • Save zamboOlino/71f67d8614a239fd8ad1 to your computer and use it in GitHub Desktop.
Save zamboOlino/71f67d8614a239fd8ad1 to your computer and use it in GitHub Desktop.
Now you can get the ALBUM ARTWORK -- set track_artwork_path to update_artwork(track_album, track_artwork, folder_path)
set output_folder to (choose folder with prompt "Please choose an output directory")
set folder_path to POSIX path of output_folder
property file_extension : ".mp3"
-- property file_extension : ".m4a" (* If format is changed to AAC *)
property check_delay : 0.1 (* How often to check for a new track *)
property write_delay : 2 (* How long to wait before updating file name *)
property stop_delay : 1 (* How long to wait before updating final file after playback stops *)
set track_counter to 1
tell application "Spotify"
if player state is playing then pause
set track_name_x to (name of current track)
set track_artist_x to (artist of current track)
set track_album_x to (album of current track)
set track_artwork_x to (artwork of current track)
set track_album_artist_x to (album artist of current track)
--set track_year_x to (year of current track)
end tell
tell application "Audio Hijack Pro"
activate
set theSession to my getSession()
tell theSession
-- Recording file settings
set output folder to output_folder
set output name format to "%tag_comment"
set comment tag to track_counter
-- Update Track Session data
set title tag to track_name_x
set artist tag to track_artist_x
set album tag to track_album_x
set album artist tag to track_album_artist_x
--set year tag to track_year_x
-- Audio format settings
--set recording format to {encoding:MP3, bit rate:320, sample rate:44100, channels:Stereo, style:VBR}
--set recording format to {encoding:AAC, bit rate:256, sample rate: 44100, channels:Stereo}
end tell
if hijacked of theSession is false then start hijacking theSession
start recording theSession
end tell
set track_counter to (track_counter + 1)
tell application "Spotify"
-- Start playing Spotify from beginning of current track
set player position to 0
set sound volume to 100
play
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_artwork to (artwork of current track)
set track_number to (track number of current track)
set track_album_artist to (album artist of current track)
--set track_year to (year of current track)
-- Add delay for resolve 1s recording and stop problem
delay 1
repeat until player state is not playing
-- Get new track data
set track_name_x to (name of current track)
set track_artist_x to (artist of current track)
set track_album_x to (album of current track)
set track_artwork_x to (artwork of current track)
set track_album_artist_x to (album artist of current track)
--set track_year_x to (year of current track)
-- On change of track
if track_name is not equal to (name of current track) then
my update_artwork(track_album, track_artwork, folder_path)
tell application "Audio Hijack Pro"
tell theSession
set comment tag to track_counter
set title tag to track_name_x
set artist tag to track_artist_x
set album tag to track_album_x
set album artist tag to track_album_artist_x
--set year tag to track_year_x
split recording
end tell
end tell
delay write_delay
-- Update the file name from track_counter.mp3 to artist - ... - track.mp3
my update_filename(track_counter - 1, track_artist, track_name, track_album, track_number, folder_path)
set track_counter to (track_counter + 1)
-- Get new track data
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_artwork to (artwork of current track)
set track_number to (track number of current track)
set track_album_artist to (album artist of current track)
--set track_year to (year of current track)
end if
delay check_delay
end repeat
-- Stop recording and edit final file name once playback has stopped
delay stop_delay
tell application "Audio Hijack Pro"
stop recording theSession
stop hijacking theSession
end tell
my update_filename(track_counter - 1, track_artist, track_name, track_album, track_number, folder_path)
end tell
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
-- Update file name so it can be tagged using Kid3
on update_filename(track_counter, track_artist, track_name, track_album, track_number, folder_path)
set old_file to ("\"" & folder_path & track_counter & file_extension & "\"")
set new_file to (replace_chars(track_artist, "\"", "") & " - " & replace_chars(track_name, "\"", "") & file_extension & "\"")
set temp_final_file to replace_chars(new_file, "/", "-")
set final_file to ("\"" & folder_path & temp_final_file)
do shell script ("mv " & old_file & " " & final_file)
end update_filename
-- Update Artwork of current track
on update_artwork(track_album, track_artwork, folder_path)
set newPath to ((folder_path as text) & (my replace_chars(track_album, ":", "_")) & ".tiff") as text
try
set fileRef to (open for access newPath with write permission)
write track_artwork to fileRef starting at 0
tell me to close access fileRef
on error m number n
log n
log m
try
tell me to close access fileRef
end try
end try
return newPath
end update_artwork
-- Set Spotify session in Audio Hijack Pro
on getSession()
tell application "Audio Hijack Pro"
set sessionName to "Spotify"
try
set theSession to (first item of (every session whose name is sessionName))
theSession is not null
on error
set theSession to (make new application session at end of sessions)
set name of theSession to sessionName
end try
end tell
return theSession
end getSession
@takethefake
Copy link

Hey i have an error under OS X 10.11.1, Audio Hijack Pro 2.10.9, Spotify 1.0.14.124

error "„Audio Hijack Pro“ hat einen Fehler erhalten: Process Source could not find the process #1947 in the attachable list" number 8

@zamboOlino
Copy link
Author

You have to use spotify "0.9.4" or less :)
Or You have to look at your App-Name, it has to be the Same as in the Script-Editor.

  • tell application "Audio Hijack Pro" <-
    If you start the script, he needs to ask after the needed Applikation if he can't find them

@Hugocorp974
Copy link

Love it !! thanks.
Don't have any album artwork in the folder of my ripped song ? How it works ?

@zamboOlino
Copy link
Author

Update 👍

@SantoshSrinivas79
Copy link

I am getting the following error: error "The variable theSessiontra is not defined." number -2753 from "theSessiontra"

I am using Audio Hijack Pro 2.11.3 and Spotify 1.0.16.104.

Can you please let me know if I need to do any setup tasks on Audio Hijack Pro or should I simply run the script?

@zamboOlino
Copy link
Author

@AspireToCodeBetter SORRY!!! It was a failure in my code :) now, it do his job.

@letterale
Copy link

the script works great, it's beautiful.
I have a question: if I wanted to add the track number at the beginning of the file name, as I do?
Thanks for a job well done!

@letterale
Copy link

I fixed by changing this line:
set final_file to (""" & folder_path & temp_final_file)

in this way:
set final_file to (""" & folder_path & track_counter & "-" & temp_final_file)

My solution is perhaps not as elegant as the original code, but I'm happy;-)

@iceace86
Copy link

Argh I'm not able to run it, there is something I'm doing wrong. I got Audio Hijack Pro 2.10.7 and Spotify 1.0.15.133 (I tried using 0.9.4 but it autoupdate) in El capitan 10.11.1.
I've set an input from application Spotify in AHP using your script, then I open spotify and launch a playlist and after that I press Hijack on AHP, it says that AHP have to quit and relaunch Spotify, I choose an output directory and then.... AHP freeze and I receive this error: Spotify got an error:
"AppleEvent timed out. Found at character position, length: 546, 7"

@zamboOlino
Copy link
Author

Your solution is good, i use all the time 0.9.4.xxx and El Capitan.
I do it like:

  • launch audio hijack (2.11.5)
  • then press "hijack"
  • this will launch spotify (0.9.4.178)
  • then choose your title or album or playlist and start playing your title
  • then start this applescript and choose your directory
  • for it this works very fine :)

@zamboOlino
Copy link
Author

your have to block this spotify autoupdate
-> LOOK -- http://www.wired.co.uk/news/archive/2015-03/12/how-to-downgrade-spotify

@mynameisedo
Copy link

Hey! First of all thanks for this script, I love it.
Is there anything for Spotify v1.0.1x? Because using the oldest version I can't see the top traks etc.
Thanks :)

@zamboOlino
Copy link
Author

I check it out and hope to make an update :)

@iceace86
Copy link

I had only now the time to test again your script, following your instructions... and it works!
My fault were using a spotify newer version and adding the script in audiohijack (instead should be used "standalone", after everything)
Only one thing.. is it normal that Album Cover are saved as .tiff images in the same folder as audio files?
Thanks!

@iceace86
Copy link

Another question: after a while, spotify disconnects and so, the script end to run. Is it because I'm not using premium spotify or simply because after x time it disconnects?

@zamboOlino
Copy link
Author

I have the same problem.
But till now i cant solved it i don't finde the problem, i think it comes from spotifiy if the app switch the track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment