Ever wish you could get your music library organized so you could make some really clever smartplaylists and be all smooth and Web 2.0? This script will get you closer. Looking at allmusic guide's genre suggestions got you scratching your head? Let the community decide.

This script uses the artist top tags feed. The very top tag is assigned to genre while other popular tags are placed in the comments field. It's a little slow, but it's faster than doing the work manually. It crashes is Last.fm doesn't like the way you've got the artist tag. Not sure this isn't in the feature bin as it will point out to you tracks that can't be succesfully scrobbled. (clean up your tags!)Suggestings to stream-line would be appreciated. I am an XML novice. Oh, I borrowed big chunks of code from a script on the web i found here

Here's the source:

tell application "iTunes"
if selection = {} then
display dialog "You must select one or more tracks first." buttons {"Cancel"} default button 1 with icon 1
end if
set trackList to selection
set lastartist to ""
set lastgenre to ""
set lastcomment to ""
repeat with i from 1 to count of trackList
set thisTrack to item i of trackList
set theartist to artist of thisTrack
                
set genre of thisTrack to ""
set comment of thisTrack to ""
set thiscomment to ""
                
if theartist = lastartist then
set genre of thisTrack to lastgenre
set comment of thisTrack to lastcomment
else
set theurl to "http://ws.audioscrobbler.com/1.0/artist/" & my encode_text(theartist) & "/toptags.xml"
                        
set tags to {}
                        
set thexml to do shell script "curl " & theurl
if thexml does not start with "No artist exists with this name:" then
tell application "System Events"
delete every XML data
set this_data to make new XML data with properties {name:"toptags", text:thexml}
tell this_data
every XML element of XML element 1
repeat with x in every XML element of XML element 1
set thetag to value of XML element "name" of x
set thestrength to value of XML element "count" of x
                                                        
if thestrength > 50 then
set the end of tags to thetag
end if
end repeat
end tell
delete every XML data
end tell
if tags /= {} then
set genre of thisTrack to item 1 of tags
repeat with i from 1 to count of tags
set thiscomment to thiscomment & "'" & item i of tags & "' "
end repeat
set comment of thisTrack to thiscomment
end if
delay 1
end if
set lastartist to theartist
set lastgenre to genre of thisTrack
set lastcomment to thiscomment
                        
end if
end repeat
        
end tell


-- this sub-routine is used to encode text 
on encode_text(this_text)
set the unacceptable_characters to " &+%'/\"\\"
set the encoded_text to ""
set the character_list to {}
repeat with this_char in this_text
set this_char to the contents of this_char
if this_char is not in the unacceptable_characters then
set the end of the character_list to this_char
else
set the end of the character_list to encode_char(this_char)
end if
end repeat
return (the character_list) as string
end encode_text

-- this sub-routine is used to encode a character 
on encode_char(this_char)
set the ASCII_num to (the ASCII number this_char)
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set x to item ((ASCII_num div 16) + 1) of the hex_list
set y to item ((ASCII_num mod 16) + 1) of the hex_list
return ("%" & x & y) as string
end encode_char