Skip to content

Instantly share code, notes, and snippets.

@tiffanygwilson
Forked from iolloyd/Spot the Hijack
Last active December 24, 2023 07:23
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save tiffanygwilson/1914c3b7724e65d736f6 to your computer and use it in GitHub Desktop.
Save tiffanygwilson/1914c3b7724e65d736f6 to your computer and use it in GitHub Desktop.
(* Script to record and tag spotify tracks, by Lloyd Moore *)
(* Modified by Tiffany G. Wilson to resolve audio splitting issues, automate starting/stopping, and add recording customization *)
(* Snippets for controlling Spotify are from Johnny B on tumblr (http://johnnyb.tumblr.com/post/25716608379/spotify-offline-playlist) *)
(* The idea of using delayed tagging/filename updating is from a guest user on pastebin (http://pastebin.com/rHqY0qg9) *)
(* The only thing to change in the script is the output format; you must change the file extension and the recording format to match *)
(* Run this script once a song you want to record is queued (stopped at beginning) or playing *)
(* Running the script will initiate hijacking, recording and audio playback *)
(* To stop script, pause Spotify or wait for album/playlist to end*)
(* To set id3 tags, use application Kid3 (http://sourceforge.net/projects/kid3/) and copy '%{artist} - %{album} - %{track} - %{title}' from file name to Tag 2 *)
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
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_title"
set title tag to track_counter
-- Audio format settings
set recording format to {encoding:MP3, bit rate:256, channels:Stereo, style:VBR}
--set recording format to {encoding:AAC, bit rate:256, 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
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_number to (track number of current track)
repeat until player state is not playing
-- On change of track
if track_name is not equal to (name of current track) then
tell application "Audio Hijack Pro"
tell theSession
set title tag to track_counter
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_number to track number 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
-- 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 ("\"" & folder_path & track_artist & " - " & track_album & " - " & track_number & " - " & track_name & file_extension & "\"")
do shell script ("mv " & old_file & " " & new_file)
end update_filename
-- 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
@tiffanygwilson
Copy link
Author

I cobbled together code from a few different Audio Hijack Pro/Spotify scripts to get one that finally works for me. It splits the files cleanly by waiting until after the session has been split to update the file name. The id3 tags are not set here because that is what I found to be causing the errors in where the files were split. Kid3 (http://sourceforge.net/projects/kid3/) is a great free application that can batch update the id3 tags based on the file name created here.

@Aetles
Copy link

Aetles commented Jul 18, 2014

Tried it and it worked! You could add a note that this script will overwrite any preferences set for Spotify in Audio Hijack Pro.

@Aetles
Copy link

Aetles commented Jul 18, 2014

Another thing, this script renames Spotify in the audio source list in Audio Hijack Pro (with the number of the track). As far as I can tell this made the script fail the second time I was trying to use it, since it couldn't start Spotify. After removing Spotify as audio source and adding it again it worked (maybe a rename would have been sufficient).

@duggi
Copy link

duggi commented Jul 18, 2014

thank you tiffany! after using this, i found that some songs and especially the ads have slashes "/" in their names, which causes a directory write error. i hardened the script to replace "/" with "-"

my fork is over here: https://gist.github.com/duggi/4dfada92d8cd92411bd9

@kuehn-ma
Copy link

unfortunately this script does not work for me... I'm running Yosemite Beta 2 and Audio Hijack 2.11.2.
I pasted this Script in the Script-Editor and saved it as .scpt, then I created a new session picked Spotify as an Source ans chose the Script in the "Open URL/File/Apple Script" box.
After I've hit Hijack I got following error:
"Audio Hijack Pro got an error: Can’t make {encoding:MP3, bit rate:256, channels:Stereo, style:VBR} into type audio recording format."

@snakn
Copy link

snakn commented Oct 5, 2014

I have Maverick and it does not work and shows the error: "Audio Hijack Pro got an error: Can’t make {encoding:MP3, bit rate:256, channels:Stereo, style:VBR} into type audio recording format."
Do you know that is?

@njbee
Copy link

njbee commented Nov 8, 2014

Works well thanks. Is there a way of getting the script to set the year from the track?

@the-arkhive
Copy link

Hey, I've been using your script with AHP 2 for a while and loved it, but I tried to make it work with the new AH 3 by switching over the application name and it did not work at all. I consistently get errors. I am new to Apple Script so I was wondering/hoping someone could take a look at this and maybe update it to the new software.

@jvreeken
Copy link

@thomas-williams
AH 3 is not currently possible as they removed scripting functions for some reason..
http://robjwells.com/post/108761821499/audio-hijack-3-and-scripts

Update: 2015-01-28
In a new post about Audio Hijack 3’s first week, scripting is explicitly noted as an area that might get some attention. http://weblog.rogueamoeba.com/2015/01/28/audio-hijack-3-is-a-hit-and-audio-hijack-3-0-1-is-here/

@Jeanjan
Copy link

Jeanjan commented May 23, 2015

In AH, this error shows up OSAScriptErrorNumberKey = "-1708";
but the tracks are properly recorded.

Thanks for the script.
Spotify : 1.0.5.186.ga9c24d6a
AH : 2.11.4

@weppingpresent
Copy link

Thank you for this, I will save a huge amount of time thanks to you ! :)

I'm a total novice with applescript, and I would like to change the name format of the files.
For the moment, the script renames the files in this order : track_artist, track_album, track_number, track_name. (Not the order that appears in the script)
I would like it to appear like this : track_name, track_artist (in this order)

I tried to mess with the script, deleting the elements I don't need, but all I get are errors or weird file name formats ... Any ideas ? :)

AH 2.10.9
Spotify 1.0.10.107.gd0dfca3a
OS X Yosemite

@weppingpresent
Copy link

My bad, I just needed to delete the elements i don't need in

-- 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 (""" & folder_path & track_artist & " - " & track_album & " - " & track_number & " - " & track_name & file_extension & """)
do shell script ("mv " & old_file & " " & new_file)
end update_filename

@XxNyRxX
Copy link

XxNyRxX commented Sep 12, 2015

Hello, I'm a complete noob at AppleScript, and I haven't been able to figure this out.. I'm not really sure what to change or delete..

I've been hitting Hijack and I get "Audio Hijack (2.9.13) got an error: AppleEvent timed out. Found at character position, length: 0,0."

Anyone have any idea what I'm doing wrong? I'd like to use Kid3 for the ID3s.. It will record for exactly 1 second, then end the recording. The Title and Everything works fine, its just the script isn't recording any more than 1 second. Please give me a hand! Thanks!

@alexandre229
Copy link

One second recording and stop problem solved:
The problem was due to Spotify which was so long to update the "player state" status.
https://gist.github.com/alexandre229/679ee2b0ea2ba6ab82c6

@zamboOlino
Copy link

Hi Guys,
Here an update with correct "tag updates" :)
@alexandre229 thx for the "player state" update.
And thx @ALL for this great job.
https://gist.github.com/ZardAi/71f67d8614a239fd8ad1

@noSumo
Copy link

noSumo commented Oct 6, 2015

Nice work!
@alexandre229, @ZardAi and @ALL

@kal505
Copy link

kal505 commented Oct 23, 2015

hi
Does this script work with the new version of audio hijack (audio Hijack 3)?
Thanks

@xxx31fr
Copy link

xxx31fr commented Nov 23, 2015

Hi
Does your script can work with a web Browser (Chrome for example)
Thanks so much

@himenlinamarbric
Copy link

@PuffDragon
Copy link

+1 @kal505 - Anyone got this working with v3?

@glennpaulmusic
Copy link

Hey has anyone used this recently?
I used to use doug's apple scripts back in the day for itunes organization...
I'm looking to record albums and have their album art and track names get imported automatically.

@jor63
Copy link

jor63 commented Oct 23, 2023

It seems last audio hijack 4 (and no longer PRO) does not support applescript anymore, only javascript. So you must look for something with javascript for both Spotify and Audio Hijack: I haven't found it yet!

@wabiloo
Copy link

wabiloo commented Nov 26, 2023

That's a shame... And there isn't much documentation to help us with the conversion...

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