Skip to content

Instantly share code, notes, and snippets.

@riocampos
Created June 30, 2014 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riocampos/c10934d50524fe397f09 to your computer and use it in GitHub Desktop.
Save riocampos/c10934d50524fe397f09 to your computer and use it in GitHub Desktop.
/Applications/Gyazo2twitter.app/Contents/Resources/script (contents: http://d.hatena.ne.jp/riocampos+tech/20140630/Gyazo_direct_upload_to_twitter, original: http://blog.kksg.net/posts/gyazo-twitter )
#!/usr/bin/env ruby
require 'net/http'
require 'twitter'
client = Twitter::REST::Client.new(
consumer_key: "your-consumer-key",
consumer_secret: "your-consumer-secret",
access_token: "your-access-token",
access_token_secret: "your-access-token-secret",
)
# get id
user = IO.popen("whoami", "r+").gets.chomp
program = ARGV[0].to_s
idfile = "/Users/#{user}/Library/Gyazo/id"
old_idfile = File.dirname(program) + "/gyazo.app/Contents/Resources/id"
id = if File.exist?(idfile)
File.read(idfile).chomp
elsif File.exist?(old_idfile)
File.read(old_idfile).chomp
end
# capture png file
tmpfile = "/tmp/image_upload#{$$}.png"
imagefile = ARGV[1]
sound_file = File.dirname(program) + "/shutter.aiff"
if imagefile && File.exist?(imagefile)
system %Q[sips -s format png "#{imagefile}" --out "#{tmpfile}"]
else
system %Q[screencapture -x -i "#{tmpfile}"]
if File.exist?(tmpfile)
system "afplay #{sound_file}"
system %Q[sips -d profile --deleteColorManagementProperties "#{tmpfile}"]
dpiWidth = `sips -g dpiWidth "#{tmpfile}" | awk '/:/ {print $2}'`
dpiHeight = `sips -g dpiHeight "#{tmpfile}" | awk '/:/ {print $2}'`
pixelWidth = `sips -g pixelWidth "#{tmpfile}" | awk '/:/ {print $2}'`
pixelHeight = `sips -g pixelHeight "#{tmpfile}" | awk '/:/ {print $2}'`
if (dpiWidth.to_f > 72.0 and dpiHeight.to_f > 72.0)
width = pixelWidth.to_f * 72.0 / dpiWidth.to_f
height = pixelHeight.to_f* 72.0 / dpiHeight.to_f
system %Q[sips -s dpiWidth 72 -s dpiHeight 72 -z #{height} #{width} "#{tmpfile}"]
end
end
end
unless File.exist?(tmpfile)
exit
end
File.open(tmpfile) { |f|
res = client.update_with_media('Screenshot by Gyazo #Gyazo', f)
display_url = res.media.first.display_url
url = "https://#{display_url}"
IO.popen("pbcopy","r+"){ |io|
io.write(url)
}
system "open #{url}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment