Skip to content

Instantly share code, notes, and snippets.

@nlf
Created August 9, 2012 16:14
Show Gist options
  • Save nlf/3305564 to your computer and use it in GitHub Desktop.
Save nlf/3305564 to your computer and use it in GitHub Desktop.
update your itunes liberry
require 'osx/cocoa'
require 'optparse'
include OSX
OSX.require_framework 'ScriptingBridge'
iTunes = SBApplication.applicationWithBundleIdentifier_('com.apple.iTunes')
$opts = {}
optparse = OptionParser.new do |args|
args.banner = 'Usage: update-itunes.rb [opts] <path>'
args.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
$opts[:verbose] = v
end
end.parse!
if ARGV.empty?
puts 'Error: must specify directory'
exit(-1)
else
$opts[:path] = ARGV[0]
end
def deleteTrack(track)
puts "Deleting missing song #{track.name} from library" if $opts[:verbose]
track.delete
end
cur_songs = []
iTunes.sources[0].libraryPlaylists[0].fileTracks.get.each do |track|
deleteTrack(track) if track.location == nil
if track.location != nil
if not File.exists?(track.location.path)
deleteTrack(track)
else
cur_songs.push(track.location.path)
end
end
end
Dir.glob($opts[:path] + '/**/*.mp3').each do |file|
if not cur_songs.include? file
puts "Adding new track #{file} to library" if $opts[:verbose]
track = NSArray.arrayWithArray([NSURL.fileURLWithPath(file)])
iTunes.add_to(track, nil)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment