Creating Double-Tap Buttons!

I have a perennial problem with accidentally brushing touchbar icons. I hit the escape key all the time! One of the reasons I got BTT was so that I could add a long-press delay to the 'esc' button....but the built-in long press is long! A more convenient solution for me is to have a double-tap function. I figured out a straightforward way to do it. In this case, 'esc' (or any button for that matter) will do nothing unless it is tapped twice, quickly.

Step 1 - Create a touchbar button, and set it to trigger an asynchronous applescript. The async is important if you want the wait for a second tap to timeout and reset after a while. Leave the applescript blank for now.

Step 2 - Before adding the actual applescript, create a named trigger under "other triggers" that performs the function you want. In my case, it simply executes the 'esc' key.

Step 3 - Use the following applescript as the action in your touchbar button created in step 1, but change the trigger_named to the name of the named trigger you created in step 2. You may also change the delay to any number of seconds you like - this is the second-tap-reset timeout.

tell application "BetterTouchTool 2"
	set is_second_click to get_string_variable "sec_click_e"
end tell

if is_second_click = "yes" then
	
	tell application "BetterTouchTool"
		set_persistent_string_variable "sec_click_e" to "no"
		trigger_named "esc"
	end tell
	
else
	tell application "BetterTouchTool"
		set_persistent_string_variable "sec_click_e" to "yes"
	end tell
	
	delay 2
	
	tell application "BetterTouchTool"
		set_persistent_string_variable "sec_click_e" to "no"
	end tell
end if

Last - you can use this method for any number of double-tap buttons. Just repeat the above steps...BUT, make sure you change the name of the persistent string variable for each one so that they don't interfere with one-another.

Thanks!
J

Nice idea and setup. I will use this double click idea.

For my understanding, what is the point to use persistent variable here?

Thanks for sharing,

Olivier

Hi Oliver,

A persistent variable is not required. I used one just in case I do a quit/restart in the middle of an action. I know it isn't very likely :slight_smile:

I've also used this same code structure (with a small modification.) to create a simple toggle on/off button.

Joe

When I input the code and change "BetterTouchTool 2" to "BetterTouchTool" it still doesn't work. Nothing happens when I double tap. It just kept repeating the intended trigger of launching a specific app over and over again. I don't really have any knowledge of code, so maybe you can help me solve this.

Hello!

I removed the "2" from my post. My BTT app must just be named differently.

Anyway - a bit of clarification:

1-Did you set the assigned action of the "esc" button on the touchbar to be "Run Apple Script (async in background)"

2-Then, put the apple script in that action's script content box

3-Then create a 'Named Trigger" under "Other triggers"

4-Call the named trigger "esc"

5-Make the action of the "named trigger" "ESC (Escape Key, repect passed modifiers)"

Let me know if this helps!

J