Results 1 to 11 of 11
  1. #1


    Member Since
    Feb 24, 2007
    Posts
    100
    Script to duplicate Apple Lossless iTunes library as MP3 files
    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
    OS X 10.5.7
    Mac Pro
    2 x 3 GHz Dual-Core Intel Xeon
    8 GB RAM

  2. #2

    McYukon's Avatar
    Member Since
    May 14, 2009
    Location
    Near Whitehorse, Yukon
    Posts
    2,052
    Specs:
    2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9
    Look here for a lot of useful iTunes applescripts, Doug's AppleScripts for iTunes ♫ - dougscripts.com
    If I may have some time I may try to figure out a way to sync the Apple Lossless and the MP3 folder using applescript.
    Use the reputation system if a post helps you!
    Mac Forums IRC Channel - Help build it by hanging out with us
    The next sentence is true. The previous sentence is false.

  3. #3


    Member Since
    Feb 24, 2007
    Posts
    100
    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.
    OS X 10.5.7
    Mac Pro
    2 x 3 GHz Dual-Core Intel Xeon
    8 GB RAM

  4. #4

    McYukon's Avatar
    Member Since
    May 14, 2009
    Location
    Near Whitehorse, Yukon
    Posts
    2,052
    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
    Use the reputation system if a post helps you!
    Mac Forums IRC Channel - Help build it by hanging out with us
    The next sentence is true. The previous sentence is false.

  5. #5


    Member Since
    Feb 24, 2007
    Posts
    100
    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
    OS X 10.5.7
    Mac Pro
    2 x 3 GHz Dual-Core Intel Xeon
    8 GB RAM

  6. #6

    McYukon's Avatar
    Member Since
    May 14, 2009
    Location
    Near Whitehorse, Yukon
    Posts
    2,052
    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

    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
    Use the reputation system if a post helps you!
    Mac Forums IRC Channel - Help build it by hanging out with us
    The next sentence is true. The previous sentence is false.

  7. #7


    Member Since
    Feb 24, 2007
    Posts
    100
    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!
    OS X 10.5.7
    Mac Pro
    2 x 3 GHz Dual-Core Intel Xeon
    8 GB RAM

  8. #8

    McYukon's Avatar
    Member Since
    May 14, 2009
    Location
    Near Whitehorse, Yukon
    Posts
    2,052
    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.
    Use the reputation system if a post helps you!
    Mac Forums IRC Channel - Help build it by hanging out with us
    The next sentence is true. The previous sentence is false.

  9. #9


    Member Since
    Feb 24, 2007
    Posts
    100
    I think there's some wires crossed somewhere, but I'm not sure where

    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.
    OS X 10.5.7
    Mac Pro
    2 x 3 GHz Dual-Core Intel Xeon
    8 GB RAM

  10. #10

    McYukon's Avatar
    Member Since
    May 14, 2009
    Location
    Near Whitehorse, Yukon
    Posts
    2,052
    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.
    Use the reputation system if a post helps you!
    Mac Forums IRC Channel - Help build it by hanging out with us
    The next sentence is true. The previous sentence is false.

  11. #11


    Member Since
    Feb 24, 2007
    Posts
    100
    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
    OS X 10.5.7
    Mac Pro
    2 x 3 GHz Dual-Core Intel Xeon
    8 GB RAM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Build a new library just of MP3 files in iTunes
    By romatech in forum Music, Audio, and Podcasting
    Replies: 4
    Last Post: 03-19-2013, 09:21 AM
  2. Converting Apple lossless to MP3...
    By MEZLEY in forum Music, Audio, and Podcasting
    Replies: 3
    Last Post: 10-10-2011, 02:57 AM
  3. iTunes Library: Managing aiff and mp3 files
    By allen_gibberish in forum iPod Hardware and Accessories
    Replies: 7
    Last Post: 01-01-2011, 11:00 PM
  4. Sharing iPhoto Library and duplicate files.
    By Doug b in forum Switcher Hangout
    Replies: 10
    Last Post: 09-08-2008, 06:43 PM
  5. syncing ratings between AAC and Apple Lossless iTunes libraries using Apple Script
    By nick_harambee in forum iPod Hardware and Accessories
    Replies: 0
    Last Post: 10-05-2007, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •