URL Shortener Service

I often send links to people through email or IM and once in a while the links are just too long to paste cleanly so I decided to create a URL shortening service using Automator in OS X. Below I’ll document how to create both a service to shorten a URL as well as a standalone app to get the job done when you’re in an application that doesn’t support OS X Services.

Below you’ll find to ways to shorten a URL using the bit.ly service. One way will simply replace a highlighted URL and the other will modify a URL that is in your clipboard so you can copy a full URL and then paste a shortened version.

URL Shortener Service
Many OS X applications support Services. As defined by OS X itself, “Services are contextual workflows available throughout Mac OS X. They accept text or files from the current application or the Finder. Services appear in the Services menu.” The service we’re about to create allows you to highlight a valid URL, right click it and convert it to a shortened URL.

You can build your own service using Automator which has been included with OS X since 10.4 (Tiger). To create the URL Shortener service do the following:

  • Go to bit.ly and create an account
  • Once your account is created, visit http://bit.ly/a/account and record your username and api key
  • Launch Automator
  • Create a new Service
  • Change “Service receives selected” to URL
  • Select “Replaces selected text”
  • Search for Script using the search tool in Automator and drag on the Run Shell Script action
  • Change the Shell: option to /usr/bin/python
  • Change the Pass input: option to “as arguments”

You’re now ready the paste the following code into the Run Shell Script action:

import sys
import urllib

# Enter your username and api key below by replacing what is
# in quotes.
# Get your own username and api key by creating an account at
# bit.ly and then going to http://bit.ly/a/account
login_user = "username"
api_key = "apikey"

# don't edit anything below here unless you know what you're doing

long_url = sys.argv[1]
long_url = long_url.strip()

try:

	longUrl = urllib.urlencode(dict(longUrl=long_url))
        login = urllib.urlencode(dict(login=login_user))
	apiKey = urllib.urlencode(dict(apiKey=api_key))

	encodedurl="http://api.bit.ly/shorten?version=2.0.1&%s&%s&%s" % (longUrl, login, apiKey)

	request = urllib.urlopen(encodedurl)
	responde = request.read()
	request.close()
	responde_dict = eval(responde)
	short_url = responde_dict["results"][long_url]["shortUrl"]
	print short_url

except:
	print sys.argv[1]

Be sure to modify the values as told in the script, specifically the username and apikey values. When finished, choose Save from the File menu and give your new service a name. You can now highlight any long URL, right click it and choose your new service from the Services menu to short the URL.

URL Shortener Program
This Automator script will be saved as an Application that you can use whenever you have a URL (and just a URL) in your clipboard. If whatever you have isn’t a URL or won’t convert properly then your clipboard won’t change. To get start do the following:

  • Unless you did this for the service described above, go to bit.ly and create an account
  • Visit http://bit.ly/a/account and record your username and api key
  • Launch Automator
  • Create a new Application
  • Search for Clipboard using Automator’s search tool
  • Drag on the “Get Contents from Clipboard” action
  • Search for “Script”
  • Drag on the “Run Shell Script” action
  • Change the Shell: option to /usr/bin/python
  • Change the Pass input: option to “as arguments”
  • Search for Clipboard again
  • Drag on copy “Copy to Clipboard”

You are now ready to paste in the same code mentioned above. Again, change the values for username and apikey to your own. Choose Save from the File menu and save your new “Application” with whatever name you want. The next time you have a URL in your clipboard you can run your application to convert the URL stored in the clipboard to a shortened one.

Shout out to this blog for providing the python code used in this post – http://bit.ly/b2SZfD

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>