How to create a tiny URL with LB (incl. paste to clipboard)

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

How to create a tiny URL with LB (incl. paste to clipboard)

Post by ptujec » Sat Feb 07, 2009 12:46 pm

I found a nice applescript with shortens URLs. You could use it like it is and it will work just fine. (You will find the original here: http://www.leancrew.com/all-this/2007/1 ... l-scripts/)

But than you are kind of limited to just one browser. I'm rather free to pick the URL from whereever it is with "Instand Send".
But this is up to you. If you just run the script it will pick the URL of the front window in Safari (or in the browser of your choice).
I also added Growl support. Like it better than beep.
This script uses is.gd to shorten the URL.

Enough words. Here is my script and how to use it:

1. Copy the following code

Code: Select all

-- based on http://www.leancrew.com/all-this/2007/11/long-and-shortened-url-scripts/ 
-- & http://www.leancrew.com/all-this/2009/02/url-shortening-scripts-fixed-i-think/
-- by @drdrang
-- modified by @ptujec (for LaunchBar)
-------------------------------------------

on handle_string(longURL)
   
   set escapedURL to URLescape(longURL)
   set cmd to "curl 'http://is.gd/api.php?longurl=" & escapedURL & "'"
   set shortURL to do shell script cmd
   set the clipboard to shortURL as text
   my growlRegister()
   growlNotify("URL shortened", longURL & "\n" & "→ " & shortURL)
   
end handle_string

-------------------------------------------

on run
   
   tell application "Safari"
      set longURL to URL of front document
   end tell
   
   set escapedURL to URLescape(longURL)
   set cmd to "curl 'http://is.gd/api.php?longurl=" & escapedURL & "'"
   set shortURL to do shell script cmd
   set the clipboard to shortURL as text
   my growlRegister()
   growlNotify("URL shortened", longURL & "\n" & "→ " & shortURL)
   
end run

-------------------------------------------

on URLescape(longURL)
   set cmd to "\nfrom urllib import quote\nprint quote(\"\"\"" & longURL & "\"\"\", \"/:\")\n"
   return (do shell script "python -c " & (quoted form of cmd))
end URLescape

-------------------------------------------

using terms from application "GrowlHelperApp"
   on growlRegister()
      tell application "GrowlHelperApp"
         register as application "is.gd" all notifications {"Alert"} default notifications {"Alert"} icon of application "Launchbar.app"
      end tell
   end growlRegister
   on growlNotify(grrTitle, grrDescription)
      tell application "GrowlHelperApp"
         notify with name "Alert" title grrTitle description grrDescription application name "is.gd"
      end tell
   end growlNotify
end using terms from


2. Open Script Editor ("/Applications/AppleScript/Script Editor.app")

3. Paste the code into the empty window of Script Editor. Save it as i.g. "tinyURL" in the LaunchBar Actions Folder ("~/Library/Application Support/LaunchBar/Actions")

4. Restart LaunchBar

5. Pick a URL hit space and start typing "tiny..." / or just run the script and it will pick the URL of the front window in Safari (or in the browser of your choice)

Enjoy!
Last edited by ptujec on Tue Feb 17, 2009 4:14 pm, edited 4 times in total.

norbert
Objective Development
Objective Development
Posts: 648
Joined: Thu Nov 09, 2006 6:30 pm

Re: How to create a TinyURL with LB (incl. paste to clipboar

Post by norbert » Mon Feb 09, 2009 5:37 pm

ptujec wrote:4. Restart LaunchBar

That's not necessary. LaunchBar automatically detects new actions that are saved to the ~/Library/Application Support/LaunchBar/Actions folder.

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

Post by ptujec » Sun Feb 15, 2009 10:47 pm

This is kind of a one-more-thing: http://is.gd/jHzD

Now you can combine this with the tweet script (http://is.gd/f3pj)!

Create a tiny URL and tweet it in just 3 or 4 steps:

1) select URL with LaunchBar (Instant Send)
2) hit "tt"
3) ... add some additional words to the tweet
4) hit ↩

or:
1) run "TinyAndTweet" script (it will take the URL in Safaris frontwindow)
3) ... add some additional words to the tweet
4) hit ↩

wbro
Posts: 1
Joined: Wed Apr 22, 2009 3:26 pm

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by wbro » Wed Apr 22, 2009 3:33 pm

Thanks for this great tip ptujec. I generally prefer the tinyURL service, which has a similar API. It seems to work a little faster too. Here's the URL that can be swapped out...

Instead of...
http://is.gd/api.php?longurl=

Put In...
http://tinyurl.com/api-create.php?url=

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

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by ptujec » Wed Apr 22, 2009 4:20 pm

wbro wrote:Thanks for this great tip ptujec. I generally prefer the tinyURL service, which has a similar API. It seems to work a little faster too. Here's the URL that can be swapped out...

Instead of...
http://is.gd/api.php?longurl=

Put In...
http://tinyurl.com/api-create.php?url=


No problem. tinyURL seems to have trouble with googlemaps in this combination (e.g. http://is.gd/tSm0) Thats why I choose is.gd. You can also try tr.im or cli.gs or one of the thousand other services. You just have to find the API and an URL that looks similar to these below.

Code: Select all

http://api.tr.im/api/trim_simple?url=

or

Code: Select all

http://cli.gs/api/v1/cligs/create?url=

or

Code: Select all

http://bit.ly/api?url=

or

Code: Select all

http://skrci.me/api?url=
Last edited by ptujec on Mon Dec 07, 2009 11:39 pm, edited 2 times in total.

destiny
Posts: 1
Joined: Mon Apr 27, 2009 5:00 pm

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by destiny » Mon Apr 27, 2009 5:03 pm

Hi,
well we can make the URL if we have knowledge about that who to make the URL and which type of coding should be started.



http://www.dataentrypak.com

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

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by ptujec » Thu Jun 04, 2009 10:06 am

Here is a new version by @cometbus (http://twitter.com/cometbus/status/2023559489).
Its using tr.im. But it works fine with is.gd and others too (see above). See notes in the script for detailed changes.

Code: Select all

-- Use tr.im as a url shortner for LaunchBar
-- http://twitter.com/cometbus

-- 01:17:55 PM [05/31/2009]   
-- Initial script

-- 02:01:19 PM [05/31/2009]   
-- Added in truncateString() function
-- Added in urlencodetext() function

-- 04:08:48 PM [06/01/2009]   
-- Added in ability to dynamically detect Safari or WebKit
-- Created shortenURL() to stop code duplication
-- Stuffed the growl strings into growlNotify(), in order to avvoid
--    being annoyed by AppleScript needing carriage returns in source code

-- 05:40:38 PM [06/03/2009]   
-- tr.im decicded to unbloc me, more than likley just time passed
--      Not sure if using tr.im is even wise anymore, if they can not
--      support basic email requests
-- Went with a perl url encode routine, now handles IDN and UTF-8 Chars

-- Notes
-- This is slow at times, find out why
--      probably fixed by removing the python call and going native
-- I would like to not register with growl ever time, only the first time
-- When the url shortener goes down, this script locks up, as does LaunchBar
--       need to add --timeout settings to curl, and alternately return
--       a message that will tell the end user
-- Considering not using tr.im since they suck donkey balls

-- Bugs
-- There is a bug in the perl url encode in that a "'" will break it
-- Something is Safari is not forground, can break it

-- based on http://www.leancrew.com/all-this/2007/11/long-and-shortened-url-scripts/
-- & http://www.leancrew.com/all-this/2009/02/url-shortening-scripts-fixed-i-think/
-- by @drdrang
-- modified by @cometbus (for LaunchBar)


-- So you can use LaunchBar and paste in a url,
-- which will return a shortened one
on handle_string(longURL)
   set shortURL to shortenURL(longURL)
   set the clipboard to shortURL as text
   my growlRegister()
   set longURLDisplay to truncateString(longURL, 30)
   growlNotify(longURLDisplay, shortURL)
end handle_string

-- When just running, this will take frontmost browser url,
-- and return the shortened url
on run
   -- Get the users default browser, this will help Safari vs Webkit issues,
   -- not going to work with non webkit based browsers
   set _browser to GetDefaultWebBrowser()
   
   using terms from application "Safari"
      tell application _browser
         set longURL to URL of front document
      end tell
   end using terms from
   
   set shortURL to shortenURL(longURL)
   set the clipboard to shortURL as text
   my growlRegister()
   set longURLDisplay to truncateString(longURL, 30)
   growlNotify(longURLDisplay, shortURL)
end run

-- Given a long url, escapes it, and shoves it on the clipboard
on shortenURL(longURL)
   set escapedURL to urlencode(longURL)
   set cmd to "curl 'http://api.tr.im/api/trim_simple?url=" & escapedURL & "'"
   set shortURL to do shell script cmd
   return shortURL
end shortenURL

-- Encode the url
on urlencode(theText)
   set _scpt to "perl -MURI::Escape -e \"print uri_escape('" & theText & "');\""
   return do shell script _scpt
end urlencode


-- Truncate string, this is so I do not have to pass
-- a big huge unreasable string to growl which no one reads anyway
-- Simply makes the string as long as the len var
on truncateString(inString, len)
   if length of inString < len then
      return inString
   else
      return text 1 thru len of inString & "..."
   end if
end truncateString

-- growl is in the house
using terms from application "GrowlHelperApp"
   on growlRegister()
      tell application "GrowlHelperApp"
         register as application "tr.im" all notifications {"Alert"} default notifications {"Alert"} icon of application "Launchbar.app"
      end tell
   end growlRegister
   on growlNotify(orig_url, short_url)
      
      set growl_description to orig_url & "\n" & "→ " & short_url
      
      tell application "GrowlHelperApp"
         notify with name "Alert" title "URL shortened" description growl_description application name "tr.im"
      end tell
   end growlNotify
end using terms from

-- Supporting function for determining your default browser
-- Taken from http://daringfireball.net/2009/01/applescripts_targetting_safari_or_webkit
on GetDefaultWebBrowser()
   set _scpt to "perl -MMac::InternetConfig -le " & ¬
      "'print +(GetICHelper \"http\")[1]'"
   return do shell script _scpt
end GetDefaultWebBrowser

Forumposter
Posts: 18
Joined: Wed Jul 29, 2009 7:56 pm

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by Forumposter » Mon Aug 03, 2009 5:25 pm

Can it be that RC3 has this function already implemented?

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

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by ptujec » Mon Aug 03, 2009 6:58 pm

Forumposter wrote:Can it be that RC3 has this function already implemented?

Yes and no. LaunchBar has a similar action implemented ... actually two. But you have to give it the string/paste the URL. Here you can but you don't have to give it the URL as a string. If you just run it it will take the URL of the front most window. Plus you have a growl notification which gives you a feedback of what have happend.

norbert
Objective Development
Objective Development
Posts: 648
Joined: Thu Nov 09, 2006 6:30 pm

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by norbert » Sat Oct 31, 2009 7:26 pm

ptujec wrote:But you have to give it the string/paste the URL. Here you can but you don't have to give it the URL as a string. If you just run it it will take the URL of the front most window.


Note that you can conveniently send the current URL to LaunchBar via Instant Send. For example, if you are in Safari:

  1. Press ⌘L to select the current URL
  2. Press and hold ⌘Space to send it to LaunchBar

Now you may select any target action (for example Make TinyURL) and hit Return to process the URL with that action.

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

Re: How to create a tiny URL with LB (incl. paste to clipboard)

Post by ptujec » Mon Dec 07, 2009 11:16 pm

Update: This version uses Large Display with sound and autohide instead of growl notification. Should therefore be a bit faster. After the Large Display you simply can copy the short url wherever you need it.

Code: Select all

-- »Get ShortURL«
-- 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)

on run
   
   tell application "Safari"
      set longURL to URL of front document
   end tell
   
   set shortURL to shortenURL(longURL)
   
   set the clipboard to shortURL
   
   tell application "LaunchBar"
      display in large type longURL & "\n→ " & shortURL as text with sound "Pop"
      delay 2
      hide
   end tell
   
end run

-------------------------------------------
on handle_string(longURL)
   
   set shortURL to shortenURL(longURL)
   set the clipboard to shortURL
   
   tell application "LaunchBar"
      display in large type longURL & "\n→ " & shortURL as text with sound "Pop"
      delay 2
      hide
   end tell
   
end handle_string

-------------------------------------------
on shortenURL(longURL)
   set escapedURL to urlencode(longURL)
   set cmd to "curl http://bit.ly/api?url=" & escapedURL
   set shortURL to do shell script cmd
   return shortURL
end shortenURL

-------------------------------------------
on urlencode(longURL)
   set _scpt to "perl -MURI::Escape -e \"print uri_escape('" & longURL & "');\""
   return do shell script _scpt
end urlencode

Post Reply