Multi-Button Widget for Touch Bar

I have some touch bar widgets set up that utilize AppleScript to read a local file and extract information to display on the button. I now have 20-30 buttons all running AS to access this file and extract data. In the interest of resources, I feel like it could be nice to be able to define a response that contains multiple buttons instead of attributes for a single button. Instead of returning a JSON object with properties for the button, it could be handy if BTT supported receiving an array of JSON objects, each defining a button.

In order to work effectively, there would likely need to be a few basic styling options added such as borders (all, left, right), padding (integer), border radius (integer), and position (inline, left, right). If this was possible, then the overall number of processes could be greatly reduced in certain use cases.

I would use the crap out of this. The existing add_trigger functionality is not the same as this - the ability to format a constantly changing arbitrary return value as a series of buttons.

In particular this would be useful for the Language Picker, Browser Tabs, Calendar Actions and Reminder Actions widgets in my preset, which currently cap at 10 items per list. With this functionality I could reduce those scripts to a single widget and output an arbitrary number of items.

Yeah. My big use at the moment is a set of connected button groups that I'm building to display weather information from my personal weather station.

I've set up a LaunchAgent to pull the latest conditions from my PWS software's web server as JSON and then store it in ~/.pws as a .json file. I then have numerous widgets that grab various variables out of that JSON file and display them.

It's displayed in a multi-step process. On the main display, it's simply a temperature next to the clock. When tapped, it opens up a current condition overview of each parameter. Each of those elements is linked to another button group which is a detailed daily view of that particular element. Each one of these buttons has to query the same JSON file to get its needed value to display.

It would be so much easier (and resource-friendly) to simply be able to run a single AS that could just define all the buttons in a single view via one of these hypothetical "multi-button widgets."

1 Like

I agree this would be useful. It's a bit complicated to add though as BTT currently doesn't have a real concept of "temporary" items. I'll think about how do achieve something like it.

@buffaloseven, if I understood correctly you don't really need dynamically created items, in that case you should be able to achieve the wanted behavior by executing an apple script when opening the group. Then you can use the update_touch_bar_widget (for script widgets) or update_trigger functions in that script to update your items: https://docs.bettertouchtool.net/docs/apple_script.html

Hmmm...that would almost suffice and could be nice because I could tie the BTT UI update to my LaunchAgent so that when it grabs new data it triggers the button update.

Is the external AS support capable of updating all the same properties as JSON returned in an AS widget? The doc only mentions text, icon_path, and background_color, but the AS widgets also support font_color, font_size, and icon_data. I utilize font color/icon data in particular in combination with background colours to create multiple types of conditional highlighting depending on variable values. Since these are weather conditions, I have conditional highlighting for near-warning and warning-level events, as well as alternate displays for some elements. A simple example is that if my personal weather station is reporting a rain rate, I add a rain icon to the temperature display on the main touch bar view. A more complex example is multi-stage conditional highlighting to create simple view states for fresh data, stale data, and out of date data.

If the external AS controls are able to control all the elements, then it probably would be a handy, resource-efficient way to manage the updates that can be tied directly to my routines to actually fetch the data.

Note: I'd test it myself, but I'm away from my Mac for a few hours, so I just wanted to ask and get right to refactoring it all if it can meet my needs.

yes, it can update everything!

Well that sounds great. I'm trying it out and I'm having a small issue maybe you could provide guidance on.

First is, does the update_touch_bar_widget function support both regular buttons as well as widgets or just widgets? And if just widgets, what type?

I've tried creating a TouchBar Button titled Touch Bar Temp, then I wrote this AS:

set my_shared_secret to "s6Ed8nWXBEJeaRb?"

-- Update UI Elements
tell application "BetterTouchTool"
	update_touch_bar_widget "C47EA42F-5C5D-41CE-955D-D729760AD7A0" text "25Β°C" background_color "0,0,0,255" shared_secret my_shared_secret
end tell

But when I run it, nothing happens. I tried making a widget as well, but I had the same problem. I've enabled scripting support in the settings and made sure my shared secret matches, and I've triple checked to make sure I'm grabbing the UUID of the correct button. I feel like there must be something simple I'm missing, but I can't quite figure it out?

EDIT: I think I figured it out to the point where I can get widgets to update (I've just blanked the AS in the widget config and set the execution time to 9999), but it appears that text colour and font size cannot be updated via external scripting (or at least I can't figure it out).

True, currently only the update_trigger function can update all the properties. The update_touch_bar_widget can only update text, background color and icon. I'll update this with one of the next versions.

However I think it would be most performant if you'd use normal buttons, and update them using the update_trigger function.

I like to use the Java Script for Automation syntax, because supporting JSON is much easier here:

var BetterTouchTool = Application('BetterTouchTool');

var updateDefinition = {
  "BTTTouchBarButtonName" : "test23",
  "BTTTriggerType" : 629,
  "BTTTriggerClass" : "BTTTriggerTypeTouchBar",
  "BTTPredefinedActionType" : -1,
  "BTTPredefinedActionName" : "No Action",
  "BTTEnabled2" : 1,
  "BTTEnabled" : 1,
  "BTTOrder" : 10,
  "BTTTriggerConfig" : {
    "BTTTouchBarFreeSpaceAfterButton" : 5,
    "BTTTouchBarAlternateBackgroundColor" : "0.000000, 0.000000, 0.000000, 0.000000",
    "BTTTouchBarButtonCornerRadius" : 6,
    "BTTTouchBarFontColor" : "0.000000, 100.773549, 84.335715, 255.000000",
    "BTTTouchBarButtonName" : "test",
    "BTTTouchBarItemPlacement" : 0,
    "BTTTouchBarButtonColor" : "225.671760, 81.814139, 74.701643, 255.000000",
    "BTTTouchBarItemIconWidth" : 22,
    "BTTTouchBarItemPadding" : 0,
    "BTTTouchBarItemIconHeight" : 22
  }
}

BetterTouchTool.update_trigger("CC156217-42DF-44C3-AB9C-50CE3F65A80D", {json: JSON.stringify(updateDefinition)});

Hmmm...I see how that works. That probably would be the best way. I've updated my script to be contained within a single scpt file that is triggered by the "valid time" displays in each of my 5 button groups as well as by my LaunchAgent. Moving from AS widgets to standard buttons is obviously preferable, and I think I can easily compile the JSON in AS using JSON Helper. I'd prefer not to rewrite the whole script at the moment, and I'm not familiar with JSA syntax and Apple's documentation is kind of terrible.

That said, already this is massively more performant than before with BetterTouchToolApplescriptRunner now hovering around 0 to 2% in Activity Monitor instead of the 10-15% it was before. Thanks so much for your help so far.

That said, getting back to my original request, I still think I see a value for dynamic buttons. For example, in my main BTT temperature display, it would be great if my AS could check for significant weather in the other parameters (wind speeds, rainfall amounts), and dynamically create buttons that open the respective parameter group. It's not quite as "urgent" a request since you've shown me a feature I was unaware of that has solved my primary problem: performance.

I'm going to embed my script here so far just in case there's some obvious BTT feature I'm missing. It's a bit big and there's a more to do. I'll try converting things to standard buttons in the coming week.

-- Still to do:
--	- Add detailed views for humidity
--	- Add detailed view for pressure
--	- Re-develop all the conditional highlighting routines
--	- Define colours for each highlight state-data freshness combination


-- This section stores the UUID for each button that will be updated with fresh weather data.
-- Primary TB Display
set main_temperature to "D929545C-D98D-408D-AC96-89469CC1FA6E"
-- Overview PWS Display
set overview_temperature to "7A1434B8-8E56-46F8-86D9-442AED0B6E62"
set overview_wind to "F03865B3-2458-472B-B607-C3E0DA358A15"
set overview_precip to "A4A40194-821D-410B-81D7-77316BBC478D"
set overview_dewpoint to "EF960A7A-C0D9-4C14-833A-399B764ABDEC"
set overview_humidity to "E2C885F0-9C8B-405B-9271-CB6E2CD7C129"
set overview_pressure to "4DBDF23C-918A-4D95-B6E4-91E9AFFC49F8"

set valid_time to {"56AD0CC2-8C3D-4740-9483-0E45AD856A73", "9F1072E0-E19B-476E-B008-40F9CC13F990", "6B72BB64-58DC-484D-8E41-0FF2C062410E", "2586DB7A-99C5-459F-B4FC-7CECBBE3A107", "D49C45C3-1D13-4D18-94D2-7C66F8F8DD68"}

-- Detailed View: Temperature
set detailed_temperature_current to "BCF48091-B7B1-4AC3-A804-66D283E1FA19"
set detailed_temperature_trend to "FA49A1D3-DB9A-447D-8694-CB39729159BF"
set detailed_temperature_daily_high to "36C0F984-6567-41BB-8962-382B74F691E2"
set detailed_temperature_daily_low to "AC22F34B-6D3B-454F-B825-86F17BB81DA9"

-- Detailed View: Wind
set detailed_wind_current to "34382C26-DDD1-438E-A508-6267E3447476"
set detailed_wind_peak_sustained to "CFC924D2-F0C2-4E0B-B44A-12D14A37DEF7"
set detailed_wind_peak_gust to "D22CD0CF-A368-4854-8CD1-C1B7C4B47B93"
set detailed_wind_dominant_direction to "790E9E0F-BD16-4F7F-A850-1C07106DDDE8"

-- Detailed View: Precipitation
set detailed_precip_rate_current to "8555D85E-9F55-4948-B56D-E3574148C193"
set detailed_precip_amount_last_hour to "74E01A22-9A44-4DAC-BD13-4147CBD1F7F6"
set detailed_precip_amount_today to "8DFF54E4-5C8A-4C71-BFBB-95F46BDE4F23"
set detailed_precip_rate_daily_max to "858D2DEE-934F-4F43-A4F8-FFB6A79D44CF"
set detailed_precip_amount_twentyfour_hours to "3745AD44-2CF0-4248-9369-650EBD812EF7"
set detailed_precip_amount_yesterday to "67C3D28C-8D71-49F6-AC80-99056D335EAF"
set detailed_precip_amount_storm to "5D94D946-4E74-4D6F-8B75-5CB67F4D5D56"

-- Detailed View: Humidity
set detailed_humidity_dew_point_current to "D93D6345-1874-4E2E-AC6F-5C371EF7B1A5"
set detailed_humidity_dew_point_daily_max to "74180204-B5E1-44D8-BB00-D1791E39B8C5"
set detailed_humidity_dew_point_daily_min to "C84C4EE7-0951-49F6-A3D9-205ABA6F2559"

-- Set Color Variables for Data Freshness
set text_fresh to "255,255,255,255"
set bg_fresh to "75,75,75,255"

set text_stale to "178,178,178,255"
set bg_stale to "135,105,63,255"

set text_expired_red to "255,223,222,225"
set bg_expired_red to "155,54,63,255"

set text_expired to "100,100,100,255"
set bg_expired to "75,45,45,255"

-- Set Color Variables for Conditional Highlighting Based on Thresholds

set bg_elevated to "217,168,54,255"
set bg_alert to "232,91,86,255"

-- Other Variables Needed
set my_shared_secret to "s6Ed8nWXBEJeaRb?"
set json_target to "/Users/Brad/.glenwood_pws/api.json"
set font_size to 13

-- Load JSON Data
set json_values to readFile(json_target)

tell application "JSON Helper"
	try
		set weather to read JSON from json_values
	on error errMsg
		return "Can't create JSON"
	end try
end tell


-- Determine data state and set colour variables
set updateTime to (LastDataRead of weather as string)
set updateTimeString to "Valid at " & updateTime
set timeArray to my splice(LastDataRead of weather as string, ":")
set currentTime to getTimeInHoursAndMinutes()

-- Set overall appearance based on data freshness
if (first item of timeArray as number) is not equal to (first item of currentTime as number) then
	set bg_main to bg_expired
	set text_main to text_expired
	
	set bg_button to bg_expired
	set text_button to text_expired
else if ((second item of currentTime as number) - (second item of timeArray as number)) is greater than 15 then
	set bg_main to bg_stale
	set text_main to text_stale
	
	set bg_button to bg_stale
	set text_button to text_stale
else
	set bg_main to "0,0,0,255"
	set text_main to text_fresh
	
	set bg_button to bg_fresh
	set text_button to text_fresh
end if

-- Retrieve Data

-- RD: Temperatures
set data_current_temperature_rounded to round (outdoorTemp of weather as real)
set data_current_temperature to (outdoorTemp of weather as real)
set data_temperature_unit to (TempUnit of weather as string) as string
set data_temperature_trend to (TempTrend of weather as real)
if data_temperature_trend is greater than 0 then
	set data_temperature_trend_string to "Trend: +" & data_temperature_trend & data_temperature_unit
else
	set data_temperature_trend_string to "Trend: " & data_temperature_trend & data_temperature_unit
end if
set data_temperature_daily_high to HighTempToday of weather as number
set data_temperature_daily_low to LowTempToday of weather as number


-- RD: Wind
set data_wind_rounded to round (WindAverage of weather as number)
set data_wind_gust_rounded to round (Recentmaxgust of weather as number)
set data_wind_unit to (WindUnit of weather as string)

if (data_wind_rounded is less than 5) and (data_wind_gust_rounded is less than 10) then
	set data_wind_overview_string to "Calm"
else if (data_wind_rounded is less than 5) and (data_wind_gust_rounded is greater than 9) then
	set data_wind_overview_string to "Up to " & data_wind_gust_rounded & data_wind_unit
else
	set data_wind_overview_string to (data_wind_rounded as string) & "G" & data_wind_gust_rounded & " " & data_wind_unit
end if

set data_wind_bearing to (Avgbearing of weather as number)
set data_wind_compassDirections to {"Nil", "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"}
set data_wind_bearing to round ((data_wind_bearing mod 360) / 22.5) + 1
set data_wind_bearing to (item data_wind_bearing of data_wind_compassDirections)
set data_wind_detailed_current to "Current: " & data_wind_bearing & " " & data_wind_rounded & " gusting " & data_wind_gust_rounded & " " & data_wind_unit
set data_wind_daily_peak_sustained to HighWindToday of weather as number
set data_wind_daily_peak_gust to HighGustToday of weather as number
set data_wind_dominant_direction to DominantWindDirection of weather as string

-- RD: Precipitation
set data_daily_rain to (RainToday of weather as real)
set data_precip_unit to (RainUnit of weather as string)
set data_precip_rate_unit to data_precip_unit & "/hr"

set data_precip_rate_current to RainRate of weather as number
set data_precip_amount_last_hour to RainLastHour of weather as number
set data_precip_amount_today to RainToday of weather as number
set data_precip_rate_daily_max to HighRainRateToday of weather as number
set data_precip_amount_twentyfour_hours to RainLast24Hour of weather as number
set data_precip_amount_yesterday to RainYesterday of weather as number
set data_precip_amount_storm to StormRain of weather as number

-- RD: Humidity
set data_humidity_dewpoint_current to OutdoorDewpoint of weather as real
set data_humidity_dewpoint_current_rounded to round (OutdoorDewpoint of weather as number)
set data_humidity_dew_point_daily_max to HighDewpointToday of weather as number
set data_humidity_dew_point_daily_min to LowDewpointToday of weather as number
set data_humidity_rh_current to OutdoorHum of weather as number
set data_rh_unit to "%"
set data_humidity_rh_daily_max to HighHumToday of weather as number
set data_humidity_rh_daily_min to LowHumToday of weather as number


set data_current_pressure to Pressure of weather as number
set data_current_pressure_rounded to round (Pressure of weather as number)
set data_pressure_unit to PressUnit of weather as string

-- Set Thresholds
set threshold_wind_sustained_elevated to 15
set threshold_wind_sustained_alert to 25
set threshold_wind_gust_elevated to 35
set threshold_wind_gust_alert to 50

-- Update UI Elements
tell application "BetterTouchTool"
	-- Primary TB Display
	-- Contextual string modification to display rain emoji if the PWS is reporting a rain rate
	if data_precip_rate_current is greater than 0 then
		update_touch_bar_widget main_temperature text ("🌧 " & data_current_temperature_rounded & data_temperature_unit) as string background_color bg_main shared_secret my_shared_secret
	else
		update_touch_bar_widget main_temperature text (data_current_temperature_rounded & data_temperature_unit) as string background_color bg_main shared_secret my_shared_secret
	end if
	
	-- PWS Overview Group
	update_touch_bar_widget overview_temperature text (data_current_temperature & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	-- Highlight Winds Based on Sustained or Gust Thresholds
	if (data_wind_rounded is greater than threshold_wind_sustained_elevated) or (data_wind_gust_rounded is greater than threshold_wind_gust_elevated) then
		update_touch_bar_widget overview_wind text data_wind_overview_string background_color bg_elevated shared_secret my_shared_secret
	else if (data_wind_rounded is greater than threshold_wind_sustained_alert) or (data_wind_gust_rounded is greater than threshold_wind_gust_alert) then
		update_touch_bar_widget overview_wind text data_wind_overview_string background_color bg_alert shared_secret my_shared_secret
	else
		update_touch_bar_widget overview_wind text data_wind_overview_string background_color bg_button shared_secret my_shared_secret
	end if
	
	update_touch_bar_widget overview_precip text (data_daily_rain & data_precip_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget overview_dewpoint text (data_humidity_dewpoint_current_rounded & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget overview_humidity text (data_humidity_rh_current & data_rh_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget overview_pressure text (data_current_pressure_rounded & data_pressure_unit) as string background_color bg_button shared_secret my_shared_secret
	
	-- Detailed Temperature Group
	update_touch_bar_widget detailed_temperature_current text "Current: " & (data_current_temperature & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_temperature_trend text data_temperature_trend_string as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_temperature_daily_high text "High: " & (data_temperature_daily_high & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_temperature_daily_low text "Low: " & (data_temperature_daily_low & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	
	-- Detailed Wind Group
	update_touch_bar_widget detailed_wind_current text data_wind_detailed_current background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_wind_peak_sustained text "Peak Sustained: " & (data_wind_daily_peak_sustained & " " & data_wind_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_wind_peak_gust text "Peak Gust: " & (data_wind_daily_peak_gust & " " & data_wind_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_wind_dominant_direction text ("Dominant Direction: " & data_wind_dominant_direction) background_color bg_button shared_secret my_shared_secret
	
	-- Detailed Precipitaiton Group
	update_touch_bar_widget detailed_precip_rate_current text ("Current Rate: " & data_precip_rate_current & " " & data_precip_rate_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_precip_amount_last_hour text ("Last Hour: " & data_precip_amount_last_hour & " " & data_precip_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_precip_amount_today text ("Rain Today: " & data_precip_amount_today & " " & data_precip_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_precip_rate_daily_max text ("Max Rain Rate Today: " & data_precip_rate_daily_max & " " & data_precip_rate_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_precip_amount_twentyfour_hours text ("24hr. Rain: " & data_precip_amount_twentyfour_hours & " " & data_precip_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_precip_amount_yesterday text ("Rain Yesterday: " & data_precip_amount_yesterday & " " & data_precip_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_precip_amount_storm text ("Rain Today: " & data_precip_amount_storm & " " & data_precip_unit) as string background_color bg_button shared_secret my_shared_secret
	
	-- Detailed Humidity Group
	update_touch_bar_widget detailed_humidity_dew_point_current text ("Current: " & data_humidity_dewpoint_current & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_humidity_dew_point_daily_max text ("Max Today: " & data_humidity_dew_point_daily_max & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	update_touch_bar_widget detailed_humidity_dew_point_daily_min text ("Min Today: " & data_humidity_dew_point_daily_min & data_temperature_unit) as string background_color bg_button shared_secret my_shared_secret
	
	repeat with widget in valid_time
		update_touch_bar_widget widget text updateTimeString background_color bg_main shared_secret my_shared_secret
	end repeat
end tell


-- Custom Functions
on splice(textString, delimiter)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delimiter
	set spliceArray to every text item of textString
	set AppleScript's text item delimiters to oldDelimiters
	return spliceArray
end splice

on getTimeInHoursAndMinutes()
	-- Get the "hour"
	set timeStr to time string of (current date)
	set Pos to offset of ":" in timeStr
	set theHour to characters 1 thru (Pos - 1) of timeStr
	set hourString to ""
	repeat with letter in theHour
		set hourString to (hourString & letter)
	end repeat
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	-- Get the "minute"
	set Pos to offset of ":" in timeStr
	set theMin to characters 1 thru (Pos - 1) of timeStr as string
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	-- Get AM or PM
	set Pos to offset of " " in timeStr
	set theSfx to characters (Pos + 1) through end of timeStr as string
	if theSfx is "PM" and (hourString as number is not 12) then
		set hourString to ((hourString as number) + 12) as string
	else if theSfx is "AM" and (hourString as number is 12) then
		set hourString to ((hourString as number) - 12) as string
	end if
	
	return {hourString, theMin}
end getTimeInHoursAndMinutes

on readFile(unixPath)
	set foo to (open for access (POSIX file unixPath))
	set txt to (read foo for (get eof foo) as Β«class utf8Β»)
	close access foo
	return txt
end readFile

I've been experimenting with changing to using update_trigger instead of update_touch_bar_widget, but I'm having an issue where it seems like any call of Application('BetterTouchTool').update_trigger() alters the display back to the primary touch bar configuration (i.e. it kicks me out of any button groups). This isn't particularly desirable as I don't want to be kicked out of a particular display just because the text for a button is updated.

Is this a limitation of buttons vs. widgets, or have I stumbled upon a bug? For reference's sake, here's a small JXA script I made to display the star rating of a track in iTunes:

var token = "s6Ed8nWXBEJeaRb?";
var star_triggers = {
    "one": "15F96D92-DCE1-44DB-8A48-A62D51B925A8",
    "two": "506A1846-142F-4AF4-A10E-3213326A12D2",
    "three": "D6473523-E33A-48A5-A5F3-991B9A07F174",
    "four": "184B70B5-4F11-4EA0-A362-C5790DDB537E",
    "five": "C6709C77-66C2-45A0-981F-5D4B710FFB0D"
}
if (Application('iTunes').running()) {
    var iTunes = Application('iTunes');
    var BetterTouchTool = Application('BetterTouchTool');
    try {
        rating = getTrackRating();
        console.log(rating);
        if (rating < 20) {
            BetterTouchTool.update_touch_bar_widget(star_triggers.one, {
                "text": "β˜†",
                "shared_secret": token
            });
            apply_rating_to_trigger(star_triggers.two, 0);
            apply_rating_to_trigger(star_triggers.three, 0);
            apply_rating_to_trigger(star_triggers.four, 0);
            apply_rating_to_trigger(star_triggers.five, 0);
        } else if (rating == 20) {
			BetterTouchTool.update_touch_bar_widget(star_triggers.one, {
                "text": "β˜…",
                "shared_secret": token
            });
            apply_rating_to_trigger(star_triggers.two, 0);
            apply_rating_to_trigger(star_triggers.three, 0);
            apply_rating_to_trigger(star_triggers.four, 0);
            apply_rating_to_trigger(star_triggers.five, 0);
		} else if (rating == 40) {
			BetterTouchTool.update_touch_bar_widget(star_triggers.one, {
                "text": "β˜…",
                "shared_secret": token
            });
            apply_rating_to_trigger(star_triggers.two, 1);
            apply_rating_to_trigger(star_triggers.three, 0);
            apply_rating_to_trigger(star_triggers.four, 0);
            apply_rating_to_trigger(star_triggers.five, 0);
		} else if (rating == 60) {
			BetterTouchTool.update_touch_bar_widget(star_triggers.one, {
                "text": "β˜…",
                "shared_secret": token
            });
            apply_rating_to_trigger(star_triggers.two, 1);
            apply_rating_to_trigger(star_triggers.three, 1);
            apply_rating_to_trigger(star_triggers.four, 0);
            apply_rating_to_trigger(star_triggers.five, 0);
		} else if (rating == 80) {
			BetterTouchTool.update_touch_bar_widget(star_triggers.one, {
                "text": "β˜…",
                "shared_secret": token
            });
            apply_rating_to_trigger(star_triggers.two, 1);
            apply_rating_to_trigger(star_triggers.three, 1);
            apply_rating_to_trigger(star_triggers.four, 1);
            apply_rating_to_trigger(star_triggers.five, 0);
		} else if (rating == 100) {
			BetterTouchTool.update_touch_bar_widget(star_triggers.one, {
                "text": "β˜…",
                "shared_secret": token
            });
            apply_rating_to_trigger(star_triggers.two, 1);
            apply_rating_to_trigger(star_triggers.three, 1);
            apply_rating_to_trigger(star_triggers.four, 1);
            apply_rating_to_trigger(star_triggers.five, 1);
		}
    } catch (e) {

    }
}

function getTrackRating() {
    track = iTunes.currentTrack();
    return track.rating();
}

function apply_rating_to_trigger(target, filled) {
    var star_type;
    if (filled) {
        star_type = "β˜…";
    } else {
        star_type = "β˜†"
    }
    BetterTouchTool.update_trigger(target, {
        json: JSON.stringify({
            "BTTTouchBarButtonName": star_type,
        }),
        shared_secret: token
    });
}
1 Like

I also agree that the behavior of being booted out of open groups when a button updates is very frustrating. Would be great if this could be changed.