Backup your CDs to Apple's Lossless Codec (or any QuickTime codec)

This is a little long, so if you’re interested bear with me… It’s not that hard. I’ve been wanting to have a copy of all my cds in apple’s lossless format so I can have the best quality sound when playing them through my home stereo (at a minimum of size). Now that i’m learning applescript I have the knowledge to make the process as easy as possible, so I’m going for it! Just follow the directions in the script. Any improvements will be appreciated.


-- this script will encode your audio CD's into any format you can set through QuickTime. It will copy the formatted music to a location you set under the "variables to change" section of the script. The script will place the files inside a subfolder of the artist name and a subfolder of the CD name (i.e. the album name). Follow the instructions below. If you don't want QuickTime to encode the files, you can copy the aiff files directly from the CD. Look for these instructions near the bottom of the script. iTunes is used in this script only to get the artist's and song names for the CD, your music will not be copied into iTunes.

(*
--INSTRUCTIONS--
1. open System Preferences and make the setting... system preferences>CDs & DVDs>When you insert a music CD>Ignore
2. start itunes and leave it running while you use this script, it's needed to get artist and song names
3. open the preferences in iTunes and make the setting... iTunes preferences>advanced>importing>On CD Insert>Show CD
4. create a quicktime settings file to tell the script how you want to encode the songs. Follow the directions to do this. Note: I used export to "Movie to QuickTime Movie", options-sound settings-"Apple Lossless" "Stereo" "Recommended" "Best".
	a) Open a music file from the CD in QuickTime
	b) Choose "Export" from the "File" menu
	c) Select the desired export kind from the "Export:" popup menu (Movie to QuickTime movie in this example)
	d) Click the "Options..." button to specify your custom settings, then click OK
	e) Note that the "Use:" popup menu now displays "Most Recent Settings"
	f) Start the export by clicking on the "Save" button (if you don't start an export the "Most Recent Settings" won't stick)
	g) Cancel the export if it is a long one
	h) To double-check the export settings, select the "Export..." menu item again and check them
	i) Without closing the open movie, run the following AppleScript and set the path to where you want the settings file saved
		tell app "QuickTime Player"
		tell first movie
		save export settings for QuickTime movie to file "Leopard:CD Backups: ExportAppleLossless"
		end tell
		end tell
5. in this script, you need to set the path to your destination folder (in the "variables to change section")
6. in this script, you need to set the path to your quicktime settings file (in the "variables to change section")
7. That's it! Insert a CD and run the script. When the songs have been imported you will be asked to insert another CD or you can quit the script. Have Fun!!!
*)

-- variables to change
set destinationFolder to "Leopard:CD Backups:" -- the path to the folder where you want your music stored
set quicktimeSettings to alias "Leopard:CD Backups: ExportAppleLossless" -- the path to your quicktime settings file

try
	repeat
		set badSongs to {}
		-- wait for CD to be inserted
		activate me
		set tempVar to display dialog "Please insert a CD, close the tray, and wait until the CD mounts before clicking \"Start\"" with icon note buttons {"Quit", "Start"} default button "Start" with title "Start Encoding"
		set buttonEntered to the button returned of tempVar
		if buttonEntered is "Quit" then exit repeat
		
		-- get name of inserted cd
		tell application "System Events"
			set audioCD to 1st disk whose format is audio format and local volume is true
			set cdName to name of audioCD
		end tell
		
		-- get artist name of cd from itunes
		tell application "iTunes"
			set theCD to view of front browser window
			if kind of container of theCD is audio CD then set theArtist to artist of theCD
		end tell
		
		-- search the destination folder to see if the artist folder already exists, if not make it
		tell application "Finder"
			set folderExists to false
			set artistFolders to the folders of (destinationFolder as alias)
			repeat with anArtistFolder in artistFolders
				if name of anArtistFolder is theArtist then
					set folderExists to true
				end if
			end repeat
			if folderExists is false then
				make new folder at destinationFolder with properties {name:theArtist}
			end if
			
			-- search the artist folder to see if the album exists, if it does stop else make new folder
			set albumExists to false
			set theArtistFolder to alias (destinationFolder & theArtist & ":")
			set albumFolders to folders of theArtistFolder
			repeat with anAlbumFolder in albumFolders
				if name of anAlbumFolder is cdName then
					set albumExists to true
				end if
			end repeat
			if albumExists is true then
				display alert "The CD named " & "\"" & cdName & "\"" & " with Artist name " & "\"" & theArtist & "\"" & " already exists" message "The Script will not copy this CD's Files!" as warning
				error
			else
				make new folder at theArtistFolder with properties {name:cdName}
			end if
			
			-- encode music with quicktime in apple lossless format
			set theAlbumFolder to alias (destinationFolder & theArtist & ":" & cdName & ":")
			set theFiles to the files of audioCD
			set myCounter to -1
			repeat with anItem in theFiles
				try
					set myCounter to myCounter + 1
					set theItem to anItem as alias
					set itemInfo to the info for theItem
					set theItemName to (destinationFolder & theArtist & ":" & cdName & ":" & (displayed name of itemInfo))
					if name extension of itemInfo is "aiff" then
						tell application "QuickTime Player"
							open theItem
							tell first movie
								export to theItemName as QuickTime movie using settings quicktimeSettings
								delay 0.1
								close
								delay 0.1
							end tell
						end tell
					end if
				on error
					set badSongs to badSongs & myCounter
				end try
			end repeat
			get badSongs
			
			-- Note: if you want to copy the raw aiff files from the cd instead of encoding them to Apple Lossless format first then comment out the section above this Note and uncomment the section below this Note.
			(*
		-- copy cd aiff tracks to artist folder
		set theAlbumFolder to alias (destinationFolder & theArtist & ":" & cdName & ":")
		set theFiles to the files of audioCD
		repeat with anItem in theFiles
			set theItem to anItem as alias
			move theItem to theAlbumFolder
		end repeat
		*)
			
			-- get the song numbers that did not get copied
			if badSongs is not {} then
				activate me
				set AppleScript's text item delimiters to ", "
				set the_song_numbers to badSongs as string
				set AppleScript's text item delimiters to ""
				display alert "The was a problem copying some of the songs into your destination folder." & return & return message "It's often related to odd characters in the song name." & return & "Artist Folder Name: " & theArtist & return & "Album Folder Name: " & cdName & return & "Song Numbers not copied: " & the_song_numbers
			end if
			
		end tell
	end repeat
end try

One other thing that might help you. When QuickTime is encoding the song files, it happens in the backgound because I didn’t want quicktime to keep coming to the front and bothering me while it’s doing its job. There’s a problem with this though… the quicktime icon in the dock will bounce like crazy because it’s in the background… so I wrote this script to kill the dock behavior of bouncing. With this script you can set the dock to bounce or not bounce, so I stop the dock bouncing while I’m encoding and when I’m finished I can put the normal dock behavior back. Use this script at your discretion.


-- stop or start dock bouncing when an application wants your attention
set tempVar to display dialog "Do you want the Dock icons to bounce?" with icon note buttons {"Cancel", "Don't Bounce", "Bounce"} default button "Bounce"
set buttonEntered to the button returned of tempVar

if buttonEntered is "Don't Bounce" then
	do shell script "defaults write com.apple.dock no-bouncing -bool Yes"
else if buttonEntered is "Bounce" then
	do shell script "defaults write com.apple.dock no-bouncing -bool No"
end if

quit application "Dock"