Beautify your JavaScript in BBEdit
BBEdit has nice tools built-in for formatting HTML and CSS into nice, hierarchically-indented blocks. It doesn’t do JavaScript though, which I often lament when I’m copying and pasting other people’s source code (or just my own lazily-written code).
Enter the JavaScript beautifier written by Einars “elfz” Lielmanis. It works very well. But I’m lazy, and I don’t want to visit a web site every time I want to format some code. I want to press a shortcut key and have some highlighted code be instantly formatted. elfz has thoughtfully made the source code available, so the question is: how do you get BBEdit to talk to PHP?
Luckily, OS X can run PHP as a shell script language. With a couple of extra lines of code tacked on the end, I was able to turn elfz’s script into a BBEdit-friendly “filter” which I can pass JavaScript through.
$BBEditInput = file_get_contents($argv[1]) or die($php_errormsg);
print(js_beautify($BBEditInput));
Install said PHP file into ~/Library/Application Support/BBEdit/Unix Support/Unix Filters/ and then give it a keyboard shortcut (I chose Cmd-Ctrl-J since it wasn’t doing anything else yet).
Yay! Nicely-formatted JavaScript at the stroke of a key.