Which kind of javascript env does Automation run?


#1

(Choosing OmniPlan - OmniPlan for Mac for lack of a better selectable category).

Hi there.
I’m very interested in Automation for OmniPlan. I would like to know more about which kind of javascript enviroment our scripts get executed on. Is it a embedded nodejs? Which version?

Ultimately I’m trying to understand what can I do inside my scripts:

  1. Which javascript syntax can i use?
  2. If a superset of a nodejs version, can I import modules (for example, fs, so I can read local files in the filesystem; or https to make http requests)?
  3. Can I import npm modules somehow (external libraries)?

Thanks!


#2

No, not nodejs.

It’s a JSContext object:

https://developer.apple.com/documentation/javascriptcore/jscontext

No NodeJS access to the filesystem, and no DOM.

You can list the set of available top-level objects by evaluating

Object.keys(this)

in the Automation Console.

To narrow that down to the file API objects:

Object.keys(this).filter(s => s.includes('File'))

Which will yield:

[FileSaver, FileWrapper, FileType, FilePicker]

(details for each of which can be found in Automation > API Reference)


#3

PS you will also see a URL object with a fetch method.

See, for example: https://omni-automation.com/shared/url.html


#4

Thank you, draft8! Sorry for my delayed response here.

That takes care of #2 and #3 I suppose.

Now the answer to one is more specific to Apple’s JavascriptCore, but if you have any pointers I’d greatly appreciate it: where can we find which version that JSContext is and how does it conform to ECMAScript standards?

I just want to know which kinds of javascript’s constructs and syntaxes would be available for me to use…

Any help appreciated, thanks!


#5

The interpreter provided by a JSContext instance is that used by the JSC of the given macOS version, and is essentially that of the version of Safari current at the macOS release.

(You can’t, of course, update the system JSC by installing a later version of Safari)

Grosso modo, it will certainly be a highly compatible ES6 (omniJS is not available on earlier macOS versions), and finer-grained specifics should show up in the Safari columns here:

https://kangax.github.io/compat-table/es6/


#6

PS if you are referring to JXA (which reaches back further than omniJS) it will be ES6 from Sierra onwards, ES5 for the preceding macOS, and absent in any form before that.

The JXA column at https://kangax.github.io/compat-table/es6/ gives a conservative lowest common denominator (macOS Yosemite) view.

(In practice you can use highly compliant ES6 in the JXA of Mojave + Catalina)


#7

That’s perfect! Thank you, @draft8. You rock.