New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Operating System General OS operation information and support

Script to duplicate Apple Lossless iTunes library as MP3 files


Post Reply New Thread Subscribe

 
Thread Tools
nick_harambee

 
Member Since: Feb 24, 2007
Posts: 100
nick_harambee is on a distinguished road

nick_harambee is offline
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
QUOTE Thanks
McYukon

 
McYukon's Avatar
 
Member Since: May 14, 2009
Location: Near Whitehorse, Yukon
Posts: 2,053
McYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to behold
Mac Specs: 2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9

McYukon is offline
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.
QUOTE Thanks
nick_harambee

 
Member Since: Feb 24, 2007
Posts: 100
nick_harambee is on a distinguished road

nick_harambee is offline
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
QUOTE Thanks
McYukon

 
McYukon's Avatar
 
Member Since: May 14, 2009
Location: Near Whitehorse, Yukon
Posts: 2,053
McYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to behold
Mac Specs: 2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9

McYukon is offline
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.
QUOTE Thanks
nick_harambee

 
Member Since: Feb 24, 2007
Posts: 100
nick_harambee is on a distinguished road

nick_harambee is offline
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
QUOTE Thanks
McYukon

 
McYukon's Avatar
 
Member Since: May 14, 2009
Location: Near Whitehorse, Yukon
Posts: 2,053
McYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to behold
Mac Specs: 2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9

McYukon is offline
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.
QUOTE Thanks
nick_harambee

 
Member Since: Feb 24, 2007
Posts: 100
nick_harambee is on a distinguished road

nick_harambee is offline
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
QUOTE Thanks
McYukon

 
McYukon's Avatar
 
Member Since: May 14, 2009
Location: Near Whitehorse, Yukon
Posts: 2,053
McYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to behold
Mac Specs: 2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9

McYukon is offline
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.
QUOTE Thanks
nick_harambee

 
Member Since: Feb 24, 2007
Posts: 100
nick_harambee is on a distinguished road

nick_harambee is offline
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
QUOTE Thanks
McYukon

 
McYukon's Avatar
 
Member Since: May 14, 2009
Location: Near Whitehorse, Yukon
Posts: 2,053
McYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to beholdMcYukon is a splendid one to behold
Mac Specs: 2012 MBP i7 2.7 GHz 15" Matte - 16 GB RAM - 120 GB Intel SSD - 500 GB DataDoubler Mac OS 10.9

McYukon is offline
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.
QUOTE Thanks
nick_harambee

 
Member Since: Feb 24, 2007
Posts: 100
nick_harambee is on a distinguished road

nick_harambee is offline
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
QUOTE Thanks

Post Reply New Thread Subscribe


« is there a way to change folders to show thumbnails of content? | Installing OS X from Scratch on a Macbook »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Apple Releases iTunes 4, QT 6.2, and iPod Update schweb Apple Rumors and Reports 2 12-17-2008 01:47 AM
Converting 'normal' MP3 files to iTunes Podcast files? stevo-m Music, Audio, and Podcasting 1 03-24-2008 10:51 PM
Same iTunes under 2 accounts lardyboy Music, Audio, and Podcasting 6 01-16-2008 08:22 PM
iTunes Tips And Tricks RJgamer iPod Hardware and Accessories 2 07-12-2007 11:59 AM

All times are GMT -4. The time now is 03:00 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?