Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
How do you want to "run" JavaScript independently of any browser? –  slhck Nov 1 '11 at 23:14
    
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 Nov 2 '11 at 0:19

1 Answer 1

up vote 7 down vote accepted

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']))}"
share|improve this answer
    
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 Nov 2 '11 at 16:13
    
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 Nov 2 '11 at 16:15
    
Sorry, you are right, I didnt read this very carefully yet –  oalo Nov 2 '11 at 16:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.