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!
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.
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.
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)
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.
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?
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
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 :