Setting
A few weeks ago I was browsing the web for possible Christmas gift ideas. I was directed to create a list of gift ideas to be e-mailed to my family. Being a tech/geek most products that I wanted were found online. Each product had it’s own product link on the company’s website. As I was compiling my list I found it increasingly disgusting. The URLs were at least a hundred characters long and spilled over to the next line. Not only did this distract from the list structure of the email, but it also made copying the URL difficult. An example product URL is found below.
Gamecube Controller – http://www.amazon.com/gp/offer-listing/B000UQCER6/ref=dp_olp_new?ie=UTF8
Although only 76 characters, my point is made.
So out of this problem a project was born.
URL shortening was the answer. By providing a shortened URL besides the item I could cleanly convey where to buy the gift idea. I tried a plethora of URL shortening browser plug-ins for Firefox and Chromium. None of them worked for me, all were too slow and cumbersome. So, I set it to myself to devise an alternative to the browser plug-ins and web interfaces. The alternative would be Linux specific and snappy.
The Process
As a foundation I used a script written by Sean O’Donnell that allowed me to shorten links from the command-line. The perl script, bitly.pl, can be found on his blog. With minimal configuration the script was up and running. It was exactly what I was looking for, a small, simple script which quickly returned a shortened link (The code is below).
The next step was to figure out how to control my clipboard from the command line. I new it was possible and was pleasantly surprised to find a simple tutorial posted by Gen2ly on Linux Tidbits. Following his suggestion I used xclip to handle my clipboard. Installation was simple:
$ sudo apt-get install xclip
I discovered I could pipe data to xclip. And so, putting two and two together, I could now send a shortened URL straight to my clipboard. With some finangaling I managed to put together a command which would take a URL located on my clipboard, and replace it with the shortened counterpart.
$ xclip -o -selection clipboard | xargs perl ~/bitly.pl | xclip -selection clipboard
note: this assumes that bitly.pl is in your home folder. Be sure to change “~/bitly.pl” to wherever the script is found.
The final stage in constructing my URL shortening contraption was to bind the command to a key. By doing this I could simply copy a URL, press a button, and paste my shortened URL. By following a tutorial on gNewsSense.org I was able to successfully connect my command to a special key combination.
For a nice touch I printed the bit.ly icon on some label paper, cut it down to size, and labeled my F15 key.
Instructions
- First you must have a bit.ly account. Find your api_login and api_key (These are the number which will link the script to your account). Visit the following URL to find them. http://bitly.com/a/your_api_key/
- Assuming Perl is installed, save the following perl script as bitly.pl. I placed mine in my home directory, but this is up to you. When you download the soucrce code, be sure to change the file extension to .pl After you have saved the script you may want to add the executable bit (
$ chmod +x).
SOURCE CODE BITLY.PL - Change the values for your API Key and API Login, lines 17 and 18 respectively.
- Install xclip
$ sudo apt-get install xclip - Install xbindkeys:
$ sudo apt-get install xbindkeys
Create the following file:
$ touch ~/.xbindkeysrc - Using your favorite editor add the following lines to .xbindkeysrc
"xclip -o -selection clipboard |xargs perl ~/bitly.pl | xclip -selection clipboard"
Shift + F5
note: this assumes that bitly.pl is in your home folder. Be sure to change “~/bitly.pl” to wherever the script is found - Run the command to apply your new key bindings.
$ xbindkeys - Add the command above to your /etc/rc.local file so that it starts up on boot.
Have fun shortening.
Other Uses
- storing links in text files (evernote)
- Disguising URLs quickly
