Skip to content

Instantly share code, notes, and snippets.

@deguchi
Created June 17, 2013 06:00
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 deguchi/5794882 to your computer and use it in GitHub Desktop.
Save deguchi/5794882 to your computer and use it in GitHub Desktop.
Gyazoに、実行したら自動的にEvernoteに取り込む機能を追加。
#!/usr/bin/env ruby
require 'osx/cocoa'
include OSX
require 'net/http'
# 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) then
id = File.read(idfile).chomp
elsif File.exist?(old_idfile) then
id = File.read(old_idfile).chomp
end
# capture png file
tmpfile = "/tmp/image_upload#{$$}.png"
imagefile = ARGV[1]
if imagefile && File.exist?(imagefile) then
system "sips -s format png \"#{imagefile}\" --out \"#{tmpfile}\""
else
system "screencapture -i \"#{tmpfile}\""
if File.exist?(tmpfile) then
system "sips -d profile --deleteColorManagementProperties \"#{tmpfile}\""
end
end
if !File.exist?(tmpfile) then
exit
end
imagedata = File.read(tmpfile)
File.delete(tmpfile)
# upload
boundary = '----BOUNDARYBOUNDARY----'
HOST = 'gyazo.com'
CGI = '/upload.cgi'
UA = 'Gyazo/1.0'
data = <<EOF
--#{boundary}\r
content-disposition: form-data; name="id"\r
\r
#{id}\r
--#{boundary}\r
content-disposition: form-data; name="imagedata"; filename="gyazo.com"\r
\r
#{imagedata}\r
--#{boundary}--\r
EOF
header ={
'Content-Length' => data.length.to_s,
'Content-type' => "multipart/form-data; boundary=#{boundary}",
'User-Agent' => UA
}
Net::HTTP.start(HOST,80){|http|
res = http.post(CGI,data,header)
url = res.response.to_ary[1]
system "echo -n #{url} | pbcopy"
system "open #{url}"
cacheurl = url.sub(/gyazo/,"cache.gyazo")
cmd = <<"EOS"
tell application "Evernote"
-- create, rename, and delete notebooks
if (not (notebook named "Gyazo" exists)) then
-- NOTE also check out the "create notebook" command
make notebook with properties {name:"Gyazo"}
end if
set gyazonote to notebook "Gyazo"
-- create note title "Note 1" with text "Here is my new text note" notebook gyazonote
create note title "Gyazo image" from url "#{cacheurl}.png" notebook gyazonote
end tell
EOS
script = NSAppleScript.alloc.initWithSource(NSString.stringWithCString(cmd))
desc = script.executeAndReturnError(nil)
# save id
newid = res.response['X-Gyazo-Id']
if newid and newid != "" then
if !File.exist?(File.dirname(idfile)) then
Dir.mkdir(File.dirname(idfile))
end
if File.exist?(idfile) then
File.rename(idfile, idfile+Time.new.strftime("_%Y%m%d%H%M%S.bak"))
end
File.open(idfile,"w").print(newid)
if File.exist?(old_idfile) then
File.delete(old_idfile)
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment