Skip to content

Instantly share code, notes, and snippets.

@TomK32
Created April 24, 2017 08:00
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 TomK32/18709ada9a27a9c81d4d6479fd9a3f70 to your computer and use it in GitHub Desktop.
Save TomK32/18709ada9a27a9c81d4d6479fd9a3f70 to your computer and use it in GitHub Desktop.
move images from aperture lib into a clean directory structure
#!/bin/env ruby
require 'pp'
require 'fileutils'
# Intended for users who don't use aperture anymore (I moved back to linux)
# and need to rescue their photo library somehow.
# Takes two argument, source and target directory
@source = ARGV[0]
@target = ARGV[1]
puts "Copying from #{@source} to #{@target}"
def process_dir(dir)
Dir.foreach(dir) do |file|
if file == '.' || file == '..'
next
end
file = [dir, file].join('/').gsub('//', '/')
if File.directory?(file)
process_dir(file)
else
process_file(file)
end
end
end
IMAGE_TYPES = %w(.jpg .png .tif .jpeg .tiff)
@discared = []
def process_file(file)
ext = File.extname(file).downcase
if file.match(/\/(Previews|Thumbnails)\//)
return
elsif IMAGE_TYPES.include? ext
target = target_filename(file)
if !target
puts "Skipping #{file}"
return
end
create_target_dir(target)
puts "%s => %s" % [File.basename(file), target]
FileUtils.cp(file, target, preserve: true)
elsif !@discared.include?(ext)
@discared.push ext
end
end
def target_filename(filename)
target = filename.dup
target.gsub!(@source, @target + '/')
target.gsub!(/(\.approject|\.apimportgroup)\//, '/')
# remove last directory if we don't overwrite an existing file
tmp = target.split('/')
tmp[-2] = nil
tmp = tmp.compact.join('/')
if File.exist?(tmp)
# Did we already copy that file?
if File.size(tmp) == File.size(filename) && File.mtime(tmp) == File.mtime(filename)
return false
end
return tmp
end
return target
end
def create_target_dir(filename)
FileUtils.mkdir_p(File.dirname(filename))
end
process_dir(@source)
pp @discared
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment