A little over a week ago I noticed that my song star ratings in iTunes were randomly disappearing. Entire playlists were blanked out, and so were other tracks scattered across various albums. To put into perspective how this feels… some of these tracks I hadn't modified the rating for in over 10 years.
I can't understand how this happened en masse, but I believe that it's due to a current bug involving the combination of iTunes Match being active on my Mac yet disabled on my iPhone, and having iTunes music manually syncing back and forth to my iPhone only via a few designated playlists that I have selected. — This issue is actually still happening to me periodically at this time of writing! Yikes!
The other day when I noticed that 800+ of my iTunes songs' star ratings had disappeared... Turns out that's an iTunes Match issue.— Ben Guild (@benguild) February 10, 2015
…Not cool! But luckily, I was able to write some code to repair the ongoing damage.
I generally don't ever remove a song's star rating. Once it's rated, it stays rated… even if its rating is later changed to something else by me. Because of this, I was able to write a script to find any songs that were not currently rated at all in my iTunes Library, and then have it look for the most recent rating that could be found amongst a series of backup iTunes Library files that I restored from a backup drive. That's the key here… I keep routine backups! You should, too.
With some preprocessing (described below), the script that I wrote will output a custom AppleScript for your Mac that will reassign the most recently recovered rating when run for songs whose rating is missing but present in one or more of the backup files that you provide to it.
“This PHP script will find songs in your iTunes Library without star ratings, and will then scour your specified backup files to find the most recent star rating for that file in case it was somehow removed or lost somewhere along the way for some reason. It will generate an AppleScript as output that can be run on your Mac to automatically re-set all of the missing ratings!”
Proceed with these steps at your own risk! This process and code are only designed to work on Mac OS X, and there's no warranty here for the code or anything, of course. I just wanted to share my solution with you in case you ran into this same problem and wanted a head start on fixing it programmatically. :)
The script uses the XML variant of the “iTunes Library” database files as mentioned. If you're restoring these files from a Time Machine backup of your Mac, please know that Time Machine automatically excludes these files from being backed up. To recreate the XML files from ITL files (which are backed up), you're best off creating a temporary additional user account on your Mac and replacing that user's “iTunes Library.itl” file with your backed up version then (…one at a time) opening and quitting iTunes to generate the XML equivalent in the same folder. — I have a tweakable Automator workflow that I used to do this with 30+ files automatically. You can reconfigure and use it (at your own risk) to do the same.
Once you have a folder of your “iTunes Music Library.xml” files of the past (not the *.itl version!), you can run the PHP script as outlined in its included “README.md” file. The output, as mentioned, is an AppleScript that contains unique commands to fix each missing rating for songs in your library. You can run the PHP script as often as you want to find any missing ratings, since it will only generate output if there are ratings missing and the output is only for those particular songs! — I hope that this helps you out if you're a Mac user who has run into this issue, but please keep in mind (again) that this process and any of its steps can potentially make drastic changes to your music library and should be followed at your own risk! Be sure to backup your data, as you hopefully have been doing already. :)