Folksonomy iTunes Applescript

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


Tags: , , ,

10 Responses to “ Folksonomy iTunes Applescript”

  1. nautisch Says:

    hi there

    great job, i got frustrated and stopped trying things with the code…. anyway, got some problems with the code

    a.) when just copy/pasted the “” (e.g. in tell app “itunes”), are copied wrong, so i had to manually change them. (maybe due to my OS, run OSX, spanish language setting… anyway,resolved that)
    b.) “–set genre of thisTrack to “”” in line 16 produces an error “expects expression but found set” (that is more or less the message, in my case its just spanish ;-), any idea by what that is caused?

    again, thanks (especially for the props

  2. stevemoser Says:

    These are errors you’ll get when a wordpress newbie posts. Most code uses unstylized ” and workpress likes to change them to fancy “. The -set genre… error is due to the code tag deleting the first dash, — in applescript denotes a comment. I left some of the old code in as a comment you help you see the modifications I made. Try to copy it again and let me know how it works. Enjoy.

  3. nautisch Says:

    ah ok, thats what i thought.
    anyway, copy/pasted the code again, this time it “works”, i.e. it tells me to “ou must select one or more tracks first” if no track is selected, when i select one/more, the script runs (i guess..) i.e. the “execute” button stays grey for quite some time. (longer if more tracks are selected…)
    unfortunately nothing else happens, no tag (of the mp3) is changed and no output is written to the console in the scrtipteditor. (however no syntax etc errors are mentioned)

    tried with several tracks that should work (including moby, metallica, beatles etc…)

  4. nautisch Says:

    by the way, i tried with your api key and also after replacing that with mine, does not work either…

  5. stevemoser Says:

    I used the return function heavily while I was debugging the script. I would first test to see if you are getting the xml file with the tag information. Try to open terminal and running this:
    curl -d api_key=b25b959554ed76058ac220b7b2e0a026 -d method=track.gettoptags -d artist=Metallica -d track=One
    This uses the api key from the api documentation. I’m not sure how to help you if that fails. If that returns ok then I would go back to the script and insert return thexml after the set thexml command and see if it returns the tags or if it fails. I’ll test this out with my friend’s MacBook to try to narrow down the problem. Good luck.

  6. nautisch Says:

    get the xml in the console (both with the key u used as well as with mine)

    “return the xml” in the actual script (after “if thexml does not start with “failed” then) also works, writes the following (without the —)


    thrash metal
    202476</co …. and so forth

    is it possible that the ” and \” cause the problems?

  7. nautisch Says:

    ahhh how do u comment actual code….?

  8. nentecular Says:

    I’d like to know exactly how to use this script, it sounds just like what I want but I don’t know what to do with it to make it work!

  9. nautisch Says:

    i decided to start working on the script again, i post the updates here:

    so far include….
    –uses absolute + relative count as limit
    –supports black/whitelist for spefific tags not affected by other criteria
    –uses valid_tags (i.e. top250 tags used on

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: