Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSX - How can I read an app's badge from another process?

Tags:

macos

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?

Badge in slack

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.

like image 257
Kofthefens Avatar asked Sep 05 '25 02:09

Kofthefens


1 Answers

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.

like image 170
Kofthefens Avatar answered Sep 07 '25 23:09

Kofthefens