I have a script written in JavaScript which needs to run one small piece of AppleScript (to achieve some functionality I\'ve been unable to implement due to a number of fact
Use Standard Additions' run script
command.
(BTW, my standard recommendation is to stick to using AppleScript for application automation as it's the only [supported] option that works right. JXA's various bugs, defects, and omissions were known about before even it shipped, and Apple couldn't be bothered to fix them back then, so they certainly won't be fixed now the Automation team has been eliminated. The whole Mac Automation platform's dying anyway, but at least AppleScript should bitrot slower than JXA, which was never right to begin with.)
Here is an evalAS function for ES6 (Sierra onwards) macOS.
(() => {
'use strict';
// evalAS :: String -> IO String
const evalAS = s => {
const
a = Application.currentApplication(),
sa = (a.includeStandardAdditions = true, a);
return sa.doShellScript(
['osascript -l AppleScript <<OSA_END 2>/dev/null']
.concat([s])
.concat('OSA_END')
.join('\n')
);
};
return evalAS('tell application \"Finder\" to the clipboard');
})();
The advantage of the shell approach is simply that scripting additions are automatically available in the evaluation environment.
If we use the following alternative definition of evalSA, we need to explicitly prepend our expression string with 'use scripting additions\n'
(() => {
'use strict';
// evalAS2 :: String -> IO a
const evalAS2 = s => {
const a = Application.currentApplication();
return (a.includeStandardAdditions = true, a)
.runScript(s);
};
return evalAS2(
'use scripting additions\n\
tell Application "Finder" to the clipboard'
);
})();