Using JavaScript (not JXA)
Using Real JavaScript in BTT
NOTE: If you are using the floating webview action in BTT have a look here instead: Floating Webview JavaScript
This feature is new in BetterTouchTool version 3.333. I think it is the most powerful and easiest way to script BetterTouchTool.
It's basically just modern JavaScript (so you can use async await and the like) however it has a few important additions:
- It can call any AppleScript - and get the result
- It can call any Shell Script - and get the result.
- It can trigger all functions available in BetterTouchTool's scripting interface.
By combining these three you can automate almost any task on your Mac.

1.) Running Standard AppleScripts: runAppleScript(yourScript)
To run an AppleScript from within the JavaScript code, use the runAppleScript(scriptCode) function. The function takes a string that defines the AppleScript, and returns a promise which resolves to the result value.
I'm always using it with async await, but you can also use classic Promise syntax.
Example: Run AppleScript from within JavaScript
// You can either wrap your script in a self executing async function or a standard named async function.
// If you use a named function, you can use return instead of returnToBTT
async function someFunctionName() {
// 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.
let result = await runAppleScript(appleScript);
// do whatever you want with the result
// at the end you always need to call returnToBTT to exit the script / return the value to BTT.
return result;
}
2.) Running JavaScript For Automation (JXA) Scripts: runJXA(yourScript)
To run a JXA script from within the JavaScript code, use the runJXA(scriptCode) function. The function takes a string that defines the JXA script, and returns a promise which resolves to the result value.
I'm always using it with async await, but you can also use classic Promise syntax.
Example: Run JavaScript For Automation (JXA) from within JavaScript (requires BTT >= 4.903)
// You can either wrap your script in a self executing async function or a standard named async function.
// If you use a named function, you can use return instead of returnToBTT
async function someFunctionName() {
// put the JXA script into a string (back ticks are great for multi-line strings)
let jxaScript = `
var app = Application.currentApplication()
app.includeStandardAdditions = true
var response = app.displayDialog("Test?", {
defaultAnswer: "",
withIcon: "note",
buttons: ["Cancel", "Continue"],
defaultButton: "Continue"
})
// Result: {"buttonReturned":"Continue", "textReturned":"Test"}
app.displayDialog("Hello, " + (response.textReturned) + ".");
`;
// this will execute the JXA script and store the result in the result variable.
let result = await runJXA(jxaScript);
// do whatever you want with the result
// at the end you always need to call returnToBTT to exit the script / return the value to BTT.
return result;
}
3.) Running Shell Scripts: runShellScript
Example: Run Shell Script from within JavaScript
// You can either wrap your script in a self executing async function or a standard named async function.
// If you use a named function, you can use return instead of returnToBTT
async function someFunctionName() {
// 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. If you use multiple parameters please separate them by ;; e.g. -c;;date
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
// at the end you always need to call returnToBTT to exit the script / return the value to BTT.
return result;
}
4.) Running Shortcuts From Apple's Shortcuts App: runAppleShortcut
You can provide the name of the shortcut and some input (optional).
async function someFunctionName() {
let shortcut = "some name";
let input = "some input";
let result = await runAppleShortcut({name: shortcut, input: input});
return result;
}
If you want to provide multiple files as input, please separate them with two semicolons. For example
async function someFunctionName() {
let result = await runAppleShortcut({name: 'multi-file-test', input: '~/Downloads/test1.png;;~/Downloads/test2.png'});
return result;
}