Is it possible to run JavaScript in Textmate?

3

2

If I try to run it now I get a XCode error. Do I need to install a JavaScript interpreter on my computer?

oalo

Posted 2011-11-01T22:52:34.883

Reputation: 131

How do you want to "run" JavaScript independently of any browser? – slhck – 2011-11-01T23:14:40.440

Very good point, of course it is kind of pointless, but wouldn't it be possible to run very basic programs without the browser stuff with an interpreter? JavaScript is used for server stuff lately right?, so I guess there must be some kind of browser independent interpreter, or am I wrong? – oalo – 2011-11-02T00:19:27.733

Answers

9

javascript-tools

First of all, there's the javascript-tools Bundle, which offers some productivity tools such as Lint syntax checking, YUI compression, bookmarklet creation, and more.


Creating your own bundle

OS X comes with a JS interpreter found under /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc. You can symlink it to your PATH to have it available everywhere.

For example:

ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin

Check if it works by just typing:

jsc

You should land in a console.

enter image description here

Now, enter TextMate and open the bundle editor by going to Bundles » Bundle Editor » Show Bundle Editor. In the JavaScript bundle, create a new command by clicking the + button below.

Set the Input to Entire Document, and the Output to Discard. Set the keyboard shortcut to Cmd-R. Now, paste the following script into the command area itself:

#!/usr/bin/env ruby

require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'

def terminal_script_filepath
  %|tell application "Terminal"
      activate
      do script "jsc -i #{e_as(e_sh(ENV['TM_FILEPATH']))}"
    end tell|
end

open("|osascript", "w") { |io| io << terminal_script_filepath }

This should look like the following:

enter image description here

And you're done. Try it by saving a JS file and pressing the keyboard shortcut.

enter image description here


Using Google's V8 instead

You can install Google's V8 Javascript engine, it's free and open source, and comes for every major OS.

Now, to install it, you either need to build it yourself, or use a package manager like Homebrew.

Install Homebrew first:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

Then install the V8 engine:

brew install v8

This takes a while to compile. Now, when you're done, you can adapt the script above to use V8 instead. Just change the line with do script to:

do script "v8 #{e_as(e_sh(ENV['TM_FILEPATH']))}"

slhck

Posted 2011-11-01T22:52:34.883

Reputation: 182 472

Great, this is exactly what I was looking for. Do you think this will allow me to run JavaScript scripts in textmate too? (I cant try it right now, I am at work) – oalo – 2011-11-02T16:13:54.420

This does run JavaScript from TextMate, see my line about the TextMate bundle editor :) (It just opens a console for doing it … might be possible to run it in a TextMate window, but I'll have to see about that) – slhck – 2011-11-02T16:15:26.103

Sorry, you are right, I didnt read this very carefully yet – oalo – 2011-11-02T16:19:15.927