Posts Tagged ‘’ Folksonomy iTunes Applescript

December 26, 2008

*Updated, sorry I used ‘code’ tags instead of
‘pre’ when I posted the code. Try it out now*
I’ll post more details later but I just wanted to get this off before I leave to see my family for the Holidays. This is an applescript for iTunes that I modified. The orginial grabbed the top tag for an artist from and wrote it to the genre field of the ID3 tag of a selection of mp3’s. I modified it to grab the tags for each individial track and write them to the groupings field. This allows you to make smart playlists of your music based on the tags. So in the end your music will be organized in a Folksonomy fashion. Here is the code, think of it as a belated Christmas gift or celebration of the Fevistal of Stephen :)

--orginial script authored by user T-spoon
--modified by Stephen Moser

v1.0 Dec 26 '08
-- initial release

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

or visit
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 thename to name of thisTrack
		set thegroup to grouping of thisTrack

		--set genre of thisTrack to ""
		--set comment of thisTrack to ""
		set thiscomment to ""

		set theurl to "-d api_key=f10c1bc4b66d5a4b6ca86440d95a32e8 -d method=track.gettoptags -d artist=" & my encode_text(theartist) & " -d track=" & my encode_text(thename) & ""

		set tags to {}

		set thexml to do shell script "curl " & theurl
		if thexml does not start with "failed" then

			tell application "System Events"
				delete every XML data

				set this_data to make new XML data with properties {name:"lfm", text:thexml}
				--tell thexml to set level_1 to XML element 1
				--tell thexml to set level_2 to XML element 1 of level_1
				tell XML element of 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
						--change strength to your taste
						if thestrength as integer ≥ 1800 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 grouping of thisTrack to ((get thisTrack's grouping) & thiscomment)
				--appends tags to grouping, can change this to comments
			end if
			delay 2
		end if
		--set lastartist to theartist
		--set lastgenre to genre of thisTrack
		--set lastcomment to thiscomment

	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
			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

You can change the field it writes to to the comments field instead of groupings. Also you can change the strength of the tags it looks for. This script might have problems if your artist name or track name has special characters. I will post updates in the future. Let me know if you have any problems.

This is the original post on that started the project:

Thank you Doug from DougScripts, t-spoon, and nautisch for your help.

I also referenced these sites for the idea.