You are on page 1of 3

-- Usage

-- 1. Edit the settings (see below). This step is NECESSARY the first time you
use it!
-- 2. Run this script (this should open Audio Hijack Pro and Spotify)
-- 3. Play the album / playlist in Spotify
--
-- NOTE: the script assumes that each track is played entirely. You CANNOT skip
tracks in Spotify.
-- If you do so the script will get out of sync and the resulting files contain
partial or multiple songs.
--
-- You need to have "atomicparsley" installed on your system. You can install the
application with
-- homebrew using "brew install atomicparsley" (see http://brew.sh/)
--
-- The recording settings for Audio Hijack Pro NEED to be defined here, they CANNOT
be changed
-- withing the application itself.
-- Every time the script is executed, the settings defined here are applied to
Audio Hijack Pro,
-- customizations in the application are overwritten.

property output_folder : "/Users/USERNAME/Music/AudioHijack"


property atomicparsley_path : "/usr/local/bin/atomicparsley"

-- In order to change the output format, you need to find the line in this script
which looks like
-- "set recording format of spotify_session to {...}"
-- If you change the output format, make sure to update the file_extension property
to match the codec!
property file_extension : ".m4a"

on format_filename(track_artist, track_album, track_name, track_number)


return "" & track_artist & " - " & track_album & " - " & track_number & " - "
& track_name & file_extension
end format_filename

-- Internal properties (no need to change those)

property initial_poll_delay : 0.1


property metadata_save_delay : 2

tell application "Audio Hijack Pro"


activate
-- Find the "Spotify" session or create it if it doesn't exist.
try
set spotify_session to first session whose name is "Spotify"
on error number -1719
tell application "Finder"
set spotify_path to POSIX path of (application file id "spty" as
alias)
end tell
set spotify_session to make new application session at end of sessions
set name of spotify_session to "Spotify"
set targeted application of spotify_session to spotify_path
end try

-- Define / overwrite the settings of the "Spotify" session in Audio Hijack


Pro.
-- This script only works correctly if the settings are as specified here.
set output folder of spotify_session to output_folder
set output name format of spotify_session to "%tag_title"

-- Audio format settings


set recording format of spotify_session to {encoding:AAC, bit rate:256,
channels:Stereo}
-- set recording format of spotify_session to {encoding:MP3, bit rate:320,
channels:Stereo, style:VBR}

-- (Re-)start hijacking and recording on the spotify session


if hijacked of spotify_session is true then
stop hijacking spotify_session
end if
start hijacking spotify_session relaunch yes
end tell

tell application "Spotify"


if not running then activate
if player state is playing then pause

if repeating then display dialog "Note that repeating is enabled in Spotify!


You can still disable it."

repeat until player state is playing


delay initial_poll_delay
end repeat

set track_counter to 0
set previous_metadata to {}

set track_counter to (track_counter + 1)


my start_next_track(spotify_session, track_counter)
delay metadata_save_delay

repeat until player state is not playing


set previous_metadata to my save_metadata(previous_metadata,
track_counter - 1, spotify_session)

set track_length to (duration of current track)


set current_time to player position
delay track_length - current_time

set track_counter to (track_counter + 1)


my start_next_track(spotify_session, track_counter)
delay metadata_save_delay
end repeat

my stop_recording(spotify_session)
my save_metadata(previous_metadata, track_counter - 1, spotify_session)

do shell script "rm " & my temp_filename(track_counter)


end tell

on start_next_track(spotify_session, track_counter)
tell application "Audio Hijack Pro"
stop recording spotify_session
set title tag of spotify_session to track_counter
start recording spotify_session
end tell
end start_next_track

on stop_recording(spotify_session)
tell application "Audio Hijack Pro"
stop recording spotify_session
end tell
end stop_recording

on save_metadata(previous_metadata, track_counter, spotify_session)


tell application "Spotify"
if previous_metadata is not {} then
set track_artist to md_artist of previous_metadata
set track_album to md_album of previous_metadata
set track_name to md_name of previous_metadata
set track_number to md_number of previous_metadata

set new_filename to my format_filename(track_artist, track_album,


track_name, track_number)
-- eliminate special characters from file name
-- TODO: keep brackets [], also other symbols
set new_filename to do shell script ("echo \"" & new_filename &
"\" | sed 's/[^a-zA-Z0-9 .äöüÄÖÜéèàç&(){}-]/_/g'")

set current_file to my temp_filename(track_counter)


set new_file to ("\"" & output_folder & "/" & new_filename &
"\"")
do shell script ("mv " & current_file & " " & new_file)

-- update metadata (tags)


do shell script atomicparsley_path & " " & new_file & "
--artist \"" & track_artist & "\" --album \"" & track_album & "\" --title \"" &
track_name & "\" --tracknum " & track_number & " --overWrite"
end if
-- save and return metadata of current track
try
set current_track to current track
set track_artist to artist of current_track
set track_album to album of current_track
set track_name to name of current_track
set track_number to track number of current_track
return {md_artist:track_artist, md_album:track_album,
md_name:track_name, md_number:track_number}
on error number -1728 -- not running, no current track
return {}
end try
end tell
end save_metadata

on temp_filename(track_counter)
return ("\"" & output_folder & "/" & track_counter & file_extension & "\"")
end temp_filename

You might also like