Skip to content

Instantly share code, notes, and snippets.

@reednj
Last active December 8, 2016 23:42
Show Gist options
  • Save reednj/d958cf178ea348102ecdaf86a788cf59 to your computer and use it in GitHub Desktop.
Save reednj/d958cf178ea348102ecdaf86a788cf59 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# screens.rb
# @reednj, 2016-12-06
#
# This script will take a screenshot of the desktop on up to two monitors, and save it in the
# given location (usually Dropbox). It will only take a screenshot if the user is active.
#
# If there is already a screenshot that exists for that day, it will _sometimes_ overwrite it
# and make a new one. This is an attempt to get a screenshot at a random time each day.
#
require 'time'
class App
def main
@dir = "/Users/reednj/Dropbox/Transfer/desktop"
# if we have been idle for more than 30 seconds, just exit
if idletime > 30.0
puts 'user not active'
exit
end
if !exist?
# if the screenshot for today doesn't already exist, then
# take it
puts 'first screenshot for the day'
screenshot
else
# if the screenshot does exist, we overwrite it some of the time
if one_in_n? 12
puts 'replacement screenshot'
screenshot
end
end
end
def exist?
File.exist? filepath(1)
end
def filepath(i)
File.join @dir, "#{Date.today.to_s}.#{i}.png"
end
def screenshot
`screencapture -x #{filepath(1)} #{filepath(2)}`
end
def idletime
`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'`.to_f
end
def one_in_n?(n)
rand < (1.0 / n.to_f)
end
end
App.new.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment