sort and delete duplicates in list (itunes Comment)

I have multiple genres in comments from different programs and want to sort and delete duplicates
an example comment for a song could be:
Pop,pop,r&b,female vocalist,sad,ballad,hip-hop,soul,english,r&b,pop,reggae,female vocalists,2006,00s,dance,r&b,
the script below will do 10 tracks then you start it again, I have tried to do it for 50 or 100 then itunes comes back with error message can’t save library.
Is there anyway to get script to go through whole library and faster? Any ideas would be appreciated.


repeat 10 times
tell application “iTunes”
activate
reveal current track
set someText to comment of current track
set AppleScript’s text item delimiters to {“,”}
set delimitedList to every text item of someText
set the_list to delimitedList
set return_list to {}
repeat with an_item in the_list
if return_list does not contain an_item then set end of return_list to (contents of an_item)
end repeat
end tell

simple_sort(return_list)
tell application "iTunes"
	next track
end tell

end repeat
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to “”
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is “” then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat

set someText to sorted_list
set AppleScript's text item delimiters to {","}
set newdelimitedList to every text item of someText
set the_list to newdelimitedList
set return_list to {}
repeat with an_item in the_list
	if return_list does not contain an_item then set end of return_list to (contents of an_item)
end repeat

tell application "iTunes"
	activate
	reveal current track
	
	tell current track
		try
			
			set comment to every text item of sorted_list as text
		end try
		
	end tell
	
end tell

end simple_sort

I cannot reproduce the error in iTunes 10.

Instead of revealing the current track you can access them by index:

tell application "iTunes"
	tell source 1
		tell playlist 1
			repeat with i from 1 to (number of tracks)
				get comment of track i 
--........
			end repeat
		end tell
	end tell
end tell

With this script I can select more than 5000 tracks and come back later and it’s all done with no errors and much faster.
I finally figured it out, woks much better:


tell application “iTunes”
set sel to selection
if sel is not {} then
repeat with aTrack in sel
set someText to comment of aTrack
set AppleScript’s text item delimiters to {“,”, ", “, " ,”}
set delimitedList to every text item of someText
set the_list to delimitedList
set return_list to {}
repeat with an_item in the_list
if return_list does not contain an_item then set end of return_list to (contents of an_item)
end repeat
tell aTrack
try
set theTitleList to (my simple_sort(return_list))
set comment to every text item of theTitleList as text
end try
end tell
end repeat
else
display dialog return & “No tracks are selected…” buttons {“Cancel”} ¬
default button 1 with icon 0 giving up after 15
return
end if
end tell

on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to “”
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is “” then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort