Command Line JavaScript in Yosemite

After running JavaScript from the Script Editor, I tried to run a script via Terminal as I had done before with AppleScript programs.

I started with a simple “Hello, world!”-ish program in the Script Editor.

Screen Shot 2014-10-19 at 2.11.31 PM

I saved the program.  I noted that the suffix on the filename is “.scpt” instead of “.js”

I then tried to run the script, jstest.scpt via the “osascript” command:

Screen Shot 2014-10-19 at 1.56.52 PM


Note the “failed to get scripting definition” warning.  I have no idea why that’s happening.  I’ve seen another blog post about running JavaScript from the command-line that did not seem to experience the above problem.

I tried to add the language-specifier option to the command-line. That didn’t seem to make a difference:

Screen Shot 2014-10-19 at 1.57.27 PM

I wondered what would happen if I exported the script as an application from the Script Editor.  I tried that next:

Screen Shot 2014-10-19 at 1.58.30 PM

Once saved, I used the “open” command to invoke

Screen Shot 2014-10-19 at 2.00.13 PM


To run from the command-line without exporting, I wrote a short bash script that will invoke the scpt file, but will redirect stderr to /dev/null ( which can be dangerous if some exception condition happens in the script. )

Screen Shot 2014-10-19 at 2.21.13 PM

The next step in my education on scripting OS/X in JavaScript is to figure out the real reason why I’m getting these warning.  I’ll get that fixed.

After that, I’d like to figure out how to perform general scripting tasks within JavaScript.  I would assume that I can process command-line arguments with some built-in functions.  If not, I will likely have to use the ObjC object.

Stay tuned.



About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Programming and tagged , . Bookmark the permalink.

2 Responses to Command Line JavaScript in Yosemite

  1. Jake Wolpert says:

    I put my script in a file, chmodded it +x, and I see the arguments.

    #!/usr/bin/osascript -l JavaScript
    var args=ObjC.deepUnwrap($.NSProcessInfo.processInfo.arguments).slice(3) // 1 redundant name 2 -l 3 JavaScript

    I am working on doing CGIs next.

    Thanks for your blog post!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s