Parsing Notifications in macOS Sequoia

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 :roll_eyes:)

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:
Screenshot 2024-11-01 at 5.00.18 PM


My questions, are therefore:

  1. 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.

  2. 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?

@tree_frog

So, I am still on Sonoma and not Sequoia, but I have spent more time than I am proud to admit dealing with GUI scripting the Notification Center. My initial goal was to create a way to clear push notifications and also clear out the Notification Center. What I have works, but I cannot guarantee it will work for Sequoia. Hopefully things haven’t changed too much between these two versions. I plan to upgrade soon, and I can try to be of more help if this doesn’t work. Anyway, I made some modifications and removed some logic for the sake of brevity. I hope this works for you:

delay 0.1
-- Test Notification
display notification "Test 123"
delay 0.1
tell application "System Events"	
    return the value of static text 2 of group 1 of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter"
end tell

Obviously, I am not checking to see if a notification exists nor am I handling the errors gracefully, but those are simple additions if you so desire.

If you have more than one notification push to you in a short time, you may need to change the numerical values associated with the groups, UI elements, etc…

Thanks Moose!

Yes, unfortunately that no longer works in Sequoia. There aren’t any accessible static texts anywhere in the hierarchy. I’ve manually inspected the entire thing in Script Debugger.

This is the hierarchy according to UI browser:

I have made some progress on a Swift-based command line utility that accesses the C Accessibility APIs. It does look as if the AppleScript interface is broken in Sequoia.

With the C Accessibility APIs, I can access a property that returns the title, optional subtitle, and message as a single string separated with ", ", but I don’t see any way to fully disambiguate the fields (i.e. if the message has commas in it).

If you’re reliant on the GUI scripting, my advice would be to delay the update to Sequoia until we’ve made some more progress! I wish I had. :neutral_face: