Accessing scripting variables

Summary: How can I access a variable set in a shell script that was run in a self executing async function?

Details: I'm making good progress triggering actions with BTT and StreamDeck. I'm running shell scripts using the template code at https://docs.folivora.ai/docs/1106_java_script.html, wrapping my shell script in a Javascript function.

I had displayed the result of the shell script in the HUD with {BTTLastTerminalCommandResult} but was having some difficulties. Specifically, BTTLastTerminalCommandResult wasn't updated the first time I accessed it from a previous execution.

My goal is to access the value in the result variable (from returnToBTT), but I can't figure how to get at it in the subsequent Show HUD overlay action.

(async () => {

// put the shell script into a string (single backticks are great for multiline strings)
let shellScript = `export PATH=$PATH:/opt/homebrew/bin/ ; curl -X GET "http://127.0.0.1:5555/duenext" -H  "accept: */*"  | jq '.[0]'`;


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 Apple 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.
returnToBTT(result);

// it is important that this function self-executes ()
})();
1 Like

Nevemind, @Andreas_Hegenberg already showed mee how to do this :disappointed:

Leaving this message as a future reminder to myself—or to help anyone else who comes across the same problem.

The key to returning the value is this…

let result = await runShellScript(shellScriptWrapper);
await callBTT('set_string_variable', {variable_name: 'somevariablename', to: result});

As recommended by the online docs, wrapping the shell script in a self executing async function solves the problem for me of variables staying always being up to date.

1 Like