Is it possible to run JavaScript in Textmate?

All we need is an easy explanation of the problem, so here it is.

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

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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 CmdR. 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']))}"

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply