Skip to content

Instantly share code, notes, and snippets.

@raws
Created March 4, 2010 16:24
Show Gist options
  • Save raws/321850 to your computer and use it in GitHub Desktop.
Save raws/321850 to your computer and use it in GitHub Desktop.
exit if ARGV.empty?
require 'timeout'
# Upload files to Hey.
uploaded = Array.new
ARGV.each do |f|
next unless File.exists?(f)
type = case File.extname(f).downcase
when '.jpg', '.jpeg' then 'image/jpeg'
when '.gif' then 'image/gif'
when '.png' then 'image/png'
else next; end
begin
Timeout::timeout(30) do
response = `curl -s -m 30 -F "userfile=@#{f};type=#{type}" -F "format=json" hey.czechmyjugs.com`
if response =~ /"success":\["(\S+?)"\]/ then uploaded << $1.gsub('\/', '/') end
end
rescue; end
end
# Copy URLs of successful uploads to clipboard.
`/bin/echo -n '#{uploaded.inject('') { |clipboard, url| clipboard << "#{url}\n" }.chomp}' | pbcopy`
# Display Growl notification if Growl is installed.
growl = unless uploaded.empty?
{ :title => "File#{ARGV.size == 1 ? '' : 's'} uploaded", :description => uploaded.inject('') { |files, file| files << "#{File.basename(file).chomp}\n" } }
else
{ :title => "Upload failure", :description => "An error occurred while attempting to send your file#{ARGV.size == 1 ? '' : 's'} to Hey." }
end
applescript = %{tell application "System Events"
set isGrowlRunning to (count of (every process whose name is "GrowlHelperApp")) > 0
end tell
if isGrowlRunning then
tell application "GrowlHelperApp"
set the allNotificationsList to {"Upload Status"}
set the enabledNotificationsList to {"Upload Status"}
register as application "Hey Service" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Finder"
notify with name "Upload Status" title "#{growl[:title]}" description "#{growl[:description]}" application name "Hey Service"
end tell
end if}
`osascript -e '#{applescript}'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment