public
Last active

Script to copy photos/videos into iPhone Simulator

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
require 'etc'
require 'fileutils'
 
# Copies the most recent MAX_IMAGES photos/videos from the device image dir.
# I use Dropbox to sync my device images to my workstation disk
MAX_IMAGES=500
 
USER_HOME_DIR = Etc::getpwuid.dir
DEVICE_IMAGES_DIR="#{USER_HOME_DIR}/Dropbox/Camera Uploads"
SIMULATOR_IMAGES_DIR="#{USER_HOME_DIR}/Library/Application Support/iPhone Simulator/5.1/Media/DCIM/100APPLE"
 
unless Dir.exists? DEVICE_IMAGES_DIR
puts "No such images directory: #{DEVICE_IMAGES_DIR}"
exit
end
 
unless Dir.exists? SIMULATOR_IMAGES_DIR
puts "No such images directory: #{SIMULATOR_IMAGES_DIR}"
puts "Create it? [y/n]"
answer = STDIN.gets.chomp.downcase
unless answer == 'y' || answer == 'Y'
puts "Ok. Not creating."
exit
end
puts "Creating #{SIMULATOR_IMAGES_DIR}"
FileUtils.mkdir_p SIMULATOR_IMAGES_DIR
end
 
files_sorted_recent_first = Dir.entries(DEVICE_IMAGES_DIR).sort do |a,b|
File.mtime("#{DEVICE_IMAGES_DIR}/#{a}") <=> File.mtime("#{DEVICE_IMAGES_DIR}/#{b}")
end
 
filecount = 0
files_sorted_recent_first.each do |filename|
ext = File.extname(filename).downcase
if %w(.jpg .mov .png).include? ext
filecount += 1
puts "File #{filecount}: #{filename}"
device_path = "#{DEVICE_IMAGES_DIR}/#{filename}"
simulator_path = "#{SIMULATOR_IMAGES_DIR}/IMG_%04d#{ext}" % filecount
mtime = File.mtime(device_path)
system "cp \"#{device_path}\" \"#{simulator_path}\""
File.utime(mtime, mtime, simulator_path)
break if filecount >= MAX_IMAGES
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.