Skip to main content

AppleScripts & Shell Scripts

You can run AppleScripts and Shell Scripts right from within the webview HTML using the runAppleScript (or runJXA) and runShellScript JavaScript functions that are automatically made available.

Running AppleScripts from the WebView

<html>
<head>
<script>
async function runSomeAppleScript() {

// put the AppleScript into a string (back ticks are great for multi-line strings)
let appleScript = `
set theDialogText to "The curent date and time is " & (current date) & "."
set result to display dialog theDialogText
return result
`;

// this will execute the AppleScript and store the result in the result variable.
// if you want to use JavaScript for Automation use runJXA instead
let result = await runAppleScript({script: appleScript});

//do whatever you want with the result
document.getElementById("testButton").value=result;
console.log('result', result);

}
</script>
</head>
<body>
<button id="testButton" onclick="runSomeAppleScript()">Run AppleScript</button>
</body>
</html>

Running Shell Scripts from the WebView

<html>
<head>
<script>
async function runSomeShellScriptScript() {

// put the shell script into a string (single backticks are great for multiline strings)
let shellScript = `say hello world`;


let shellScriptWrapper = {
script: shellScript, // mandatory
launchPath: '/bin/bash', //optional - default is /bin/bash
parameters: '-c', // optional - default is -c
environmentVariables: '' //optional e.g. VAR1=/test/;VAR2=/test2/;
};

// this will execute the Shell Script and store the result in the result variable.
let result = await runShellScript(shellScriptWrapper);

//do whatever you want with the result
document.getElementById("testButton").value=result;
console.log('result', result);

}
</script>
</head>
<body>
<button id="testButton" onclick="runSomeShellScriptScript()">Run Shell Script</button>
</body>
</html>