Using AppleScript I can call a shell script with:
do shell script "echo 'Foo & Bar'"
But I can't find a way to do this with JavaScript in the Yosemite Script Editor.
Using AppleScript I can call a shell script with:
do shell script "echo 'Foo & Bar'"
But I can't find a way to do this with JavaScript in the Yosemite Script Editor.
Share Improve this question edited Jan 21, 2015 at 11:38 foo 3,23918 silver badges18 bronze badges asked Jan 20, 2015 at 11:55 sverrejohsverrejoh 16.9k13 gold badges42 silver badges29 bronze badges 3- Maybe this helps you out: stackoverflow./questions/27586694/… – ShooTerKo Commented Jan 20, 2015 at 13:18
-
I'd remend looking into Cocoa's
NSTask
as it's much more robust and capable thando shell script
. – foo Commented Jan 21, 2015 at 11:08 - Similar question: stackoverflow./questions/48201173/… – GDP2 Commented Aug 15, 2022 at 22:07
2 Answers
Reset to default 6do shell script
is part of the Standard Scripting Additions, so something like this should work:
app = Application.currentApplication()
app.includeStandardAdditions = true
app.doShellScript("echo 'Foo & Bar'")
To plement ShooTerKo's helpful answer:
When calling the shell, properly quoting arguments embedded in the mand is important:
To that end, AppleScript provides quoted form of
for safely using variable values as arguments in a shell mand, without fear of having the values altered by the shell or breaking the mand altogether.
Curiously, as of OSX 10.11, there appears to be no JXA equivalent of quoted form of
, but, it's easy to implement one's own (credit goes to this ment on another answer and calum_b's later correction):
// This is the JS equivalent of AppleScript's `quoted form of`
function quotedForm(s) { return "'" + s.replace(/'/g, "'\\''") + "'" }
From what I can tell, this does exactly what AppleScript's quoted form of
does.
It encloses the argument in single-quotes, which protects it from shell expansions; since single-quoted shell strings don't support escaping embedded single-quotes, an input string with single-quotes is broken into multiple, single-quoted substrings, with the embedded single-quotes spliced in via \'
, which the shell then reassembles into a single literal.
Example:
var app = Application.currentApplication(); app.includeStandardAdditions = true
function quotedForm(s) { return "'" + s.replace(/'/g, "'\\''") + "'" }
// Construct value with spaces, a single quote, and other shell metacharacters
// (those that must be quoted to be taken literally).
var arg = "I'm a value that needs quoting - |&;()<>"
// This should echo arg unmodified, thanks to quotedForm();
// It is the equivalent of AppleScript `do shell script "echo " & quoted form of arg`:
console.log(app.doShellScript("echo " + quotedForm(arg)))
Alternatively, if your JXA script happens to load a custom AppleScript library anyway, BallpointBen suggests doing the following (lightly edited):
If you have an AppleScript library you reference in JS using
var lib = Library("lib")
, you may wish to addon quotedFormOf(s) return quoted form of s end quotedFormOf
to this library.
This will make the AppleScript implementation of quoted form of available everywhere, aslib.quotedFormOf(s)