AppleScript action sometimes delayed

Describe the bug
Keyboard action running "Run AppleScript (async in background)" with one-line applescript: "tell application "" to activate", with "Show HUD overlay when shortcut is triggered" enabled.

Problem: On keypress the HUD overlay shows up instantly. However, the Applescript either executes delayed or really slowly. Response (i.e. window come to front) takes 5 to 10 seconds in a large portion of cases, otherwise works as intended and window come to front instantly.

I am experiencing this with a number of applications, all running already.

This happens only after upgrade to Mojave, before exactly the same config worked fine.

Response is instant if if press the same keyboard shortcut twice

Affected input device (e.g. MacBook Trackpad, Magic Mouse/Trackpad, Touch Bar, etc.):

Experienced with keyboard.

Device information:

  • Type of Mac: 2017 13" MacBook Pro
  • macOS version: 10.14
  • BetterTouchTool version: 2.660

Maybe you have other Apple Scripts running in BTT that are running? (Only one Apple Script can be executed at a time, they will be queued)

I did restart BTT and executed the keyboard action as first activity. Same effect.

Weird, I haven't had similar reports yet.

One other idea:
If the Apple Scripts you are running require some UI interaction, you need to use the Apple Script - blocking action instead of the async one, because these can not be run in the background:

Same effect with both, blocking and background. I don't do UI in the AppleScript, just the one tell-to-activate line.

Running the script from the AppleScript editor is always instant.

Strange activating apps seems to be pretty instant here. It sounds very much like some other scripts are blocking the execution (maybe some Touch Bar scripts?)

Have you tried to restart your Mac since this started happening? (In general BTT just executes the Apple Script and waits for the system to do it's stuff).

Alternatively if you just want to activate an app you could also use the "Launch Application / Open File ..." action in BTT.

This works well. Thanks.

Hi @Andreas_Hegenberg , you say that AppleScript are queued, I guess it is a BTT-related behavior.

I'm facing the issue where I have a TouchBar button that trigger an AppleScript on click. I've found that it sometimes takes seconds (sometimes a minute) to be ran after I click it. Given that I've also have a few TouchBar buttons which run AppleScript every 5 seconds, it may be related. If this is due to this, I would love to see a option to set AppleScript priority, as I really want to run my AppleScript instantly when clicking the button, whereas I don't care if other buttons updates are longer than expected.

that indicates that something strange is going on with your other scripts. What are they taking so much time for?

If your button press apple script doesn't need to show any UI, you can also try to use the "run in background" option.

Prioritizing and scheduling Apple Script is complicated because after they are dispatched to the system, there is not much BTT can do but wait. (maybe I could forcefully kill the running Apple Script process, but not sure whether that would introduce new issues)

Ok, I think I may have found the culprit script. I wrote one that check for fan speed every 30 seconds (set in BTT), and if fan speed is bigger than my defined threshold, it will update every 5 seconds. I think the delay 5 may block other AppleScript execution. But I can't find a way to have an async delay in AppleScript. Maybe there's something in BTT API that would allow me to update script frequency, instead of using delay in AppleScript ?

For the record, here is my script :

set threeshold to 1000 #min speed to show widget

#this command returns the second line, which is the speed of the second fan (because it seems it is often higher than the second)
set currentFanSpeed to (do shell script "/usr/local/bin/istats fan speed --value-only | sed -n 2p") as number

if (currentFanSpeed > threeshold) then
	#refresh every 5 seconds when it is above threeshold
	delay 5
	tell application "BetterTouchTool"
		refresh_widget "12A15E7C-0B06-4472-9C89-A7E1DF08294A"
	end tell
	
	return currentFanSpeed
else
	return ""
end if

I tried the Run in Background option, but it doesn't change anything. Should it be used when the goal is to update the TouchBar button title ?