Skip to content

Instantly share code, notes, and snippets.

@andrewgribben
Last active March 23, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgribben/9ad33f7c670f0b827416dbc94259d90b to your computer and use it in GitHub Desktop.
Save andrewgribben/9ad33f7c670f0b827416dbc94259d90b to your computer and use it in GitHub Desktop.
Record tagged tracks from iTunes using Audio Hijack Pro
property update_delay : 1.0E-3
property output_folder : "/Users/<<ENTERUSERNAME>>/Music/"
set folder_path to output_folder
property nameOfTrack : ""
property nameOfAlbum : ""
property nameOfArtist : ""
property file_extension : ".mp3"
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 "iTunes"
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
tell application "Finder"
set itunes_path to "/Applications/iTunes.app"
end tell
set targeted application to itunes_path
set output folder to output_folder
set track number tag to track_counter
set output name format to "%tag_trackcount"
set track count 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 "iTunes"
-- Start playing iTunes 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)
tell application "Audio Hijack Pro"
tell theSession
set title tag to track_name
set artist tag to track_artist
set album tag to track_album
end tell
end tell
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 track count tag to track_counter
set title tag to track_name
set artist tag to track_artist
set album tag to track_album
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 this_quote to "\""
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 " & quoted form of old_file & " " & quoted form of new_file)
end update_filename
-- Set Spotify session in Audio Hijack Pro
on getSession()
tell application "Audio Hijack Pro"
set sessionName to "iTunes"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment