#!/usr/bin/env ruby # This script can use some improving... require 'optparse' require 'open3' TIME = Time.now DATE_PATH = TIME.strftime '%Y/%m/%d' LOCAL = '/tmp' FILENAME = "#{TIME.strftime '%Y-%m-%d_%H-%M-%S'}.png" REMOTE = 'scrotify' # In ~/.ssh/config TARGET = "/var/www/files.sharparam.com/public_html/#{DATE_PATH}" URL = "http://files.sharparam.com/#{DATE_PATH}/#{FILENAME}" options = { mode: :all } # Apparently the keyring variables cannot be accessed from scripts that run # from i3's exec tool, so we have to manually set the socket here. sock = `gpgconf --list-dirs agent-ssh-socket`.strip ENV['SSH_AUTH_SOCK'] = sock OptionParser.new do |opts| opts.banner = "Usage: #{$0} [options]" opts.on('-s', '--select', 'Screenshot region') do options[:mode] = :select end opts.on('-a', '--active', 'Screenshot active window') do options[:mode] = :active end end.parse! def error(msg) `notify-send -u critical "Scrotify" "#{msg}"` abort msg end def maim(args = '') `maim -u #{args} #{LOCAL}/#{FILENAME}` error 'maim failed' unless $?.exitstatus == 0 `notify-send -u low "Scrotify" "Uploading screenshot..."` `ssh -q #{REMOTE} mkdir -p #{TARGET}/` error 'ssh mkdir failed' unless $?.exitstatus == 0 `rsync -q #{LOCAL}/#{FILENAME} #{REMOTE}:#{TARGET}/` error 'rsync failed' unless $?.exitstatus == 0 `rm #{LOCAL}/#{FILENAME}` Open3.popen2('xclip -sel c') { |i, _, _| i.print(URL); i.close; } `notify-send -u normal "Scrotify" "#{URL}"` end def capture(mode = :all) case mode when :select region = `slop -f %g`.strip maim "-g #{region}" when :active `xdotool getactivewindow getwindowgeometry` =~ /(\d+),(\d+).+?(\d+)x(\d+)/m maim "-g #{$3}x#{$4}+#{$1}+#{$2}" else maim end end capture options[:mode]