iTunes and duplicates

Hi,
recently, I wrote a litte script for for iTunes to get rid of duplicate tracks.
My purpose was to use the build-in command šfind duplicates˜ which is a bit useless, because shows all files of a given track with the same name, forcing the user to select and delete the 2nd-Xnd duplicates manually. Furthermore, we have to run šfind duplicates˜ several times to find all duplicate tracks, especially if our music library is big.

Some suggestions to improve this script?

--create duplicates list 
set dupe_playlist_name to "Duplicates"

tell application "iTunes" to if (not (exists user playlist dupe_playlist_name)) then make new playlist with properties {name:dupe_playlist_name}
end

set x to the button returned of (display dialog žPlease drag found duplicates to playlist šDuplicates˜, then press continue"  buttons {žContinue", žCancel"} default button 1)
if x is žContinue" then
	tell application "iTunes"
		set view of front window to playlist "Duplicates"
		set thisPlaylist to view of front window
		try
			repeat with i from 1 to count of tracks in thisPlaylist
				set nm to name of track i of thisPlaylist
				set g to (tracks of thisPlaylist whose name is nm)
				if (count of g) > 1 then
					repeat with x from 2 to count of items in g
						set this_g to item x of g
						delete this_g
					end repeat
				end if
			end repeat
		end try
	end tell
	display dialog žDuplicates filtered. Select and delete tracks permanently with option+Delete !" buttons "End" default button 1
end if

Here’s an idea. I haven’t tried this… just wrote it off the top of my head. It works by getting all the tracks of a playlist, then compiling a list of their names and their database id’s. So you end up with a list of lists (LOL). I then sort that LOL by name so all the names are next to each other in the LOL. Then you can repeat through it backwards… if the name of an item is equal to the name of the item just before it in the list then it’s a duplicate. So to delete a duplicate, you find that track in the main music library using its database id and delete that.

As mentioned, I haven’t tried this so proceed with caution. I can’t say if this is any faster than your method… it’s just an idea. However, at least I think you could improve your script by deleting a file as I have using the ID and finding it in the main library, because they way you’re deleting it will only remove it from the duplicates playlist and not from your library.

set dupe_playlist_name to "Duplicates"

tell application "iTunes"
	set allTracks to tracks of playlist dupe_playlist_name
	
	set initialLOL to {}
	repeat with aTrack in allTracks
		set end of initialLOL to {name, database ID} of aTrack
	end repeat
end tell

set sortedLOL to sortListofLists(initialLOL, 1)

repeat with i from (count of sortedLOL) to 2 by -1
	set thisList to item i of sortedLOL
	set previousList to item (i - 1) of sortedLOL
	if (item 1 of thisList) is (item 1 of previousList) then
		-- we need to delete the track permanently because it's a duplicate
		tell application "iTunes"
			try
				delete (some track of library playlist 1 whose database ID is (item 2 of thisList))
			end try
		end tell
	end if
end repeat



(*======================== SUBROUTINES =========================*)
on sortListofLists(array, sortItemNum) --> this is a slight modification of the bublesort routine to make it work with a list of lists
	repeat with i from length of array to 2 by -1 -- go backwards
		repeat with j from 1 to i - 1 -- go forwards
			if (item sortItemNum of (item j of array)) > (item sortItemNum of (item (j + 1) of array)) then
				tell array to set {item j, item (j + 1)} to {item (j + 1), item j}
			end if
		end repeat
	end repeat
	return array
end sortListofLists