Connecting Alfred to Bitly via Ruby
Introduction
I love Alfred for the Mac OS. It is one of the core power tools that I use on a daily basis. Coupled with the Powerpack I’m always finding new ways of expending my capabilities. In this case, faster URL shortening.
Requirements
This post assumes you are familiar with the following:
The Problem
Recently, while using my Tweeter Gem (e.g. Twitter for the Command Line), I ran into a situation where I wanted to streamline my URL shortening experience by executing the following keyboard commands:
- OPTION + SPACE
- bilty
+ pasted long URL + ENTER
If you are thinking “Yeah, but Alfred comes with a Bitly command out of the box.” You are correct. However, it only sends you to the Bitly page for shortening URLs. I’d rather not see Bitly open in a browser tab. I only want the work performed and sent to my clipboard without the browser involved. Then I can paste the shortened URL within my terminal window with only a few keystrokes. Simple. Fast. Efficient.
The Solution
The solution is an easy one now that Alfred supports terminal/shell shortcuts. Almost. The reason is that if you are using RVM, which any sensible Rubyist should be using, you’ll find that Alfred defaults to the system Ruby environment that comes pre-installed with your Mac OS. To prove this, try running a Ruby script referencing a Ruby Gem that you DON’T have installed in your system environment. With Alfred logging turned on (via advanced options), you’ll see the following in your Mac OS Console (click to enlarge):
D’oh!
While this might not be an issue for some, I’d rather use a specific Ruby version and gem set rather than install the same gem to my system library and pollute that unused space. This then requires a shell script to be crafted which allows one to launch a Ruby script within a specific Ruby environment.
Using this shell script, the following becomes possible from the command line:
rvm_ruby ruby-1.9.2-p136 <path to Ruby script>/bitly.rb <some long URL>
NOTE: I placed this shell script in my /bin folder with user, group, and world access set with read and execute access only (i.e. sudo chmod 555).
The corresponding bitly.rb Ruby script then accepts a single URL argument which leverages the Bilty API to shorten a long URL in the background with the end result being copied to the clipboard via the pbcopy Mac OS command.
Putting It Together
Armed with the shell script and the bitly.rb Ruby script, you can now setup the following terminal shortcut in Alfred (click to enlarge):
Here are the shortcut details:
Now you can type:
- OPTION + SPACE
- bilty
+ pasted long URL + ENTER
…and have the shortened URL ready for pasting via the Mac OS clipboard.
Happy URL shortening!
No comments yet.
Leave a comment
You must be logged in to post a comment.
Search
Categories
- Adventures
(145)
- Announcements
(39)
- Business
(20)
- Electronics
(22)
- Employment
(1)
- Epicurean
(10)
- Games
(2)
- Literature
(1)
- Mechanical
(4)
- Meetups
(15)
- Movies
(2)
- Music
(26)
- Photography
(1)
- Services
(27)
- Software
(135)


