Created
February 2, 2016 13:47
-
-
Save julik/d96c7c680e29603052a1 to your computer and use it in GitHub Desktop.
Automatically upload screenshots to your S3 bucket and copy the link to the clipboard
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 | |
require 'bundler' | |
Bundler.setup | |
require 'aws-sdk' | |
require 'rb-fsevent' | |
require 'terminal-notifier' | |
require 'clipboard' | |
# ENV['AWS_ACCESS_KEY_ID'] = # | |
# ENV['AWS_SECRET_ACCESS_KEY'] = # | |
# ENV['AWS_BUCKET_NAME'] = 'julik-screenshots' | |
# ENV['AWS_REGION'] = 'eu-central-1' | |
WATCH_PATTERN = '/Users/julik/Desktop/Screen Shot *.*' | |
def upload_screenshot(screenie_path) | |
return unless File.exist?(screenie_path) && File.size(screenie_path) > 0 | |
b = Aws::S3::Bucket.new(ENV.fetch('AWS_BUCKET_NAME')) | |
s3_obj = b.object(File.basename(screenie_path)) | |
File.open(screenie_path, 'rb') do |f| | |
s3_obj.put(body: f, acl: 'public-read', storage_class: 'REDUCED_REDUNDANCY') | |
end | |
public_url = s3_obj.public_url | |
Clipboard.copy(public_url) | |
TerminalNotifier.notify('Screenshot path copied', :open => public_url) | |
end | |
require 'rb-fsevent' | |
fsevent = FSEvent.new | |
fsevent.watch(File.dirname(WATCH_PATTERN), :file_events => true) do |paths| | |
screenies = paths.select{|path| File.fnmatch(WATCH_PATTERN, path) && File.exist?(path) } | |
screenies.each do | screenie_path | | |
pid = fork { upload_screenshot(screenie_path) } | |
Process.wait(pid) | |
end | |
end | |
fsevent.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment