I usually get up an hour or two before I start my work day and “play.” Playtime usually results in half-finished scripts and deleted git branches, but sometimes I do something simple and useful (to me). Wednesday was Bash fun, and here’s this morning’s project: LaunchBar actions to url encode and decode strings1. If you run them outside of LaunchBar, they’ll encode/decode your clipboard, replacing what’s in your clipboard with the result, so they have multiple applications. These have probably been done before, but my quick DuckDuckGo search didn’t yield any immediate results.
I find it especially useful to be able to quickly encode and decode urls and strings when I’m testing online APIs out, but there are many times when I find I need this. I usually use a shell function or the very handy Hash Widget for Dashboard, but as a LaunchBar user, this is faster.
To use with LaunchBar, open the script in your Script Editor (instant-open links provided) and save it to ~/Library/Application Support/LaunchBar/Actions, creating the folder if it doesn’t already exist. Then, assuming you have Actions enabled in your indexing preferences, you can just type “urle” to get the action, then hit space to enter or paste the text to encode/decode. Alternatively, you can paste first or use Instant Send on a selection, then hit Tab and select the encode or decode action. To use elsewhere, such as in FastScripts, just save them as scripts in your ~/Library/Scripts folder. Using them outside of LaunchBar won’t be interactive; they will encode or decode your clipboard in place.
Side note: I decided to do the encoding/decoding in pure AppleScript, using functions I’ve mentioned previously from http://harvey.nu/. You can encode faster with Perl using a shell call, if you prefer. In a shell script for encoding, you’d use echo "string to encode"|perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord("$1"))/seg'. In AppleScript, that would look like set _res to do shell script "echo \"" & ASTextVar & "\"|perl -pe's/([^-_.~A-Za-z0-9])/sprintf(\"%%%02X\", ord(\"$1\"))/seg'", where ASTextVar is your string to encode.
The LaunchBar actions copy the resulting text to the clipboard, but you may prefer to have it passed back to LaunchBar for subsequent processing. There are commented lines in the scripts for doing so, just comment out the last line in the handle_string function and uncomment the line above it.
Open this script in your AppleScript editor
People may love Alfred, but this is another example of why I will probably always prefer LaunchBar. Extensibility. Also, QuickSilver is dead, just give up.↩
Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff on Twitter, GitHub, and Mastodon. Keep up with this blog by subscribing in your favorite news reader.