Have more than one regex-triggered image and color per Touch Bar widget

Some buttons have more than two states, for example the repeat key can be set to "off", "all", or "one". With BTT's current capabilities I can only toggle widgets between two states, but I really need n states. This means the currently implemented repeat button does not highlight properly.

I think a solution to this would be to allow users to add an arbitrary number of additional icons with +/- management controls, as well as regex text fields and and alternate bg color/text color fields for each. Interface-wise I think it could be similar to the weather widget, where you've got ten different states as options already.

Unfortunately this is unlikely to be added (at least for now) because it would need various changes to the data model as well.
However you can always return a image & color as base64 in the result JSON string.

1 Like


I was wondering if it would be possible to have the icon changed with the "log value" instead of the "return value" so that we would be able to have a change of icon and color on a certain logged value and have different value actually showing on the touchbar

I want this because I am making my own battery widget (i know there is one built into BTT but i need a bit more flexibility) and I would need to have an icon change for battery levels lower than 30% and still show the percentage on the touchbar, so I would have it "log 'low' " and"return 'battery percentage' "

You can use the Apple Script update_touch_bar_widget functions to update the icon at any time:

Alternatively you can persistently set a new icon using the update_trigger function.

1 Like

Wow, such a quick reply ! :smiley:
Thanks! I'll try it out!

Ok, so just tested but you see, I am making a preset to be shared (its called TouchBar Done Right if you wanna look it up :wink: ) and therefore it needs to be universal, I can't have people download an image and place it in a directory.. And thats why the icons are triggered on regex, but when I do the new title thingy, it still displays the old one

Maybe if you can see the script it would be easier to visualize so here it is (I put "acual percentage" as a string just in case I would be having problems inserting the var percentLeft but it still displays "half" and I also tried with the update_touch_bar_widget function and changing the text, but still no luck.. :frowning: ):

set chargeState to do shell script "pmset -g batt | awk '{printf \"%s %s\\n\", $4,$5;exit}'"
set percentLeft to do shell script "pmset -g batt | egrep -ow '([0-9]{1,3})[%]'" as text
set percentcheck to words of percentLeft

if (chargeState contains "Battery Power") then
	if (((item 1 of percentcheck as integer) < 85) and ((item 1 of percentcheck as integer) ≥ 65)) then
		return percentLeft
		if (((item 1 of percentcheck as integer) < 65) and ((item 1 of percentcheck as integer) ≥ 31)) then
			tell application "BetterTouchTool"
                return "half"
				update_trigger "A9368EFE-C120-4E44-8FF2-BFDC5B1F126A" json "{\"half\" : \"actual percentage\"}"
			end tell
			return ""
		end if
	end if
	return ""
end if

update_touch_bar_widget and update_trigger both provide the ability to update icons using a base64 string that you can include in your preset.

 "{\"half\" : \"actual percentage\"}

won't do anything, half is not a parameter, you can either use text, icon_path, icon_data or background_color in that method.

1 Like

Oh ok sorry, I'm still new to scripts and stuff..

You can get the base64 string for an icon using this terminal command

base64 ~/Desktop/youricon.png

sorry that's wrong :slight_smile: one moment

This is correct:
Be aware, this only works if the widget with the given UUID is a Run Script Widget

tell application "BetterTouchTool"
	update_touch_bar_widget "F09B6F95-0970-4922-A06C-511EBF32FE0B" text "some percentage" icon_data "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABXklEQVR42u3aoU7EQBAG4H/YXlAlFW2Cw5N6AvYCBEEdqppXQHIJklfAnKlC1fIC5LBNkCQ4mrSGKkKXQfAAcKG9btP/181l99uZbSc5gGGYKUe6+qEsy3STC0/TtJO1e10tqCzLUVaAN/UWIAAB+Bb4e5Ik2RWRO1U9FpFtlzaiqh8AHkTkMs/zspcKEJElgFMRce8kfw7kHMASwFlfLTAfQVXP+7wDxnBnzHgJbhLg+n5/0A3cXDwPC9DoC78DCEAAAhCAAAQgwDQBFidvg24gCIJhAf67ALYAATgNchpkCxCAAAQgAAEIQABOg5wG2QIE4DTIaZAtQAACEIAABCAAATgNchp0rgJUtRURp9tGVdveAOq6fgrD8MhlgLquV+s8v9bfvn3fP4jj+DaKokMRmTl28p9VVT0WRXHVNM2qFwBjjGet3QOwA2DLscP/AvBujHm11rZgGIZhfs83q+NYb7UJWxYAAAAASUVORK5CYII="
end tell
1 Like

Thank you so much!
I'll give it a try and let you know :smiley:

Well it worked like a charm ! well done :facepunch::ok_hand::ok_hand: