Skip to content

Instantly share code, notes, and snippets.

@Mekajiki
Last active February 15, 2018 16:20
Show Gist options
  • Save Mekajiki/e0e7e522df8b99ec301e to your computer and use it in GitHub Desktop.
Save Mekajiki/e0e7e522df8b99ec301e to your computer and use it in GitHub Desktop.
fix "iTunes Music Library.xml" which has album ratings for non-rated tracks
#!/usr/bin/env ruby
out = File.open('/tmp/iTunes Music Library.xml', 'w')
in_path = 'iTunes/iTunes Music Library.xml'
regex_rating = /<key>Album Rating<\/key><integer>\d+<\/integer>/
regex_rating_computed_true = /<key>Album Rating Computed<\/key><true\/>/
regex_rating_computed_false = /<key>Album Rating Computed<\/key><false\/>/
regex_last_line = /<\/plist>/
rate_computed = " <key>Album Rating Computed</key><true/>\n"
line = File.open(in_path).each_cons(2) do |lines|
out << lines.first unless lines.first =~ regex_rating_computed_false
if regex_rating =~ lines.first &&
regex_rating_computed_true !~ lines.last
out << rate_computed
end
out << lines.last if lines.last =~ regex_last_line
end
@Bluejanis
Copy link

According to apple, any changes to the file wont be reflected in iTunes. They say the xml is only a read-only copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment