macosautomationnotificationsapplescript

Read MacOs Notifications while screen is locked AppleScript


I have managed to put together an AppleScript that reads the notifications and outputs the text of these. While it works while the display is on and unlocked, I haven't been able to read the notifications while on the lock screen.

#!/usr/bin/osascript
on run
    delay 6.0 -- wait for locking the screen
    tell application "System Events"
        try
            set _groups to groups of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter"
            repeat with _group in _groups
                return the value of static text 1 of _group --limit to one notification for the moment
            end repeat
        end try
    end tell
end run

This is my current code, which successfully reads the notification title of the most recent notification on macOS Ventura. However, after locking the screen (⌘ + Ctrl + Q) and trying to read the notification, the code doesn't output anything, even though the notification is still visible in the same location.

Final Goal:

To execute this script (with the required error handling) every 10 seconds, even when the display is locked, and for it to read the notification to pass it on to a different script.


If there is any other way to read macOS notifications through a script, they're also welcomed.


Solution

  • This is a python script with which you can get notification contents:

    https://github.com/ydkhatri/MacForensics/blob/master/macNotifications.py