Applescript to set Track Comment to Rating

I'm trying to develop an applescript that sets the comment tag to the track's rating. i.e., if it's a 3 star track, I want the comment to say "3 Star".

tell application "iTunes"
  set theTrack to (item 1 of (get selection))
  set comment of theTrack to theRating
end tell

This isn't working for me. Any Ideas?


First thing is that the rating value is just stored as "rating". But iTunes stores the star rating as a value between 0 and 100, so you need to convert from that value to the number of stars.

There might be a smarter way to do this but this code seems to work.

tell application "iTunes"
    set theTrack to (item 1 of (get selection))
    set theRating to rating of theTrack
    if theRating = 100 then
        set comment of theTrack to "5 Star"
    else if theRating ≥ 80 then
        set comment of theTrack to "4 Star"
    else if theRating ≥ 60 then
        set comment of theTrack to "3 Star"
    else if theRating ≥ 40 then
        set comment of theTrack to "2 Star"
    else if theRating ≥ 20 then
        set comment of theTrack to "1 Star"
    else if theRating = 0 then
        set comment of theTrack to "0 Star"
    end if
end tell