Alfred: Bitly Script

January 28, 2012

Found a lot of nice extensions for Alfred, but found myself needing to modify existing scripts to better suit my needs, or to maybe fix little issues that existed. This script is based on the one @ mosx.tumblr.com.

Added an easy area to add your Bit.ly username and api key. Noticed the script would give me an API error if the address was missing the http tag or only used the www prefix, so tried to have it add a working prefix to the URL. Added a Growl notification, but sometimes it doesn’t pop up. Still sorting everything out, but so far its been working okay.

Update:
– Tried to add something to deal with no prefix.
– Still using the 1x version of Alfred, so don’t know if there were any changes that would affect anything.

#!/bin/bash
# Configuration:
# ulog=YOUR_USERNAME
# ukey=YOUR_BITLY_API_KEY
#
# Get your Username and API key here:
# https://bitly.com/a/your_api_key/

ulog=
ukey=
ulink={query}
if echo ${ulink} | grep -q -E 'http:\/\/|https:\/\/'
then
slink=`curl -s --data "login=${ulog}&apiKey=${ukey}&longUrl=${ulink}&format=txt" http://api.bitly.com/v3/shorten`
echo $slink | pbcopy && /usr/local/bin/growlnotify "Link Created" -m $slink
elif ulink2=`echo ${ulink} | sed 's/www./http:\/\/www./'`
then
slink2=`curl -s --data "login=${ulog}&apiKey=${ukey}&longUrl=${ulink2}&format=txt" http://api.bitly.com/v3/shorten`
echo $slink2 | pbcopy && /usr/local/bin/growlnotify "Link Created" -m $slink2
else
ulink3=`echo http://${ulink}` ||
slink3=`curl -s --data "login=${ulog}&apiKey=${ukey}&longUrl=${ulink3}&format=txt" http://api.bitly.com/v3/shorten`
echo $slink3 | pbcopy && /usr/local/bin/growlnotify "Link Created" -m $slink3
fi