Hello everyone,
First (real) post. I’m quite a novice with Applescript. I’ve been fiddling around a bit and I am reading Rosenthal’s SE Applescript guide (at page 43 :rolleyes:).
I am now experimenting with a script I found floating around on the net to adjust it to my liking. It fetches the top tags and should place the first hit (highest count) in the genre field in itunes, and a few other high hits in the comment field (at least, that’s how I am intepreting the ‘thestrength’ in the script).
I want to change the script so it uses the tags for tracks instread of the artitst and when that is empty after filtering use the artists’ or albums’ tags. Some tags are often not genres (especially for tracks and albums) so I want to filter those in an ‘if’ loop where I can add unwanted results.
But before I get to the end I’d like to fix a few things first and try and speed it up.
I’ve been at it for hours and the result comes up with the wrong tags in the genre field.
Also, I have no idea how to limit the tags read to a maximum of the first ten to speed things up.
here is the original script:
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
I think that this line
set theurl to “http://ws.audioscrobbler.com/1.0/artist/”; & my encode_text(theartist) & “/toptags.xml”
has the ; as a typo. It won’t compile unless I remove it, after which it runs fine.
The url in this script should become something like
http://ws.audioscrobbler.com/1.0/artist/Peter Gabriel/toptags.xml
The url I need for the track would be something like
http://ws.audioscrobbler.com/1.0/track/Peter%20Gabriel/Solsbury%20Hill/toptags.xml
So this is the script I’ve come up with so far to fetch the track tags:
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 lasttrack 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 genre of thisTrack to ""
set comment of thisTrack to ""
set thiscomment to ""
if thisTrack = lasttrack then
set genre of thisTrack to lastgenre
set comment of thisTrack to lastcomment
else
set theurl to "http://ws.audioscrobbler.com/1.0/track/" & my encode_text(theartist) & "/" & my encode_text(thename) & "/toptags.xml"
set tags to {}
set thexml to do shell script "curl " & theurl
if thexml does not start with "No such track" 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 lasttrack to thisTrack
set lastgenre to genre of thisTrack
set lastcomment to thiscomment
end if
end repeat
end tell
-- this sub-routine is used to encode text
--same as original but removed to shorten the post
The issues I’m having with both scripts is that it is not taking the the tags with the highest count to set the genre, but one of the lower ones. The highest count does show up in the comment field but obviously that is not where I want it.
So finally, the questions.
Can someone help me figure out why the top count does not end up in the genre field and how I can correct that;
Can someone help me figure out how to limit the tags processed to the first 10 or so to speed things up.
Once I get past those issues I’ll focus on filtering and fetching other tags in case track tags are empty or unwanted.
TIA!
Ingemar.
Model: alu iMac 24"
AppleScript: 2.2
Browser: Firefox 3.0
Operating System: Mac OS X (10.5)