Skip to content

Instantly share code, notes, and snippets.

@youpy
Created April 16, 2012 16:49
Show Gist options
  • Save youpy/2399915 to your computer and use it in GitHub Desktop.
Save youpy/2399915 to your computer and use it in GitHub Desktop.
#!/usr/bin/env macruby
# encoding: utf-8
require 'ubygems'
require 'digest/md5'
framework 'cocoa'
framework 'ScriptingBridge'
# http://www.johnmyleswhite.com/notebook/2009/11/20/cleaning-up-an-itunes-library-with-macruby/comment-page-1/#comment-17798
load_bridge_support_file File.expand_path('~/local/ITunes.bridgesupport')
RATING_DUPLICATED = 21
itunes = SBApplication.applicationWithBundleIdentifier("com.apple.itunes")
lib = itunes.sources[0].libraryPlaylists[0]
hash = {}
lib.fileTracks.each_with_index do |track, i|
next unless track.respond_to?(:location)
next unless track.location
next if track.rating == RATING_DUPLICATED # already marked as duplicated
path = track.location.path
next unless path =~ /(wav|mp3|aiff?)$/i;
key = Digest::MD5.hexdigest([track.name, track.duration.to_i].join('$$$'))
puts 'check: %s' % [track.name, track.artist, key].join(' - ')
if hash[key]
puts 'duplicate: %s' % [track.name, track.artist].join(' - ')
# keep rating
if track.rating > 20
hash[key].rating = track.rating
end
# mark as duplicated track
track.rating = RATING_DUPLICATED
else
hash[key] = track
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment