Skip to content

Instantly share code, notes, and snippets.

@granth
Created April 25, 2011 15:06
Show Gist options
  • Save granth/940639 to your computer and use it in GitHub Desktop.
Save granth/940639 to your computer and use it in GitHub Desktop.
repair busted iTunes file locations with macirb
# repair busted iTunes file locations with macirb
framework 'ScriptingBridge'
itunes = SBApplication.applicationWithBundleIdentifier("com.apple.itunes")
load_bridge_support_file 'iTunes.bridgesupport'
class SBElementArray
def [](value)
self.objectWithName(value)
end
end
l = itunes.sources["Library"].libraryPlaylists.first
missing = l.fileTracks.select {|t| t.location.nil? || t.location.path !~ %r{^/Volumes/hd/iTunes}}
def folders(t)
[
"Music/#{sanitize t.artist}/#{sanitize t.album}",
"Music/#{sanitize t.artist}/Unknown Album",
"Music/Compilations/#{sanitize t.album}",
"Movies/#{sanitize t.name}",
"Podcasts/#{sanitize t.album}",
"Books/#{sanitize t.artist}",
]
end
def possible_files(t)
# FIXME use track number if possible
# FIXME don't sanitize first character if a track number goes before it
folders(t).map {|f| "/Volumes/hd/iTunes/#{f}/*#{sanitize t.name}.*" }
end
def matching_files(t)
Dir.glob possible_files(t)
end
def track_url(t)
files = possible_files t
files.size == 1 && NSURL.fileURLWithPath(files.first)
end
# missing.each {|t| u = track_url t; t.location = u if u}
def sanitize(name)
# FIXME strip trailing whitespace
name.gsub(/[:\/|’<>]|\.$|^\./, "_").gsub /([\[\]])/ { |m| "\\#{m}" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment