URL Shortening via Cloud.app API (keychain support)

Sharing and discussing custom actions for LaunchBar
Post Reply
ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

URL Shortening via Cloud.app API (keychain support)

Post by ptujec » Wed Jan 19, 2011 12:51 pm

Though you can send files to Cloud.app via LaunchBar its not possible to send URLs. That's where this script comes in handy.
It uses the login data from your keychain. (Note: You will be asked to allow keychain access on first use. Also the script doesn't work if your privacy settings are set to "long". You can change that in my.cl.ly/​account#privacy /Thx to @GFontenot for the note)

How to install/use?
1. Copy the following code (Select all, ⌘-C).

Code: Select all

-- »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 "Safari"
         set longURL to URL of front document
      end tell
      
      set shortURL to shortenURL(longURL)
      
      set the clipboard to shortURL
      
      my growlRegister()
      growlNotify("Get ShortURL", longURL & "\n \n → " & shortURL, 0)
      
   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 & "\n \n → " & 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 "Get ShortURL" all notifications {"Alert"} default notifications {"Alert"} icon of application "Cloud.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 "Get ShortURL"
   end tell
end growlNotify

2. Open AppleScript-Editor ("/Applications/Utilities/AppleScript Editor.app"). Paste the code into the empty window of AppleScript-Editor. Save it as e.g. "Get ShortURL" in the LaunchBar Actions Folder ("~/Library/Application Support/LaunchBar/Actions")

3. Now select the script in LaunchBar. Just start typing "Get Sho...", hit enter. The script will take the URL from Safaris frontmost window and will shorten it with Cloudapp. (You can also use an URL from about anywhere and send it with Instant Send to LaunchBar (press and hold ⌘-space), start typing "Get Sho..." …)
Last edited by ptujec on Thu Jan 20, 2011 9:45 pm, edited 2 times in total.

gfontenot
Posts: 1
Joined: Thu Jan 20, 2011 7:29 pm

Re: URL Shortening via Cloud.app API (keychain support)

Post by gfontenot » Thu Jan 20, 2011 7:31 pm

--EDIT--

After some googling, it seems that I needed to set my default privacy settings to "Public" (as seen here: http://cl.ly/47il). Now it works fine.

--END EDIT --

Not working for me. Bookmarking this page gives me this link: http://cl.ly/2D2H, which is obviously not a link to this page.

derdiplomat
Rank 1
Rank 1
Posts: 28
Joined: Wed Apr 23, 2008 8:45 pm

Re: URL Shortening via Cloud.app API (keychain support)

Post by derdiplomat » Sun Feb 06, 2011 11:44 am

For me it says "The variable longURL is not defined" when I try to trigger the script.

Using QuickSend there's another error:
"Can't get text 0 thru 16 of "HTTP Digest: Access denied"



Public links are actived - I'd love to use this - any tips?

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: URL Shortening via Cloud.app API (keychain support)

Post by ptujec » Sun Feb 06, 2011 12:32 pm

derdiplomat wrote:For me it says "The variable longURL is not defined" when I try to trigger the script.

Using QuickSend there's another error:
"Can't get text 0 thru 16 of "HTTP Digest: Access denied"



Public links are actived - I'd love to use this - any tips?

1) On run its looking for the URL in Safaris frontmost window/tab. Are you using a different browser?
2) No clue? Works fine here

derdiplomat
Rank 1
Rank 1
Posts: 28
Joined: Wed Apr 23, 2008 8:45 pm

Re: URL Shortening via Cloud.app API (keychain support)

Post by derdiplomat » Wed Feb 09, 2011 1:23 am

Uhm. Yes. I'm on Google's chromey Chrome here.
I wasn't aware that this script is browser depended.
I guess there is no way I change a line of code and
it miraculously works with Chrome?

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: URL Shortening via Cloud.app API (keychain support)

Post by ptujec » Wed Feb 09, 2011 1:42 am

derdiplomat wrote:Uhm. Yes. I'm on Google's chromey Chrome here.
I wasn't aware that this script is browser depended.
I guess there is no way I change a line of code and
it miraculously works with Chrome?

Try …

Code: Select all

tell application "Google Chrome"
   set longURL to URL of active tab of window 1
end tell

instead of …

Code: Select all

tell application "Safari"
         set longURL to URL of front document
      end tell

derdiplomat
Rank 1
Rank 1
Posts: 28
Joined: Wed Apr 23, 2008 8:45 pm

Re: URL Shortening via Cloud.app API (keychain support)

Post by derdiplomat » Wed Feb 09, 2011 7:45 pm

Thanks for the help ptujec, I must appeal very lazy, but I didn't see the 'tell application "Safari" ' part on the iPhone.

At home I went ahead and tried it out, it almost worked perfectly.

I get the "cl.ly" appended to the front of every URL I shrink. This is the URL I get for this page:

http://cl.lyforums.obdev.at/viewtopic.php?f=24&t=5369&p=17539#p17539


I tried some stupid stuff, but it ain't worked. Any ideas are welcome for this lonesome
Chrome owner.

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: URL Shortening via Cloud.app API (keychain support)

Post by ptujec » Wed Feb 09, 2011 7:49 pm

So your script looks like that:

Code: Select all

-- »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 window 1
      end tell
      
      set shortURL to shortenURL(longURL)
      
      set the clipboard to shortURL
      
      my growlRegister()
      growlNotify("Get ShortURL", longURL & "\n \n → " & shortURL, 0)
      
   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 & "\n \n → " & 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 "Get ShortURL" all notifications {"Alert"} default notifications {"Alert"} icon of application "Cloud.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 "Get ShortURL"
   end tell
end growlNotify

What about the keychain entry?

derdiplomat
Rank 1
Rank 1
Posts: 28
Joined: Wed Apr 23, 2008 8:45 pm

Re: URL Shortening via Cloud.app API (keychain support)

Post by derdiplomat » Wed Feb 09, 2011 9:16 pm

It works. Out of habit I copy&pasted the URL before triggering the script.
That has caused this behavior.

So the script works on Chrome if you have no hyperlink in your Clipboard already.
I guess that's good enough for me for now. Many thanks for helping me out.

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: URL Shortening via Cloud.app API (keychain support)

Post by ptujec » Wed Feb 09, 2011 9:19 pm

derdiplomat wrote:It works. Out of habit I copy&pasted the URL before triggering the script.
That has caused this behavior.

So the script works on Chrome if you have no hyperlink in your Clipboard already.
I guess that's good enough for me for now. Many thanks for helping me out.


It should also work if you send an url to the action. Select URL - Tab - Script...

derdiplomat
Rank 1
Rank 1
Posts: 28
Joined: Wed Apr 23, 2008 8:45 pm

Re: URL Shortening via Cloud.app API (keychain support)

Post by derdiplomat » Wed Feb 09, 2011 9:45 pm

You are right - I just had to omit QuickSend when copying an URL.

Zettt
Rank 2
Rank 2
Posts: 45
Joined: Sat Mar 28, 2009 1:04 pm
Location: Stuttgart
Contact:

Re: URL Shortening via Cloud.app API (keychain support)

Post by Zettt » Mon Aug 18, 2014 8:53 am

Hey everyone,

Just did some quick updates to this script, because I needed some parts of it. This solution now uses

Code: Select all

security
. I've deleted Growl and made some fancier clipboard checking for a URL at the beginning of this script.

PLEASE edit your username!

Code: Select all

-- »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
-- Updated to use `security` command, thrown out Growl,
-- a little fancier clipboard checking by @zettt 2014-08-18

on run
   
   try
      if (the clipboard) starts with "http" or (the clipboard) starts with "https" then
         set longURL to the clipboard as string
      else
         tell application "Safari"
            set longURL to URL of front document
         end tell
      end if
      
      set shortURL to shortenURL(longURL)
      
      set the clipboard to shortURL
      
      display notification shortURL
      
   on error e
      display notification e
   end try
   
end run

-------------------------------------------
on handle_string(longURL)
   
   try
      set shortURL to shortenURL(longURL)
      set the clipboard to shortURL
      
      display notification shortURL
      
   on error e
      display notification e
   end try
   
end handle_string

-------------------------------------------
on shortenURL(longURL) -- via Cloudapp
   
   set Username to "YOURUSER@DOMAIN.TLD"
   set Pword to do shell script "security find-generic-password -w -l 'CloudApp' -a '" & Username & "'"
   --log "Username: " & Username & " Password: " & Pword
   
   set cmd 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 cmd) as string
   set oset to offset of "http://cl.ly/" in shortURL
   set shortURL to text (oset) thru (oset + 24) of shortURL
   return shortURL
end shortenURL

Post Reply