[Implemented in 2.814] Improving dock badge energy efficiency

@GoldenChaos @yuuiko
Next alpha later today will have a new apple script function that can be used like this:

tell application "BetterTouchTool"
	get_dock_badge_for "Calendar" update_interval 5
end tell

When an update interval is provided, BTT will internally refresh the badges for all apps every x seconds. In this case the "get_dock_badge_for" function calls are basically free, as they will just return the last cached value. This is the recommended way to do it, and the update_interval should not be too small.

If you need to refresh the cache immediately for some reason, you can leave the update_interval function out. Then BTT will immediately refresh all the dock badges and return the latest value.

tell application "BetterTouchTool"
	get_dock_badge_for "Calendar"
end tell

Instead of the app name you can also use its url (to make it independent from any language), e.g. file:///Applications/Google%20Chrome.app/

1 Like

Hope this works!!

Will these have a โ€œdonโ€™t show when DND ONโ€ function?

Otherwise a dnd variable would be great. A couple of my users are struggling with the badges always hiding and iโ€™m not sure whatโ€™s causing it

The function just returns the current dock badge number for the requested app efficiently, what you do with it is up to you :slight_smile:

Ok, just added a variable for that. It's available via "SystemDoNotDisturbState" as a number variable.

how does it sense dnd? just curious :wink:

It reads the user defaults of the notificationcenterui

NSUserDefaults* defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.notificationcenterui"];
BOOL dnd = [defaults boolForKey:@"doNotDisturb"];
1 Like

shoot wait

could I have just used defaults read then? aha well itโ€™s coming :joy:

Not sure if a normal read would work because it may not access the "live state", but yeah probably it would :slight_smile:

1 Like

Oh another thing, what does the new badge function return? The same as now right?

Especially for handoff. Just wondering if I need to rewrite the display rules of the widget or they can stay as is

In addition I think the docs need an update too

It just returns the current/last cached dock badge as a string.

So e.g. "2" or "lalala"

I'll do a big docs update next week that is based on the new UI and everything :slight_smile:

1 Like

It's now integrated in v2.814 alpha

Giving this a shot now! Expecting to release a new experimental version today with this + dnd detection updates + the initialization fix for the settings menu. :smiley:

EDIT: Alright, this is fully implemented in 2.814!!

@Andreas_Hegenberg seeing a potential issue with the badge numbers updating. I got a text and the badge number for Messages returned 1, but after reading the message the number variable continues to return 1, meaning the dock badge never disappears. Restarting BTT fixes the variable discrepancy. Seems like the number is just failing to update itself.

did you provide an update interval? you still need to execute the script every time you want a new value - the update interval just updates the internal btt variable, your script needs to still retrieve it in regular intervals. This means your script won't take cpu, but it still needs to run

Yup - here's the new script for a dock badge, which itself runs every 5s:

tell application "BetterTouchTool"
	try
		set dndEnabled to get_number_variable "SystemDoNotDisturbState"
	end try
end tell

if dndEnabled is 0 then
	tell application "BetterTouchTool"
		set badgeNumber to get_dock_badge_for "Google Chrome Canary" update_interval 5
	end tell
	if badgeNumber is not missing value then
		if badgeNumber contains "iPhone" then
			return "iPhone"
		else if badgeNumber contains "iPad" then
			return "iPad"
		else if badgeNumber contains "watch" then
			return "Watch"
		else if badgeNumber contains "Mac" then
			return "Mac"
		else
			return badgeNumber
		end if
	else
		return ""
	end if
else
	return ""
end if

I will try it later when I'm back at home. Maybe there is still some bug that prevents the update :slight_smile:

Your code looks good.

1 Like

It seems to specifically happen when the dock badge value changes to missing value, if that helps. BTT doesn't seem to be able to cope with going from having a badge to not having one. Updating from one value to another, however, works properly.

ah thanks for finding that! I forgot to delete them :slight_smile:
Will upload a new build in a few minutes.

2 Likes

Can confirm fixed in 2.817 :slight_smile:

1 Like

Oh how beautifully smooth

@GoldenChaos Just let me nab a few of those new apps... :eyes: aand it's perfect.

1 Like