Script to duplicate Apple Lossless iTunes library as MP3 files

Joined
Feb 24, 2007
Messages
100
Reaction score
0
Points
16
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:nick:Desktop: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:nick:Desktop: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
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
OP
N
Joined
Feb 24, 2007
Messages
100
Reaction score
0
Points
16
Thanks. I am fairly familiar with Doug's scripts, but I have not come across one that compares different folders of music files.

I presume that the best method would be to work folder by folder comparing folders and subfolders of the main destination folder 'music' with their equivalent folders and subfolders in the iTunes music library, removing any file (checked by filename) in the destination folder that is not found in the source folder. But I am not sure how to go about this.
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
This should work, I successfully tested it with a couple of mp3s.

Code:
tell application "iTunes"
	activate
	
	set sel to selection
		
	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 path3 to "Macintosh HD:Users:nick:Desktop:music" & trackComposer as text #Source folder
			set path4 to "Macintosh HD:Users:nick:Desktop:mp3" & trackComposer as text #Destination folder
			
			if exists file (path3 & ":" & trackName & ".mp3") then
				if exists file (path4 & ":" & trackName & ".mp3") then
					move file (path3 & ":" & trackName & ".mp3") to trash
				else
					{}
				end if
			end if
			
		end tell
	end repeat
end tell
 
OP
N
Joined
Feb 24, 2007
Messages
100
Reaction score
0
Points
16
Thanks, but I can't see how this would work.

Your script seems to be removing mp3s from the source folder if they are also found in the destination.

What I am looking for is to remove mp3 files from the destination folder if they are not found in a m4a version in the source (iTunes) folder.

Nick
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
Oh!, sorry I misunderstood and got it backwards.
Now it removes files found in the mp3 folder if that same file is not found in the music (iTunes) folder.
Hope I got it this time :p

Code:
tell application "iTunes"
	activate
	
	set sel to selection
	
	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 path3 to "Macintosh HD:Users:nick:Desktop:music" & trackComposer as text #Source folder
			set path4 to "Macintosh HD:Users:nick:Desktop:mp3" & trackComposer as text #Destination folder
			
			if exists file (path4 & ":" & trackName & ".mp3") then
				if exists file (path3 & ":" & trackName & ".m4a") then
					{}
				else
					move file (path4 & ":" & trackName & ".mp3") to trash
				end if
			end if
			
		end tell
	end repeat
end tell
 
OP
N
Joined
Feb 24, 2007
Messages
100
Reaction score
0
Points
16
Thanks. I think there may still be a problem though. I only have Apple Lossless files in my iTunes Library (the MP3 files are not in the Library, only in the destination folder). So as the script you posted cycles through tracks in the iTunes Library it won't know of the existence of the mp3 files that need to be deleted. I hope this makes sense!
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
I know what you mean but since we add the .mp3/.m4a extension later it may not matter.
The script only gets the name of the song from iTunes and then it later adds the respective extension to the name.

When I tried it with my mp3 only iTunes library it worked with the two test folders.
The mp3s that didn't exist in the iTunes folder as m4a got deleted in the mp3 folder.
 
OP
N
Joined
Feb 24, 2007
Messages
100
Reaction score
0
Points
16
I think there's some wires crossed somewhere, but I'm not sure where;P

If I am wanting to delete tracks from the destination folder (which consists of MP3 files outside of the iTunes Library) that once existed in my iTunes Library as m4a files, but which have since been deleted from the iTunes Library, then a script which generates the paths to compare using just the tracks currently in my iTunes library isn't going to delete any MP3 files, because the paths for these MP3 can't be generated by referring to the iTunes Library.

I tried running the script, and as I suspected it didn't delete files that should have been deleted.
 
Joined
May 14, 2009
Messages
2,052
Reaction score
136
Points
63
Location
Near Whitehorse, Yukon
Your Mac's Specs
2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
Yeah, I had my wires crossed :)
Anyways since the songs aren't in the library anymore I don't quite know how to continue.
I tried different things I know for the better part of 4 hours yesterday night and couldn't come up with anything.
I'm also thinking that a shell script could maybe do it, but I have even less experience in that area.

I would try asking over at macscripter.net, hopefully someone there can help you.
 
OP
N
Joined
Feb 24, 2007
Messages
100
Reaction score
0
Points
16
Thanks.

I now have a script that does everything I want, including deleting superfluous tracks from the destination folder, except where there are characters in the track name in iTunes that aren't supported in filenames in Finder (i.e. ? / :). This means that all tracks which contain these characters in the name are re-converted every time I run the script.

Is there a way I wonder of adapting the script so that when it is checking to see if the track already exists in the destination folder, it replaces these characters in the track name with an _ (which is what happens in Finder) for the purposes of the comparison?

Nick

Code:
tell application "iTunes"
	activate
	
	set sel to every track of playlist "1 music library"
	set encoderBackup to name of current encoder
	set this_folder to "Macintosh HD:Users:nick:Desktop: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:nick:Desktop:music:" & trackComposer as text
			
			if not (folder path1 exists) then
				make new folder at this_folder with properties {name:trackComposer}
			end if
			
			local path2
			set path2 to this_folder & trackComposer & ":" & trackAlbum as text
			
			if not (folder path2 exists) then
				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 not (modDate is greater than modDate2) then
					my convertTrack(this_track, path2)
				end if
			else
				my convertTrack(this_track, path2)
			end if
		end tell
		
	end repeat
	
	set current encoder to encoder encoderBackup
	display dialog "done"
end tell

on convertTrack(this_track, path2)
	tell application "iTunes"
		try -- skip on failure
			set new_track to item 1 of (convert this_track)
			set loc to new_track's location
			-- 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 convertTrack
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top