automatic audio cd import and playlist cretaion

hey everybody,
i made a script with the help of code snippets out of this forum to automate the progress of importing audio cd’s into itunes. the script also create a sorted playlist within a playlist folder. i thought i might share my success (i don’t put any credit on it)

tell application "iTunes"
	activate
	set mainlibrary to library playlist 1
	set myCD to first source whose kind is audio CD
	set allCDtracks to get a reference to (every audio CD track of playlist 1 of myCD)
	
	-- check the tracks
	repeat with aCdtrack in allCDtracks
		tell aCdtrack
			set {nom, alb, art, dis} to {name, album, artist, disc number}
			set enabled to true
		end tell
		
		-- if track is in iTunes
		-- then un-check enabled track on CD
		if exists (some track of mainlibrary whose album is alb and artist is art and name is nom) then
			set enabled of aCdtrack to false
		end if
	end repeat
	
	-- ok, now import any enabled tracks
	set allEnabled to get a reference to (every audio CD track of playlist 1 of myCD whose enabled is true)
	with timeout of 30000 seconds
		convert allEnabled
	end timeout
	
	-- create temporary playlist of just the imported audio cd in a playlist folder
	set TempPlayList to "Temp" as text
	
	if playlist TempPlayList exists then
		delete every track of playlist TempPlayList
	else
		make new playlist with properties {name:TempPlayList} at folder playlist "_rips"
	end if
	
	-- copy songs to Temp playlist
	duplicate (every file track of playlist 1 whose album is alb) to user playlist TempPlayList
	
	--sort playlist
	set {theAlbum, discNumber} to {alb, dis}
	set theTracks to every track of playlist TempPlayList
	set TrackNum to {}
	set flag to true
	repeat with i from 1 to count theTracks
		if track number of item i of theTracks = 0 then exit repeat
		if track number of item i of theTracks is not equal to i then set flag to false
		set end of TrackNum to track number of item i of theTracks
	end repeat
	
	if (count TrackNum) = (count theTracks) and flag = false then
		my sort_items(TrackNum, theTracks)
	end if
	
	-- create final playlist
	if dis > 1 then
		set PlayListName to art & " - " & alb & " - Disc " & dis as text
	else
		set PlayListName to art & " - " & alb
	end if
	
	if playlist PlayListName exists then
		delete every track of playlist PlayListName in folder playlist "_rips"
	else
		make new playlist with properties {name:PlayListName} at folder playlist "_rips"
	end if
	
	repeat with i in theTracks
		duplicate i to playlist PlayListName
	end repeat
	
	--delete Temp playlist
	delete playlist TempPlayList
	
	-- eject audio cd
	tell application "Finder" to eject disk alb
	
end tell

to sort_items(sortList, SecondList)
	script L
		property srt : sortList
		property sec : SecondList
	end script
	tell (count L's srt) to repeat with i from (it - 1) to 1 by -1
		set s to (L's srt)'s item i
		set r to (L's sec)'s item i
		repeat with i from (i + 1) to it
			tell (L's srt)'s item i to if s > it then
				set (L's srt)'s item (i - 1) to it
				set (L's sec)'s item (i - 1) to (L's sec)'s item i
			else
				set (L's srt)'s item (i - 1) to s
				set (L's sec)'s item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > (L's srt)'s end then
			set (L's srt)'s item it to s
			set (L's sec)'s item it to r
		end if
	end repeat
end sort_items

if anyone has any suggestions or improvement tips, feel free to leave a comment :wink:

cheers
goeste