Auto-closing Touch Bar Groups

I have a couple of Touch Bar Groups that show merely informative information. Sometimes I forget to close one of these groups after I've opened it, which means the next time I need something on the Touch Bar I have to close the group first. How annoying! Recently, though, I've figured out a way to auto-close these informative groups.

  1. Instead of showing the button to open a group, we're going to hide that button and use another button with the action "Open Touch Bar Group With Name". Why? Because we need to run a bit of AppleScript as an additional action and groups can't have additional actions.

  2. Attach an additional action to the just-added button. For that additional action we're going to run some AppleScript (async):

     tell application "BetterTouchTool" to set_string_variable "B5599399-FC04-450E-9540-4DB216213C7A" to "1"
     delay 30 
     tell application "BetterTouchTool"
         set b to get_string_variable "B5599399-FC04-450E-9540-4DB216213C7A"
         if b as string is "1" then
             trigger_action "{\"BTTPredefinedActionType\" : 191}"
         end if
     end tell
    

    This script sets a variable and then waits. After the wait, if the variable is still set to the same value, close the group. The important part is the variable name; it can be whatever you want but it must be unique for each group. For example, I use the group UUID.
    In the example above delay 30 will make the timeout 30 seconds. Change it to be longer or shorter as you see fit.

  3. Now in the group itself, attach an additional action to the close button. We need to "clear" the timeout if you close the group via the close button. Set the action to run some AppleScript (blocking):

     tell application "BetterTouchTool" to set_string_variable "B5599399-FC04-450E-9540-4DB216213C7A" to "0"
    

    Notice that we're using the same variable name that we used in the other snippet of AppleScript. When you press the close button we set the variable to "0". In the first AppleScript snippet above, after the delay, the variable no longer equals "1" so we don't close the group. This prevents multiple groups from closing each other at the end of their timeouts. Closing one group and then opening another shouldn't cause the second group to be auto-closed by the first's timeout.

Thanks for the trick.
I tried something like that in the past but the variable was not possible at that time. Nice that this is possible now.

I think that I will put in the variable the "time" where to close the group (if not done) such that,
I would be able to change that time if I press some of the button by updating that variable. And it will also prevent issue if you open/close/re-open in less than 30s.

Thanks,

Olivier

EDIT:

Here is my modification to that idea.
code linked to the opening of the group

on time_to_int(inputdate)
	set {year:y, month:m, day:d, time:t} to inputdate
	return t + 24 * 3600 * d + 31 * 24 * 3600 * m + 365 * 24 * 3600 * (y - 2018)
end time_to_int

set now to current date
set now_int to time_to_int(now)
set end_time to now_int + 8
set close_time to (end_time as string)

tell application "BetterTouchTool" to set_string_variable "close_mail_group" to close_time 

set remaintime to 30
repeat while remaintime > 0
	delay remaintime
	
	tell application "BetterTouchTool"
		set close_time to get_string_variable "close_mail_group" 
	end tell
	if close_time as string is "0" then
		return
	end if
	set close_time_int to (close_time as integer)
	set now to current date
	set now_int to time_to_int(now)
	if close_time_int ≤ now_int then
		tell application "BetterTouchTool"
			trigger_action "{\"BTTPredefinedActionType\" : 191}" 
		end tell
		set remaintime to 0
	else
		set remaintime to close_time_int - now_int
	end if
end repeat
return 0

Now I added a named trigger launching (named "delay_close_mail_group") running the following script:

on time_to_int(inputdate)
	set {year:y, month:m, day:d, time:t} to inputdate
	return t + 24 * 3600 * d + 31 * 24 * 3600 * m + 365 * 24 * 3600 * (y - 2018)
end time_to_int

set now to current date
set now_int to time_to_int(now)
set end_time to now_int + 30
set close_time to (end_time as string)

tell application "BetterTouchTool" to set_string_variable "close_mail_group" to close_time 

and I call this trigger for each interaction with the touchbar inside the group.

Here is similar super easy way that uses mostly BTT with the new BTT variables and minimal applescript:

1-Hide the group 'open' button for your button group

2-Make two named triggers in the 'other' tab, one with an action to open and one to close the group. In this case "opencstrip" and "closecstrip" are the named trigger names.

3-Make a second touchbar button that opens and then closes(on a timer) the group through an applescript async action:

tell application "BetterTouchTool 2"
	
	set_number_variable "til_close" to 4
	 	
	trigger_named "opencstrip"
	
	repeat until ((get_number_variable "til_close") = 0)
		set_number_variable "til_close" to ((get_number_variable "til_close") - 1)
		delay 1
	end repeat
	    	
	trigger_named "closecstrip"
	
end tell

4-Add an additional action named trigger to each button in the group to delay the close if the button is pushed. Make one applescript async named trigger under the "other" tab and refer to it with each button:

tell application "BetterTouchTool 2"
	set_number_variable "til_close" to 2
end tell

Now, when you touch the button to open the strip it remains open for 4 seconds so you have time to choose a button. If you tap a button, it resets the countdown until close to 2 seconds, giving you enough time to hit another button. Each button pressed resets the counter to two.

Regards,
J

3 Likes