Skip to content

Instantly share code, notes, and snippets.

@dav
Created September 17, 2012 17:40
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dav/3738695 to your computer and use it in GitHub Desktop.
Save dav/3738695 to your computer and use it in GitHub Desktop.
Script to copy photos/videos into iPhone Simulator
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment