Set variable with "Run Real JavaScript"

Hi, I found in this thread how to set a variable with AppleScript: HELP: Conditional Activation Groups with Variables

I've now tried to do the same when using the action "Run Real JavaScript". So far I can't get it to work, also the documentation didn't bring me further.

What's the best way to do it? I would prefer to use JavaScript only, without AppleScript. So far I tried to modify example 1) from here: https://docs.bettertouchtool.net/docs/1106_java_script.html

The resulting code was basically as described on that page, except for:

let appleScript = `
    tell application "BetterTouchTool" to set_string_variable "customVariable1" to "asdf"
`;

Unfortunately this does not work. What am I doing wrong? Plus - is there a way to do it without AppleScript nested in JS?

Thanks already!

just search for set_string_variable on the docs page you linked, it should have an example how to do this with JS. (all BTT script interfaces can be called like this from JS)

I tried that before but always got an error. Figured it out now.

In case anyone else is struggling with it: Instead of just taking the example for set_string_variable, you need to open it with an "(" and accordingly then close it with "})();", instead of "}();".

So this:

async ()=> {
let result = await callBTT('set_string_variable', {variable_name: 'customVariable1', to: "this is a test value"});
returnToBTT(result);
}();

turns to this:

1 Like