Oct 28 2007

TextMate: On the fly JavaScript evaluation with Rhino

For those interested in developing JavaScript outside the browser, Rhino is one of the natural choices. It seems to combine the best of both the dynamicaly and statically typed worlds. Rhino offers a Shell for dynamic script evaluation, along with the option to compile the scripts into Java classes and reap any advantages compiled software has on display. It's a mature platform, built with performance in mind, leveraging a robust VM.

In order to evaluate scripts using Rhino in TextMate in a similar fashion to that of the Run command ("cmd R") from the Ruby Bundle:

Provided that Java is installed on the system and the $JAVA_HOME environment variable has been set, download Rhino, unpack it somewhere and set an environment variable ($RHINO_HOME) to point to the unpacked Rhino distribution directory. I keep these environment variables in ~/.profile. Open the Bundle Editor in TextMate, navigate to JavaScript and add a new Command. From the Save drop-down, pick Current File, set Input to Entire Document, Output to Show as Tool Tip and Activation to Key Equivalent. I chose "cmd E" as the hot-key for invoking the Command.

Here's the script for the Command(s) input:

echo Evaluating : "$TM_FILEPATH"
echo
echo
$JAVA_HOME/bin/java -classpath $RHINO_HOME/js.jar \
org.mozilla.javascript.tools.shell.Main -f $1 "$TM_FILEPATH"

Sample output:

textmate-rhino-js