Javascript step blocks and times out

Describe the bug
My keyboard shortcut objective is to replace a given string pattern in the selected text.
Should all be very simple :

With the following javascript

var betterTouchTool = Application('BetterTouchTool');
var url = betterTouchTool.get_string_variable("selected_text");
url = url.replace(/.+[?&]q=(.+)?&.*$/, '$1');
betterTouchTool.set_string_variable("new_text", {to: url} ) ;

The bug is that when I test the key, the script locks BTT and times out after a minute or so.
If I test the script part using the "run script" button in the action configuration pane it also blocks and after a minute I get an error dialog saying

AppleScript Error:
Error on line 2: Error: AppleEvent timed out.

The script compiles fine and I have tested the script logic and it works fine.

Device information:

  • Type of Mac: MBP 2018
  • macOS version: Catalina 10.15.1
  • BetterTouchTool version: 3.209

Additional information (e.g. StackTraces, related issues, screenshots, workarounds, etc.):
I have tried alternatively using the Shell script action with the same javascript and there it runs fine (ie. not blocking/timing out) but I have other issues related to the timing of the variable contents (ie. I get the results of the 1st keypress when I test the key a 2nd time, very odd). Seems to be another unrelated issue, so let's focus on the above initial one for now...

unfortunately that's a limitation of blocking scripts that call bettertouchtool, I think if you switch it to async add add a little delay after the script it might work.

Thanks for you quick reply Andreas !
I tried your suggestion and the blocking is not occurring anymore.
However I now have the same issue as when I tried to use the shell script alternative : on run 2 it gives me the 'new_text' of run 1 !
I have a delay of 5 seconds which should be more than enough for the async script to set the 'new_text' variable. Would you have a reasonable explanation to this weird behavior ?

FYI I have already tried the following to try and find the cause but without success :

  • Add delays between each of the steps
  • Setting a persistent variable instead of a regular one
  • Checking value in the BTT preferences scripting area where it shows variable values. All looks good after running.

I think I know why this is happening but I don't have a good workaround yet. I'll look into it!
I want to add an action that allows to modify text via JS anyways!