Script to duplicate Apple Lossless iTunes library as MP3 files

Posted:
in macOS edited January 2014
Hi,



I have written the applescript below to duplicate my Apple Lossless iTunes library as a MP3 Library. I wanted the script to be able to do the following things:



1. For all Apple Lossless tracks in iTunes Library (at the moment the script is just set to work with a selection for testing purposes) check if track already exists in MP3 folder with more recent modification date and:

a If yes, do nothing

b If same track with earlier modification date convert and replace track (so that tag changes are transferred)

c If track doesn't exist convert track.



2. If there are any MP3 files in the target folder that aren't in the source folder (i.e. the iTunes Music Folder), delete these files. This is for the scenario when I delete files from my iTunes Library that I no longer want, and I want the equivalent MP3 file to be deleted as well. So once the script has finished running there should be an exact match between the Apple Lossless library and the MP3 folder.



The script below does everything I want it to do with the following caveats:



1. I am not sure how to go about deleting the MP3 files as in part 2. I guess it would have to be an additional section of script which compares each file by name in the folder/subfolder in the iTunes Music Library with the equivalent folders in the target music folder, deleting any files found in the target music folder that don't exist in the iTunes Music folders, but I'm not sure if this is the best/only method, and how I would go about scripting this.



2. Because I need to specify the conversion twice (once if no file exists in target folder and once if it does but with an older modification date) I am wondering about using a subroutine method here. I tried doing so, but no conversions happened even though some should have happened. I wonder if this is because the relevant variables aren't picked up properly by the script when it's a subroutine. I can leave things as they are, just thought it would be good to make the script neater.



Code:


tell application "iTunes"

activate



set sel to selection

set encoderBackup to name of current encoder

set this_folder to "Macintosh HD:Users:nickesktop:music:" as text

set current encoder to encoder "MP3 Encoder"





repeat with this_track in sel

set trackComposer to this_track's composer

set trackAlbum to this_track's album

set trackName to this_track's name

tell application "Finder"



set path1 to "Macintosh HD:Users:nickesktop:music:" & trackComposer as text



if folder path1 exists then

{}

else

make new folder at this_folder with properties {name:trackComposer}

end if



set path2 to this_folder & trackComposer & ":" & trackAlbum as text



if folder path2 exists then

{}

else

make new folder at path1 with properties {name:trackAlbum}

end if



if exists file (path2 & ":" & trackName & ".mp3") then

set file2 to (path2 & ":" & trackName & ".mp3") as alias

set modDate to modification date of file2

set modDate2 to this_track's modification date

if modDate is greater than modDate2 then

{}

else

tell application "iTunes"

try -- skip on failure

set new_track to item 1 of (convert this_track)

set loc to new_track's location

set dbid to new_track's database ID

delete artworks of new_track



-- move the file to new location

do shell script "mv " & (quoted form of POSIX path of loc) & " " & (quoted form of POSIX path of path2 as string)



-- delete the track

delete new_track

end try

end tell



end if

else

tell application "iTunes"

try -- skip on failure

set new_track to item 1 of (convert this_track)

set loc to new_track's location

set dbid to new_track's database ID

delete artworks of new_track



-- move the file to new location

do shell script "mv " & (quoted form of POSIX path of loc) & " " & (quoted form of POSIX path of path2 as string)



-- delete the track

delete new_track

end try

end tell



end if

end tell



end repeat



set current encoder to encoder encoderBackup

display dialog "done"

end tell







Can anyone help with either points?



Thanks



Nick
Sign In or Register to comment.