[Implemented in 2.814] Improving dock badge energy efficiency

#1

Dock badges are such a great feature, but they notoriously hog energy. As I want to do more and more with dock badges, this is really holding me back, also they eat up my system resources which is no fun.

There must be a better way that doesn't use as much energy. Here is a generic dock badge script:

tell application "BetterTouchTool"
	try
		set dndEnabled to get_string_variable "dndEnabled"
	end try
end tell

if dndEnabled is "false" then
	if application "Safari" is running then
		tell application "System Events"
			tell process "Dock"
				try
					set badgeNumber to value of attribute "AXStatusLabel" of UI element "Safari" of list 1
					if badgeNumber exists 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
				on error
					return ""
				end try
			end tell
		end tell
	else
		return ""
	end if
else
	return ""
end if

The issue is that getting information directly from the Dock process using UI scripting is weirdly inefficient. It just takes more energy for some unknown reason.

Is there some native implementation that would be more energy efficient @Andreas_Hegenberg? I would love to just have dock badges on for all supported apps and have that be that, because I want to include badges for apps that support handoff and the like, apps that don't always run in your dock.

I don't have any answers yet but I wanted to start this discussion in the hopes that somebody might have an idea or two!

0 Likes

#2

Unfortunately these queries are always expensive.

It might be slightly better using the native Accessibility API to access this, but I'll need to look into that.

0 Likes

#3

Maybe there's a way to get all of the dock information at once instead of for each individual app, then parse the rest inside BTT? Then I'd only be running one expensive query and many less expensive ones.

0 Likes

#4

All API's I know only give that info for specific apps that need to be queried one at a time ;-(
But maybe there is some way? I'd also be very happy if we could find one, could use that at various places.

0 Likes

#5

This looks promising, I wonder what's stored in the dock plist?

0 Likes

#6

It contains some information (only for apps that are set to "Keep in Dock"), but doesn't include the badge number as far as I know. (Probably because that is dynamically added by the running app)

0 Likes

#7

How do you get the contents for the app switcher? It also shows badges. This doesn't cover handoff but it covers all running apps, which is the current case. So that'd still be significant progress.

0 Likes

#8

BTT uses the system app switcher

0 Likes

#9

Can you get the badge contents there, or is it just as expensive?

0 Likes

#10

Ah you mean on the Touch Bar. No that method only gives me the apps, not their badge.

0 Likes

#11

well, poop.

:poop:

back to google...

2 Likes

#12

The main issue is the badges are private to each app, unless I missed something here but this indeed is annoying..

Maybe terminal commands are more efficient?

I’m definitley following this one!

0 Likes

#13

Is there a terminal command that lets you get the contents of a dock badge? I've never heard of one.

0 Likes

#14

Not that I know of, and I don't know much to begin with..

It was just an idea off the top of my head

0 Likes

#15

Could JS for Automation be more efficient?

I'm just throwing ideas around here, I haven't looked if this is possible yet, sorry

0 Likes

#16

If anything JXA is less performant than Apple Script :smiley:
Apple seems to have abandoned it right after they released it ;-( (I still like it though)

1 Like

#17

Since it seems you have some experience with it, could you maybe try to whip up a JXA version of these?

Yeah it may be less performant in logic but who knows what the outcome would be, i mean unless there’s a solid reason we’d need to try it before we would definitely know right?

0 Likes

#18

Hi all,

Like you, I was really interested in Dock badges. I've found this Swift command line project in this BTT feature request.

It returns all Dock icons that have a badge and their badge value in this format Mail:2 Franz:3 Message:1. I initially wanted to run this once and parse results in TouchBar in different buttons but can't find a way to do it.
So I've edited it a bit to add an optional app name parameter, so that if I run /Applications/Utils/dock-notification-count Mail it returns "2". If no notification, it returns "", therefore hiding the TouchBar button. It works really well.

It is also using Accessibility API but yet I didn't see any performance issue using it (I've watched Activity Monitor but I can't find any anormal CPU usage). I run each button update every 10sec.

It may help you doing what you want. And if you have an idea for parsing and making only one call, I may be interested :wink:

0 Likes

#19

whoa, this sounds cool!

Could you try to put this setup in a seperate preset and post it here? how would this need to be setup?

very interesting!

0 Likes

#20

I just explain quickly because I'm going to bed :stuck_out_tongue:

Basically I've built this command line tool (from linked project in my previous post, with few modifications) : dock-notification-count.zip (2.3 MB)

Opening it will launch Terminal and display all Applications from the Dock that have a notification badge, and their associated number.

But as I want a button per each app that could potentially have a badge, I've edited the code to allow parameter. It can be launched from command line like this : /Applications/Utils/dock-notification-count Mail (note that I've moved the command line utility in Applications/Utils which is a custom folder where I put utilities like this). And as it can be launched from command line, it therefore can be launched through BTT :

I have a widget like this one for the 3 apps I want to listen for notifications, and they are executed every 10 seconds.

It looks like this :

The icons are the original on which I've added a red dot to simulate the notification badge :wink:

I hope it's clear but don't hesitate for more information :slight_smile:

0 Likes