Today I discovered that when I added album artwork to iTunes, it had only actually added it to some of the tracks in each album. So I did what anyone would do and tried to write a script to rectify that.
This is the first try, after about 3 hours of tinkering. It assumes that you have selected all the songs in an album.
#! /usr/local/bin/macruby
framework 'Foundation'
framework 'ScriptingBridge'
itunes = SBApplication.applicationWithBundleIdentifier("com.apple.itunes")
tracks = itunes.selection.get
# Find some track with artwork
artwork = tracks.map { |track| track.artworks[0] }.find { |a| a }
raise "No selected song has artwork" if artwork.nil?
# I checked artwork.rawData and it is PNG wrapped in an NSConcreteData.
pngData = artwork.rawData
tracks.each do |track|
if track.artworks[0]
puts "[O] #{track.name}"
else
puts "[X] #{track.name}"
# Adding the same artwork object is apparently NG so we get the data from it
# and make a copy.
# There is probably a more straight-forward way to clone an object but
# artwork.copy raises an exception.
# I have tried using the keys 'data' and 'raw data' instead - same results.
dict = {rawData: pngData}
artwork_copy = itunes.classForScriptingClass('artwork').alloc.initWithProperties(dict)
track.artworks.addObject(artwork_copy)
raise "Didn't actually add the artwork" if track.artworks.empty?
end
end
The call to addObject does not raise an exception, but I noticed that it doesn't actually add the artwork to the track (hence the check on the next line to speed up testing the script.)
I have been working mostly from Objective-C examples of ScriptingBridge and can't find any where other people have done this either. Lots of examples of getting the artwork but suprisingly few for setting it...
I did find an interesting mailing list thread from four years ago where someone else had a similar issue, but they never came to a solution either (or found it and didn't post it to the thread, which is worse and if they did that, they're a bad person and should feel bad.)