Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Takes a screenshot every 5 seconds and save the file to a certain folder passed as argument (works on OS X)
#!/usr/bin/env ruby
#
# Takes a screenshot every 5 seconds and save the file to a certain folder passed as argument.
# It uses screencapture, which comes with OS X.
#
#
require 'fileutils'
include FileUtils
def find_max_dir(path)
max_dir = 0
Dir.glob(path + '/*').each do |name|
next unless File.directory?(name)
next unless File.basename(name) =~ /^(\d+)$/
value = $1.to_i
max_dir = value if value > max_dir
end
max_dir += 1
max_dir_name = '%04d' % max_dir
path = File.join(path, max_dir_name)
mkdir_p(path) unless File.exists?(path)
puts "using directory #{max_dir_name}"
path
end
def capture(path)
index = 0
wait_time = 5
while true do
print '.'
name = File.join(path, '%07d.jpg' % index)
if File.exists?(name)
puts "\n#{File.basename(name)} already exists, overwriting"
rm(name)
end
out = `screencapture -t jpg -x "#{name}" 2>&1`
raise "error capturing: #{out}" unless $?.success?
index += 1
sleep wait_time
end
end
path = ARGV.shift
if path.nil?
puts "usage: #{$0} <capture_path>"
exit 1
end
mkdir_p(path) unless File.exists?(path)
path = find_max_dir(path)
capture(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.