LaunchBar Script: Copy as bit.ly
Sunday, January 31, 2010 at 2:54PM I recently started using LaunchBar which comes with a Copy as TinyURL action built in however I use bit.ly. After searching around for a little while I found a script that posts to Twitter with bit.ly URL shorting but that was it.
I decided to take parts of both of these scripts to make my own and here is the code (download):
on handle_string(theURL)
-- add scheme prefix if missing
set _firstColon to offset of ":" in theURL
set _firstSlash to offset of "/" in theURL
if (_firstColon = 0 or _firstSlash < _firstColon) then
set theURL to "http://" & theURL
end if
-- Process and encode URL
set theURL to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of theURL
-- Create bit.ly
set login to "YOOURUSERNAME"
set apiKey to "YOURAPIKEY"
set bitlyURL to "http://api.bit.ly/shorten?version=2.0.1&longUrl=" & theURL & "&login=" & login & "&apiKey=" & apiKey & ""
set results to do shell script "curl " & quoted form of bitlyURL & " | grep shortUrl | awk '{print $2}' | sed 's/[\",]//g'"
-- Copy to clipboard
tell application "LaunchBar"
perform action "Copy" with string results
end tell
end handle_string
Replace "YOURUSERNAME" and "YOURAPIKEY" with your own details.
To install the action place the file in ~/Library/Application Support/LaunchBar/Actions.
I have never written any AppleScript before and now I understand just how powerful it is I'm going to start learning the language.



Reader Comments