I have several scripts that rely upon parsing Notification Center alerts/banners (like the kind generated by AppleScript’s display notification
… not the Cocoa NSNotificationCenter
):
- A script that grabs an OTP code from the current banner notification (either Mail.app or Messages.app)
- A script that re-authenticates my corporate Exchange email account (which requires re-authentication in System Settings every 9 days )
I’ve been using these scripts for the past ~10 years. The parsing is done with GUI scripting (through the macOS Accessibility framework), and typically needs minor updates with each major macOS release.
Unfortunately, I’ve hit a dead-end with macOS Sequoia. It doesn’t appear like the notification text (“Testing 123” in the example above) is exposed anywhere in AppleScript. I’m even trying to write a CLI utility to access the Accessibility framework through Swift (which unfortunately uses a C API, and is more painful that I’d like).
Note that the notifications now seem to be generated by the System with SwiftUI, and the AppleScript hierarchy now depends on whether the mouse is hovering over the notification (but regardless of the hover state, the text is not accessible in AppleScript).
I can see the element in Accessibility Inspector:
And UI Browser suggests the text can be pulled from the attribute “AXAttributedString” (note that the type is “unknown”):
However, this attribute’s value does not appear to be accessible from AppleScript (perhaps because of the “unknown” type above?):
tell application "System Events"
tell process "NotificationCenter"
if exists window 1 then
return attributes of button 1 of scroll area 1 of group 1 of group 1 of window 1
end if
end tell
end tell
And you can’t even ask for the attribute "AXAttributedDescription"
directly:
My questions, are therefore:
-
Does anyone know of any other ways to access the contents of notifications?
UNUserNotificationCenter
appears to be restricted to notifications generated by the current application. -
Is there is any was to use the Accessiblity framework with ASObjC?
My testing suggests the AppleScript interface deficiencies is a bug, a not a deliberate removal by Apple in macOS Sequoia. Any other thoughts?