Jump to content

Mac Os X Javascript For The Shell (Easy!)

a quick tip on how you can run JavaScript in Terminal

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Mac Os X Javascript For The Shell (Easy!) a quick tip on how you can run JavaScript in Terminal Rate Topic: -----

#1 User is offline   temhawk 

  • W.R. Private First-Class
  • Group: Members
  • Posts: 322
  • Joined: 30-August 07
  • Gender:Male
  • Interests:travel, cg art, macs, music, skateboarding, programming, discovery channel, TextMate 2

Posted 16 January 2011 - 01:29 AM

In Mac OS X it's very easy to get a JavaScript command line interpreter that's always at the ready. All it takes is one single Terminal command.

Before I show it to you, a tip for you people that are on Windows:
In Windows, running JavaScript from the command line is not as straightforward. There is cscript, to which you can pass the name of a JavaScript file, and it will evaluate that. But you have to use the Windows Script Host Object Model for it to work.
As an example, on Mac you would write
print("test");
instead of document.write("test") or alert("test");. When using cscript, you have to use WScript.Echo("test");.
The documentation for Windows Script Host (WSH) is here.
There may be a better way to do it with PowerShell, but I don't know. Additionally, there may be other JavaScript shells that you can download and use on Windows.

Now, on Mac OS X there is a binary called jsc. The name stands for JavaScriptCore. This is part of WebKit, the "open source web browser engine", which is used by Safari and many other applications, including Dashboard and Mail (for those few of you who didn't already know). In order to make it really easy to use jsc from Terminal, we have to create a soft link in /bin (or any other directory in your $PATH environment variable) that points to the jsc binary in /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc.

The command to do this is ln with the -s option. Here it goes:
sudo ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc
For more info on ln and the options run man ln.

Now, as long as you have /bin in your $PATH variable, which is very likely the case, you can just type jsc into Terminal and be in interactive JavaScript mode, regardless of the current directory. In other words, this saves you from having to type out the long path to the jsc binary every time you want to run it. That's the power of soft links!

Posted Image

The "undefined" in the screenshot above is normal. The WebKit website says:

Quote

The return value of undefined from the variable assignment is from the JavaScript specification. While it might seem like an odd response to a successful operation, it is the expected behavior.

quit() is a built-in function that exits the interactive mode. The other built-in functions are listed on the webpage linked above.

Finally, a list of the resources I think are useful:

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users