Created
December 4, 2012 21:30
-
-
Save rfunduk/4208911 to your computer and use it in GitHub Desktop.
Auto-scp screenshots to a webserver and copy the url to your clipboard (on OSX)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# autoshot.rb | |
# | |
# Ryan Funduk (ryanfunduk.com) | |
# Licensed under WTFPL (http://sam.zoy.org/wtfpl) | |
# | |
# Watch a directory and scp new files to a host that | |
# is running a webserver. Only works on OSX, could be | |
# made to work on other platforms with alternatives | |
# to rb-fsevent and scp. | |
# | |
# Writes a list of already processed files directly | |
# into this script (yes, *this file*). | |
# | |
# Warning: | |
# This script will overwrite your clipboard when | |
# you take a screenshot! | |
# | |
# Dependencies: | |
# ruby (only tested on 1.9.3) | |
# rb-fsevent (only tested with 0.9.2) | |
# | |
# Usage: | |
# Configure OSX to place screenshots somewhere out of the way: | |
# defaults write com.apple.screencapture location '~/Pictures/Screenshots' | |
# killall SystemUIServer | |
# | |
# Change the values below in this script such that: | |
# | |
# 'scp' is an scp(1) compatible target | |
# e.g `user@host:/var/www/mysite/img/screenshots` | |
# 'url' is the hostname the files will be available at | |
# e.g `http://mysite.com/img/screenshots` | |
# 'watch_dir' is the directory to watch | |
# e.g `~/Pictures/Screenshots` | |
# | |
# Now run it either plainly: | |
# ruby autoshot.rb | |
# Files not already processed will be uploaded | |
# (so be careful because your clipboard will only | |
# get the last one) | |
# | |
# Or 'daemonize', set 'debug' to false in CONFIG, and: | |
# ruby autoshot.rb & | |
# :) | |
require 'rb-fsevent' | |
require 'digest/sha1' | |
CONFIG = { | |
debug: true, | |
scp: "user@host:/some/path", | |
url: "http://example.com/some/path", | |
watch_dir: File.expand_path("~/Pictures/Screenshots") | |
} | |
$info_pos = DATA.pos | |
def sync(*args) | |
DATA.reopen( __FILE__, 'r' ) | |
DATA.seek $info_pos | |
processed = DATA.read.split("\n").reject { |f| f.strip.length == 0 } | |
all = Dir.glob( File.join(CONFIG[:watch_dir], '*') ) | |
(all - processed).each do |file| | |
filename = File.basename( file ) | |
hash = Digest::SHA1.hexdigest( filename ) | |
ext = File.extname( file ) | |
new_name = "#{hash}#{ext}" | |
print "Processing #{filename}... " if CONFIG[:debug] | |
`scp '#{file}' #{CONFIG[:scp]}/#{new_name}` | |
`echo #{CONFIG[:url]}/#{new_name} | pbcopy` | |
print "DONE\n" if CONFIG[:debug] | |
processed << file | |
end | |
DATA.reopen( __FILE__, 'a+' ) | |
DATA.truncate( $info_pos ) | |
DATA.puts processed.join("\n") | |
DATA.flush | |
end | |
puts "Started..." if CONFIG[:debug] | |
sync() | |
watcher = FSEvent.new | |
watcher.watch( | |
CONFIG[:watch_dir], | |
latency: 1, no_defer: true, | |
&method(:sync) | |
) | |
watcher.run | |
puts "Quitting..." if CONFIG[:debug] | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment