Last active
December 14, 2015 10:10
-
-
Save chrisyip/5070351 to your computer and use it in GitHub Desktop.
Copy raw photos from iPhoto folder.
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
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
require 'optparse' | |
require 'fileutils' | |
def get_photo (path) | |
photos = [] | |
if File.directory?(path) then | |
Dir[path + '/*'].each { |path| | |
photos += get_photo(path) | |
} | |
else | |
photos.push path if File.file?(path) | |
end | |
photos | |
end | |
options = {} | |
@errors = [] | |
OptionParser.new { |opts| | |
opts.banner = 'Usage: [options]' | |
opts.on('-f path/to/source') { |p| | |
options[:source_path] = p | |
} | |
opts.on('-t path/to/save') { |p| | |
options[:target_path] = p | |
} | |
opts.on('-h') { | |
puts opts | |
exit | |
} | |
opts.parse! | |
} | |
options[:source_path] = (File.expand_path options[:source_path] || '~/Pictures/iPhoto Library.photolibrary') + '/Masters' | |
options[:target_path] = File.expand_path options[:target_path] || '~/Desktop' | |
puts "Copying from #{options[:source_path]} to #{options.target_path}..." | |
get_photo(options[:source_path]).each { |path| | |
begin | |
f = File.new(path) | |
ext = File.extname(f) | |
basename = File.basename(f, ext) | |
f2 = "#{options[:target_path]}/#{basename}#{ext}" | |
while File.exist?(f2) | |
f2 = "#{options[:target_path]}/#{basename}_#{Time.now.usec}#{ext}" | |
end | |
FileUtils.cp f, f2 | |
rescue Exception => e | |
@errors.push path | |
end | |
} | |
@errors.each { |path| | |
puts 'These file(s) can not be copied:' | |
puts path | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment