Bit.ly URL shortening with Mac OS X Snow Leopard Services and Applescript

by mtheoryx on October 26, 2009 · 1 comment

in tutorial

If you’ve seen the nifty browser shortcuts and extensions from Bit.ly then you’re familiar with the service, and how convenient it is. But there’s a catch… they only work in your browser.

Read on to see how you, too, can have the same convenience from any application on Mac OS X Snow Leopard.

For those with no inclination to follow along, I’ve provided the workflow below. Just download this, unzip it, and place it in ~/username/Library/Services. To use it, just select a URL in any application, right-click, and select “bitly”. The shortened URL will be on your clipboard in .22 seconds, ready for you to paste anywhere you like.

Grab the Service — Freebie!!

To start, we’re going to create a workflow with Automator. Locate it in Applications > Automator.app

At the dialog that appears when creating a new workflow, choose “Service” This will be placed in ~/username/Library/Services, and will be available by right-clicking (or control-click) > Services > thisWorkflow.

Go ahead and rename your workflow now by choosing File > Save as… > bitly

You’ll notice that by default, the top parameter reads that the services takes selected text in any application. We’re keeping that.

This means, contextually, the service doesn’t apply unless you have some text selected. Additionally, this will work in ANY application, unlike browser extensions and JavaScript bookmarks.

The first thing we’ll do, for testing purposes, is add an action called “Get Specified Text” located under the Text category.

The reason we want to do this is that we’re going to build in some exception handling, and we need some text to test. As it stands, running the workflow will throw an error because you can’t simulate selected text in the Automator application.

To start, just paste in any valid http or https URL. I’m going to use http://google.com/ for this example.

The next action we add is really the meat of the workflow. Under the Utilities category, drag in the “Run AppleScript” action. Feel free to delete all of the dummy text at this point.

I’ll go ahead and paste in the script we’re going to use, and then I’ll run through what it does, line by line, skipping the comment header at the top.

The first line simply takes the input from the previous action’s output, and brings it into our AppleScript. You’ll notice the little arrow at the bottom of the “Get Specified Text” action connected to the rounded tab at the top of the “Run AppleScript” action. That means there is output from the first action flowing into the input of the next action — hence it’s a workflow.

The following line takes the input, casts it to a string class, and assigns it to another variable for our use.

We next choose to ignore the case of the string. This is generally a good idea when you’re going to test a string, unless case is important. We only want to test for the presence of “http” or “https” at the start of the string, to ensure it’s actually a URL. Feel free to get creative with additional Regular Expressions. For example, you can make sure there are no spaces or strange characters, and if so, URL-encode them. Moving on…

Here we’re doing a comparison. If our conditions are not met, we don’t want to utilize the bitly service. In my testing, the system sound is triggered as well. We don’t want to copy that to the clipboard though, which is why we’re not “returning” anything. We’ll get to that…

Now, after the else is what fires when our conditions are met. First, we’re going to build a string. The string parts are outlined as:

  • set curlCMD to :
    This just sets the string to a variable.
  • “curl :
    This is the command line program we’ll use to call bit.ly’s API
  • –stderr /dev/null :
    If we get an error back, route it to nowhere. You can add exception handling for this later if you’d like.
  • \”http://bit.ly/api :
    This is the URL of bit.ly’s api. Notice the backslash before the double quote. This is known as character escaping. It’s important here, so just put it in ;p
  • ?url=” :
    This is the start of the query string — do a Google search sometime and look at the URL in your browser. You’ve been using query strings for so long that you forgot what they were.
  • & input & “\”" :
    Here we concatenate the string of input we received in our script right onto the end, and add the closing double quotes.

Right, so now you understand what the URL is about. Now, remember, we have just built a command here as a string — we haven’t actually executed the command. That’s next…

Remember your order of operations from your math classes? You work on the part in the parenthesis first. This executes our command string on the Mac OS X command line. When we call this (hint: you can do it right in your browser too), bit.ly returns the shortened URL. We take this result, and assign it to the tinyURL variable.

Now, some housekeeping. Close your if statement, end your ignorning block, and end running the AppleScript. This completes your second Automator action in this workflow.

Now that we have input that we want, we have created a short URL from that input, we need to return the input. Let’s go ahead and do some testing before our last steps.

If you click the “Results” button at the bottom of your action window, you’ll see a drawer pop out. Now, go to the very top and click “Run” and see what happens.

See? You got a bit.ly shortened URL back. This is good, but it’s not a “real” test…

Remember the very first action? We specified the URL to use as input, and we made sure we put a good URL in there. Technically, we don’t know if the input is being tested correctly against our conditions, so we’ll do that next.

Change your URL in the “Get Specified Text” action to “ftp://google.com/

Now run your workflow and look at the results. You should hear an alert, and see an error diaglog that you have to dismiss. Now that we know our test is working, change the URL back to “http://google.com/” and we’ll finish up the workflow.

Under the Utilities category, drag in the “Copy to Clipboard” action. It expects text as input, and that’s just what we’ll give to it. Click the Results button on the “Copy to Clipboard” action. What you see is exactly what would have been placed on your clipboard if this workflow were live. Let’s make it live now…

Delete the “Get Specified Text” action by clicking the X in the upper right corner. You’ll notice that now you output from the very first action (the one that gets selected text in any application) and it’s going in as input into our “Run AppleScript” action.

Save your workflow, and place it in the ~/username/Library/Services folder. On OS X 10.6 Snow Leopard, this adds the workflow as a contextual service to the Services menu of all applications, or when you right click.

Now, go into any application, select the text for a URL. Right-click (or control-click) and select Services > bitly.

Now, wait for half a second (we are contacting bit.ly’s web API, so it’s not instant), and then just paste whatever’s on your clipboard into your browser. Go to the site, and you’ll see, it’s the URL that you originally copied. Neat, huh?

You can make the deal even sweeter by assigning a keyboard shortcut to your new service. Go to System Preferences > Keyboard > Keyboard Shortcuts > Services and find your “bitly” services. Pick whatever shortcut you want, but steer clear of any application- or system-level shortcuts to avoid collisions. I chose Control-Command-B for mine.

Practical applications? The biggest one I can think of is links in IMs and emails. Any links longer than 70 characters gets broken in Apple’s Mail.app.

If you enjoyed this article, I would like to hear your wishlist for similar articles. What would you like to learn about? What would you like to see broken down into visual step-by-step instructions? What are you interested in exploring? Let me know in the comments section below.


Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • Sphinn
  • del.icio.us
  • Tumblr
  • Twitter
  • Yahoo! Buzz

{ 1 trackback }

binWord/blog - Mac OS Xの「サービス」から、bit.lyを呼びだしてURLを短縮
November 23, 2009 at 5:09 am

Comments on this entry are closed.

Previous post:

Next post: