Using TextMate with Rhino
If you regularly read a site like reddit’s programming page, you’ve probably been seeing lots of chatter about Javascript lately. As it turns out, while Javascript was once considered by many to be a toy language, it is now being seen in a new light by programmers familiar with other dynamic languages such as Ruby or Python.
Problem is, Javascript has traditionally been a pain to play with. You’re basically locked into programming in the browser, which is a really cumbersome way to investigate the more interesting features of Javascript such as object prototyping and functional programming.
Enter the good folks at Mozilla who developed Rhino, a JVM-based javascript interpreter geared toward the application and server-side domains. Rhino provides a great way to try your hand at Javascript in your own favorite text editor.
On my Mac, I’m rarely using anything but TextMate to get coding done. Here I’ll show you how to add a new command to the TextMate Javascript bundle so that you can run your script in Rhino by simply pressing Apple-R.
First, you need to get rhino. Download the latest version here (Java SE 6 includes Rhino, however Apple hasn’t been released it for Mac OS 10.5 yet). Put it in a convenient place, such as /usr/local or somewhere in your user directory.
Once we have Rhino somewhere in our filesystem, we need to make a new command in TextMate to bind Apple-R to “Run in Rhino”. Open TextMate and select Bundles -> Bundle Editor -> Show Bundle Editor:

- First click on JavaScript in the list of bundles on the left-hand side.
- Now click the plus button in the lower-left hand corner and select New Command.
- You will see a new command show up under JavaScript called “untitled”, waiting for you to replace this with a more meaningful name. Call it something like “Run Script in Rhino”
- Set “Save” to Nothing
- Set “Command(s)” to /usr/bin/java -cp /usr/local/rhino/js.jar org.mozilla.javascript.tools.shell.Main -f “$TM_FILEPATH”
- Set “Input” to Entire Document
- Set “Output” to Show as HTML
- Set “Activation” to Key Equivalent, Apple-R (or whatever else you choose)
- Finally, set “Scope Selector” to source.js
- Now all you have to do is close the Bundle Editor window, then go to the Bundles Menu and select Bundle Editor -> Reload Bundles
Now just open a JavaScript script, hit Apple-R, and voila! A new window opens with the output of your script
