Skip to content

Instantly share code, notes, and snippets.

@gperezmz
Created April 6, 2014 17:54
Show Gist options
  • Save gperezmz/10009376 to your computer and use it in GitHub Desktop.
Save gperezmz/10009376 to your computer and use it in GitHub Desktop.
OSX – Save screenshots to path and share them with your own domain.
#!/usr/bin/env ruby
require "clipboard"
require "terminal-notifier"
require "net/http"
require "optparse"
require "securerandom"
class Options
def self.parse ( input )
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "usage: #{$0.split("/").last} [options]"
options[:params] = []
opts.on( '-f', '--fullscreen', 'Both screens' ) do
options[:params] = [ "-R0,0,3840,1080" ]
end
opts.on( '-m', '--fullscreen-main', 'Only main display' ) do
options[:params] = [ "-m" ]
end
opts.on( '-s', '--fullscreen-secondary', 'Only secondary display' ) do
options[:params] = [ "-R1920,0,3840,1080" ]
end
opts.on( '-a', '--area', 'Select an area of the screen' ) do
options[:params] = [ "-i" ]
end
opts.on( '-w', '--window', 'Select the current window' ) do
options[:params] = [ "-w", "-W" ]
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
begin
optparse.parse!(input)
rescue OptionParser::InvalidOption => e
puts e
puts optparse
exit(1)
end
return options
end
end
class Screenshot
def initialize ( input )
@options = input[:params]
@path = "/Volumes/Shared/Public/Screenshots"
@url = "http://i.shiror.in"
end
def notify ( *args )
TerminalNotifier.notify(
args.shift,
:title => 'Screenshot',
:open => args.shift,
:sender => 'com.apple.Preview',
:sound => 'default'
) unless $stdout.isatty
end
def err ( input )
notify(input)
abort "\e[1;31m:: \e[1;39m#{input}\e[0m"
end
def random
rnd = ""
loop do
rnd = SecureRandom.hex(3) + ".png"
break unless File.exist?("#{@path}/#{rnd}")
end
return rnd
end
def is_up? ( input )
begin
url = URI.parse(input)
req = Net::HTTP.new(url.host, url.port)
res = req.request_head(url.path)
rescue
false
else
true if res.code == "200" or res.code == "403"
end
end
def clipboard ( input )
e = is_up?(input)
loop do
e = is_up?(input)
break if e
end
Clipboard.copy(input)
end
def start
err("#{@url} is down, please try later!") unless is_up?("#{@url}/")
filename = random
filepath = "#{@path}/#{filename}"
@options.push("#{filepath}")
system("screencapture", *@options)
err("Something went wrong, try again!") unless File.exist?(filepath)
urlpath = "#{@url}/#{filename}"
clipboard(urlpath)
notify("#{urlpath} has been uploaded!", "#{urlpath}")
end
end
if __FILE__ == $0
options = Options.parse(ARGV)
x = Screenshot.new(options)
x.start()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment