SeniorInquisitor SeniorInquisitor - 4 months ago 38
Bash Question

javascript do shell script

Newbee question -Now that OSX/Script Editor.app can do javascript (https://developer.apple.com/library/mac/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/GettoKnowScriptEditor.html#//apple_ref/doc/uid/TP40016239-CH5-SW1), how do I change my applescript

set a to (do shell script "ls")
to the javascript syntax? From what I'm reading in the StandardAdditions.sdef, "doShellScript method : Execute a shell script using the ‘sh’ shell" is the command, so I tried,
doShellScript "ls"
, and the Script Editor (with the Script Language is set to JavaScript) returns, "Error -2700: Script error."

Answer

You have to tell your JXA script to use the Standard Additions.

app = Application.currentApplication()
app.includeStandardAdditions = true

sourcePath = "/Applications"

app.doShellScript("ls '" + sourcePath + "'").split("\r")

Enjoy, Michael / Hamburg