Skip to content

Instantly share code, notes, and snippets.

@gfontenot
Created January 24, 2011 16:43
Show Gist options
  • Save gfontenot/793493 to your computer and use it in GitHub Desktop.
Save gfontenot/793493 to your computer and use it in GitHub Desktop.
-- Tweet current page in Chrome via Cloud.app and Twitter for mac
-- cobbled together by @gfontenot
--
-- Using most of the original code from:
-- »Get ShortURL via Cloud.app«
-- by @ptujec
--
-- using following sources:
-- http://www.leancrew.com/all-this/2007/11/long-and-shortened-url-scripts/
-- by @drdrang
--
-- added truncateString() function
-- perl url encode routine - handles IDN and UTF-8 Chars)
-- by @cometbus (→ see http://twitter.com/cometbus/status/2023559489)
--
-- cloudapp implementation → http://www.pryde-design.co.uk/2010/06/cl-ly-url-shortener/
-- making use of keychain by @ptujec 2011-01-19
on run
try
tell application "Google Chrome"
set longURL to URL of active tab of first window
set the_title to title of active tab of first window
end tell
set shortURL to shortenURL(longURL)
-- set the clipboard to shortURL
set tweet to the_title & " - " & shortURL
tell application "Finder"
open location "twitter://post?message=" & tweet
end tell
on error e
my growlRegister()
growlNotify("Error", e, 2)
end try
end run
-------------------------------------------
on handle_string(longURL)
try
set shortURL to shortenURL(longURL)
set the clipboard to shortURL
my growlRegister()
growlNotify("Get ShortURL", longURL & "
→ " & shortURL, 0)
on error e
my growlRegister()
growlNotify("Error", e, 2)
end try
end handle_string
-------------------------------------------
on shortenURL(longURL) -- via Cloudapp
tell application "Keychain Scripting"
set cloud_key to first generic key of current keychain whose name is "Cloud"
set Username to account of cloud_key
set Pword to password of cloud_key
end tell
--
set curlCMD to "curl --digest -u '" & Username & ":" & Pword & "' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{ \"item\": { \"name\": \"" & longURL & "\", \"redirect_url\": \"" & longURL & "\" } }' http://my.cl.ly/items"
set shortURL to (do shell script curlCMD)
set oset to offset of "http://cl.ly/" in shortURL
set shortURL to text (oset) thru (oset + 16) of shortURL
return shortURL
end shortenURL
----------------------------------
on growlRegister()
tell application "GrowlHelperApp"
register as application "Tweet Current Page" all notifications {"Alert"} default notifications {"Alert"} icon of application "Launchbar.app"
end tell
end growlRegister
on growlNotify(grrTitle, grrDescription, grrPriority)
tell application "GrowlHelperApp"
notify with name "Alert" title grrTitle description grrDescription priority grrPriority application name "Tweet Current Page"
end tell
end growlNotify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment