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:

2 Likes

Thanks a lot... this 4 yo post is better than the doc...

This is the code from doc which doesn't work:

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

https://docs.folivora.ai/docs/1106_java_script.html

what is callBTT?

Am I looking up wrong doc or something?

this code is correct, callBTT is not required in current versions of BTT.
What are you trying to do?

After executing that code via the run real javascript action you'll can check the variable value in the settings:

I clicked the multiple times, yet didn't show up.

Now I just restarted the BTT and working both.
Thanks for the quick reply!