In OSX, apps can have badges, typically used to show how many notifications they have. From another process, I would like to read the badge of an app. I do not have control over the app itself. How can reading the badge be done?
For a little more context: I'm building a touchbar widget for OSX Mojave that displays an icon on the touchbar when there are unread messages in slack. The reason why I don't want to use the slack API is network overhead: the Slack app already handles that networking, and so I don't want to waste bandwidth with extra requests. I'd rather have a general method, but if there's a slack-specific solution that's okay. Answers in any language are acceptable.
Intercept sent notifications from an Electron app is related, but asks about intercepting notifications rather than querying badges directly.
You can use AppKit APIs. A working solution was found here: https://github.com/pock/pock/blob/9ee414fc35abe933057c87fab082bb9e67f5a34c/Pock/Private/PockDockHelper/PockDockHelper.m
At a high level, you create a copy of the dock as an AXUIElementRef
, using AXUIElementCreateApplication
. This requires the app to be authorized on the accessibility settings. From there, you can use AXUIElementCopyAttributeValue
with kAXStatusLabelAttribute
to read the badge.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With