Created
November 21, 2011 10:37
-
-
Save midore/1382271 to your computer and use it in GitHub Desktop.
remove filetracks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework "ScriptingBridge" | |
# https://gist.github.com/1382271 | |
module MyBridge | |
class RemoveSameSong | |
def initialize | |
i = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes") | |
i.run | |
@filetracks = i.sources[0].userPlaylists[0].fileTracks | |
@h = Hash.new | |
end | |
def run | |
h = Hash.new | |
@filetracks.sort_by{|t| t.album}.each{|x| | |
s = "#{x.trackNumber}-#{x.album}-#{x.size}-#{x.artist}" | |
(h[s] ||= x; next) unless h[s] | |
obj = which_obj(x, h[s]) | |
next unless obj | |
@h[obj.databaseID] = get_path(obj) | |
} | |
return nil if @h.empty? | |
delete_renmae_obj | |
end | |
private | |
def delete_renmae_obj | |
n, m = 0, 0 | |
@h.each{|k,v| | |
t = @filetracks.find{|x| x.databaseID == k} | |
next unless t | |
print "deleted: #{t.trackNumber} / #{t.name} / #{t.album}\n" | |
t.delete | |
n += 1 | |
next unless v | |
m += 1 if rename_path(v.to_s) | |
} | |
print "\nDeleted:#{n} / Moved:#{m}\n" | |
end | |
def which_obj(obj1, obj2) | |
return obj1 if check_path(get_path(obj1)) | |
return obj2 if check_path(get_path(obj2)) | |
return nil if (get_path(obj1).nil? && get_path(obj2).nil?) | |
return obj1 unless get_path(obj1) | |
return obj2 unless get_path(obj2) | |
end | |
def get_path(obj) | |
return obj.location.path if obj.location | |
return nil | |
end | |
def check_path(f) | |
return nil unless f | |
w = /\d+\.(\w..)/ | |
w.match(f.to_s.split.last) | |
end | |
def rename_path(v) | |
dir = tmpdir | |
begin | |
#gv = v.gsub(/\&|\[|\]|\'|\-|\(|\)|\!/,'*').gsub(/\s/, '\ ' ) | |
gv = v.gsub(/\s/, "\ " ) | |
system("mv \'#{gv}\' #{dir}/") | |
print "moved: #{gv}\n" | |
rescue | |
return nil | |
end | |
return true | |
end | |
def tmpdir | |
trash = File.join(ENV['HOME'], "trashsongs") | |
Dir.mkdir(trash) unless File.exist?(trash) | |
return trash | |
end | |
end | |
end | |
MyBridge::RemoveSameSong.new().run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment