AppleScript running multiple times

Describe the bug
Use command plus H to trigger this Apple script, using the specifically for Microsoft teams, when I trigger it, it runs three times, I added a volume increase of 10% at the end of the script to know when it runs, and it does indeed run three times the amount of increase of volume from 0% to 30%.

Here's the Script:

tell application "System Events"
    -- Type "H"
    keystroke "H"
    delay 0.5
    -- Press Enter
    key code 36
    delay 0.5
    -- Press Command + /
    key down command
    delay 0.1
    keystroke "/"
    delay 0.1
    key up command
    delay 0.5
    -- Press Backspace
    key code 51
    delay 0.5
    -- Type "TEXT HERE"
    keystroke "TEXT HERE"
    delay 0.5
    -- Press the down arrow key
    key code 125
    delay 0.5
    -- Press Enter
    key code 36
	-- Increase the volume by a small amount
    set currentVolume to output volume of (get volume settings)
    set volume output volume (currentVolume + 10) -- Increase by 10%
end tell

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


Device information:

  • Type of Mac: BMP M1Pro 16/1To
  • macOS version: 14.5
  • BetterTouchTool version: 4.558

BTT only triggers a script once, however maybe your script is triggering BTT again? you mention cmd+h as s trigger, and you also call the h key from your script (while cmd is probably still pressed)

You're right

The H is a problem, I added

delay 0.5
at the very start of the script, And now it's fine.

Thanks @Andreas_Hegenberg !

alternatively you could probably add

tell application "BetterTouchTool"
	trigger_action "{BTTPredefinedActionType: 101}"
end tell

at the beginning and at the end of your script (it will trigger the "Disable/Enable BetterTouchTool" action

1 Like

so it stops any actions from being performed by BTT until the script is done?

[For example, I can't use my trackpad to increase the volume while the script is running]