Transfer iTunes song ratings after re-ripping library

Apr 01, '08 10:30:03AM

Contributed by: vocaro

I originally ripped my entire CD collection into iTunes at the 128 kbps quality setting, but recently I decided to re-rip it at 256 kbps. After completing the re-rip, a problem remained: All of the song ratings in my old iTunes library were now absent from the new one.

Luckily, I had exported the old iTunes library database using the File » Export command, so all of my ratings were preserved. The only trick was transferring the ratings from the export file into the new iTunes library. Doing so manually would have required many hours of tedious work, so instead I wrote an AppleScript to automate the process:

-- Apply iTunes Song Ratings 1.0
-- By Trevor Harmon <email available in source download>
-- License: GPL - http://www.gnu.org/copyleft/gpl.html

(*
This script takes as input a Music.txt file created with iTunes' File > Export command. It then applies the song ratings found in this file to the same songs in the current iTunes library. The song name, artist, and album must match; otherwise, the rating will not be affected. If they do match, the current rating will be replaced.

IMPORTANT: Before running this script, the Music.txt file must be encoded as UTF-8. To do so, simply open Music.txt in TextEdit (or a similar editor) and re-save the file with UTF-8 encoding.
*)

set the_file to choose file
set music_lines to read the_file using delimiter {return}

display dialog "This script may take a very long time to run, depending on the size of your iTunes library. (Processing a 2000-song library required approximately 20 minutes on a 2.5 GHz MacBook Pro.)" & return & return & "Do you still want to continue?"

-- Split the line at tab stops
set text item delimiters to ASCII character 9

-- Start at 2 instead of 1 to skip the header line
repeat with i from 2 to count music_lines
  set music_line to item i of music_lines
  set music_fields to every text item of music_line
  if length of music_fields is 27 then
    set the_name to item 1 of music_fields
    set the_artist to item 2 of music_fields
    set the_album to item 4 of music_fields
    set the_rating to item 26 of music_fields
    tell application "iTunes"
      set the_tracks to (file tracks of library playlist 1 whose name is the_name and artist is the_artist and album is the_album)
      repeat with the_track in the_tracks
        set the rating of the_track to the_rating
      end repeat
    end tell
  end if
end repeat
The script takes as input the Music.txt file created with iTunes' File > Export command. It then applies the song ratings found in this file to the same songs in the current iTunes library. The song name, artist, and album must match; otherwise, the rating will not be affected. If they do match, the current rating will be replaced.

Note: Before running the script, the Music.txt file must be encoded as UTF-8. To do so, simply open the file in TextEdit (or a similar editor) and re-save the file with UTF-8 encoding. The script is also available from my site [136KB download].

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080326014420767