Get text of most recent notification with applescript I would like to be able to grab the text of the most recent notification and pass it into an applescript variable. How can I do that?
Edit: here's what I have so far:
on run
    tell application &quot;System Events&quot;
        tell process &quot;NotificationCenter&quot;
            tell first window
                set rawElements to UI elements
            end tell
        end tell
    end tell
end run

This gets me most of the way there, but it doesn't get me the body of the notification, just the label:
Here's the output:

{static text &quot;Macro Cancelled&quot; of window 1 of application process
&quot;NotificationCenter&quot; of application &quot;System Events&quot;, image 1 of window
1 of application process &quot;NotificationCenter&quot; of application &quot;System
Events&quot;, scroll area 1 of window 1 of application process
&quot;NotificationCenter&quot; of application &quot;System Events&quot;, image 2 of window
1 of application process &quot;NotificationCenter&quot; of application &quot;System
Events&quot;}
Using the following example AppleScript code to generate a notification:
display notification with title &quot;Macro Cancelled&quot; subtitle &quot;Cancel All Macros.&quot;

In the example notification, Cancel All Macros. is in:
static text of scroll area 1 of window 1 of process &quot;NotificationCenter&quot;

Using:
tell application &quot;System Events&quot; to ¬
    get value of ¬
        static text of ¬
        scroll area 1 of ¬
        window 1 of ¬
        process &quot;NotificationCenter&quot;

It returns:
{&quot;Cancel All Macros.&quot;, &quot;&quot;}

As you can see what's returned is a list and will need to be process as such.
Note however, in this example, Cancel All Macros. is actually static text 1 of scroll area 1 and static text 2 of scroll area 1, which is blank, is when display notification uses:

display notification [text] : the body text of the notification


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

With Error Handling:
tell application &quot;System Events&quot; to ¬
    if exists window 1 of ¬
        process &quot;Notification Center&quot; then ¬
        get value of ¬
            static text of ¬
            scroll area 1 of ¬
            window 1 of ¬
            process &quot;NotificationCenter&quot;