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

Wauw, this is great man! I used this on the Now Playing Widget. Long press will open spotify. One press will pause/play and double tap is play next song.

Really awesome, thanks a lot for the tip!

Robert

Great solution! Is it possible to do this for the functionality keys, e.g. f2, f3, etc? I can't find a "named trigger" for those keys...

When I get the time, I'll post a short video.

I'm not sure!

I hate the touchbar so much. :slight_smile:

1 Like

amazing! I use this for next song, just like apple earplugs. But, like earplugs, id like to be able to triple click for previous song. how can i do that?
thanks

You must have been reading my mind. I was trying to figure out how to do this so I can consolidate the Date/Time Widget down. I'm looking to single tap to pull up my Calendar Group and double tap to pull up my Clocks group. I'll play around with your script a bit.

Hi! How can I set up a double tap on the button so that it performs the specific action I need?
Thanks

This is great. I use this for mapping a mouse button to cmd + w (close windows) by triggering an async background applescript. I want to be able to close windows easily from my mouse, but don't want to accidentally close a window.

I also use Karabiner, which has first-class support for double-tap mappings using a to_delayed_action option (with to_if_invoked and to_if_canceled), which basically does an action, starts a timer, and does another action afterwards. Great for setting a variable that detects a double-tap, then un-setting it if not double-tapped.

I would LOVE it if BTT had this as a configuration option for all triggers/actions. @Andreas_Hegenberg can you comment on the likelihood of seeing delayed actions or double-click triggers natively supported in BTT?

How do you set this up so that something happens when you press a button once? Where does the code for pressing once go?

i can see multiple scenario here.

  1. an action which is done directly when you tap (and which would be executed twice within a double tap) then you just have to assign another action in top of the applescript above
  2. an action which is done only if they are no double tap (and therefore is not executed directly since you need to wait to know if this is a double tap or not). In that case you can edit the above applescript like this (which trigger another named trigger in that case):
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 myvar to get_string_variable "sec_click_e"
                if myvar is equal to "no" 
                    trigger_named "single_click_esc"
                end if
		set_persistent_string_variable "sec_click_e" to "no"
	end tell

end if

Thx for shared code..

I little bit fixed it, since it ddint work properly.. there should be "yes" and not "no" down there.. And I also made it for play/pause single click and next song for double click..

You need to have created triggers with actions for single and doble click in BTT for this to work. Names are:
System next song
System play pause

for one button with double tap option its just copy paste of this code into async apple script action atteched to the button you want to have this functionality.
Also see delay in code to tune it.

To make it work it needs turned external scripting in BTT setings -> Scripting BTT -> Allow external BetterTouchTool scripting

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

if is_second_click = "yes" then
	--double click (after first click)
	tell application "BetterTouchTool"
		set_persistent_string_variable "sec_click_e" to "no"
		trigger_named "System next song"
	end tell
	
else
	tell application "BetterTouchTool"
		set_persistent_string_variable "sec_click_e" to "yes"
	end tell
	
    --how long (in seconds) wait for second click? If not happened, then execute one click trigger
	delay 0.9

	--normal click	
	tell application "BetterTouchTool"
		set myvar to get_string_variable "sec_click_e"
		if myvar is equal to "yes" then
			trigger_named "System play pause"
		end if
		set_persistent_string_variable "sec_click_e" to "no"
	end tell	
end if`Preformatted text`