What does "Create isolated new context for script execution" do in "Run Real JavaScript" action?

I'm trying to understand what the toggle setting "Create isolated new context for script execution" in the "Run Real JavaScript" action does.

I haven't found anything mentioning it in the Docs or the Community Forum.

Screenshot showing where you can find the toggle:

by default BTT now runs all scripts in the same JS context for performance reasons. This is usually not a problem, but if you have multiple scripts that modify the same global variable it could lead to problems.

Basically the same as opening a new browser window/tab.

1 Like

Ah! I think I understand why some of my scripts have started to only work every now and then! (And the „Run Script“ button always lets them work, so my scripts should not be the problem.) I like to use let variables, and redefining an already used let variable usually throws an error. So I assume, it’s that the first run works, and then the script wants to redefine a let variable and fails, and that’s why my scripts don’t work any more.

Using var instead of let should make it work, too.

Yes, var is more forgiving with redefinitions. I just put everything inside an immediately invoked arrow function:

(() => {
let a = ...
...
})()

And now it all works again!

2 Likes

Quick question: does this also hold for JXA scripts (and, also interesting, Applescripts)? I am wondering if I can avoid overhead by using javascript global variables rather than BTT variables (and also if thread-safety is an issue).

no, unfortunately I can not influence Apple Script execution. The system executes Apple Scripts sequentially, so there shouldn't be threading issues.