JXA: Execute shell script and get results?

In KM, we can do this:

(Notice the "save results to variable" option.)

Can we do the equivalent of this from JXA? Is there a way to run a shell script and get the results, like the KM Action does?

(And before anyone suggests it, yes, I know I can call KM from JXA and get KM to run the script. I already do that. I want to know if I can do it natively in JXA.)

I take it that you are asking about more than .doShellScript(strCmd) ?

(() => {
    'use strict';

    var a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true, a);

    return sa.doShellScript('pwd');

})();

(or using a kmEngine object to store a string returned by .doShellScript() in a KMVAR ?)

Nope. Didn’t know it returned the result like that. Thanks!

Question: I know about .includeStandardAdditions = true, but I don’t understand what you’re doing here:

sa = (a.includeStandardAdditions = true, a);

Any reason why you didn’t just use the following?

(() => {
    'use strict';

    var a = Application.currentApplication();
    a.includeStandardAdditions = true;

    return a.doShellScript('pwd');

})();

Thanks.

Binding the modified object to a new name is just for my own clarity and convenience.

(I like to work within a discipline of referential transparency – avoiding mutable name bindings.

It just means that I only assume that standard additions have been enabled where I see an sa in the code).

As they say, immutability changes everything : - )

LOL. Inscrutable. :slight_smile:

Actually, I wasn’t asking what the benefit is - I’m asking what it actually does. In other words:

sa = (a.includeStandardAdditions = true, a);

What happens here?

A parenthesised and comma-delimited sequence of expressions returns the value of the final sub-expression.

Here it enables me, within a declaration line, to both update the object (changing a boolean property), and after that change, bind a new name to it.

Thank you. I was sure it was something like that, but it wasn’t familiar to me. Thanks.