Skip to content

Instantly share code, notes, and snippets.

@chrisyip
Last active December 14, 2015 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisyip/5070351 to your computer and use it in GitHub Desktop.
Save chrisyip/5070351 to your computer and use it in GitHub Desktop.
Copy raw photos from iPhoto folder.
#!/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