Use BBEdit UNIX Filters to Format Javascript

Here’s a tip specifically for people who use BBEdit to write Javascript.

If you copy a lot of Javascript examples from the web you probably end up with code that hugs the left edge of your BBEdit window like this (for example):

var txt=""
function message()
{
try
{
adddlert("Hey There!")
}
catch(err)
{
txt="An error has occurred"
alert(txt)
}
}

But you want it indented and nicely formatted, right?

Well, we found a link on the web [ http://attaboy.tumblr.com/post/20570385 ] that sort of explained how to put together a BBEdit UNIX Filter to do just that, but we also bashed our brains out for about an hour before realising that to call a PHP Shell Script in a UNIX Filter you need to wrap this around the code:

#!/usr/bin/php
<?php
[... your code...]
?>

Obvious to some - not to all of us!!

So we’ve put together the complete script to format Javascript from BBEdit here for downloading:

http://www.metaclarity.com/dl/JavaScriptFormatPHP.zip

Unzip it and put it in this folder:
~/Library/Application Support/BBEdit/Unix Support/Unix Filters/

Select your “flat” Javascript in the BBEdit window. Run the filter (from the !# menu or from Palettes>UNIX Filters) and it should format your Javascript!

Good luck!

Leave a Reply

You must be logged in to post a comment.