AppleScript execution output varies based on trigger method

Hi!

I'm a huge BTT fan. However, I've run into a bug today that is reproducible. Essentially, I have a case where running an AppleScript using a keyboard shortcut causes the AppleScript to run differently than if I run it using the "Run Script" button in BTT. The script itself is identical in both cases. Hopefully I'm just missing something simple, but I've been banging my head against this for a few hours now.

Describe the bug
Here is the script. I tried to reduce it to the simplest case. Essentially, the script sends a keystroke to copy the selected text. It then displays a dialogue box with the length of the text and the selected text itself (see screenshots below).

set the clipboard to ""

tell application "Notes"
	activate
end tell

tell application "System Events"
	keystroke "c" using {command down}
	delay 0.1
	repeat 10 times -- poll clipboard for ~1 seconds. Sometimes setting x to the clipboard might throw exception
		try
			set selectionText to the clipboard
			if selectionText is not equal to "" then
				exit repeat
			end if
			
		end try
		delay 0.1
	end repeat
	
end tell

set myStringLength to length of selectionText
display dialog "The length of the string is " & myStringLength & " and the string is: " & selectionText

When I run this using the "Run Script" button, it works perfectly every time. However, when I use the assigned keyboard shortcut trigger (ctrl-cmd-o), it fails to copy any text and keeps the clipboard as the empty string, returning a length of 0.

I know this issue is not limited to the Notes app because I swapped out Notes for a different app in the script, and I had the same issue. I've also tried a few different keyboard shortcut triggers for it and had the same result.

EDIT: This same script also works if I use a trackpad trigger. I'm unsure if this is helpful to know.

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

Screenshots


image
image

Device information:

  • Type of Mac: 2021 M1 Pro MacBook Pro
  • macOS version: Ventura 13.3.1
  • BetterTouchTool version: 4.075

*Note: I cannot restart my system today to debug this because of a long process I have running, otherwise I would.

I think that is the problem, since the Ctrl and Command buttons are still pressed at the time the script is executed:

tell application "System Events"
	keystroke "c" using {command down}
    …

Keystroke or similar, may not work correctly in this case.

Try a trigger without Option, Ctrl and Command (e.g. fn o)

Edit: @Andreas_Hegenberg
An action "Wait for release trigger keys" would help here.

I think if you use the BTT predefined actions "ctrl up" and "cmd up" before running the script it might also solve the issue:

Unfortunately not. I have had problems with this myself several times and can't get a keystroke "c" using {command down} to work with the keyboard shortcut trigger ctrl-cmd-o. Only a delay .5 (or greater) seems to help.
image

Thank you both for your replies! I'll try changing the keyboard shortcut and/or adding a delay. I appreciate the quick response!

If that doesn't work, you can also ask BTT itself to send the cmd+c, it handles the already pressed modifier keys automatically.

This would send cmd+c (55 = cmd, 8 = c)

tell application "BetterTouchTool"
	
	trigger_action "{\"BTTTriggerType\":-1, \"BTTShortcutToSend\" : \"55,8\",}"
	
end tell

This AppleScript you just sent did work for me. Thank you!