Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notification subtitle/body using AppleScript?

I'm trying to read macOS notifications title and subtitle using AppleScript. I managed to get the title using the example posted here (number 5), but I also need to get the subtitle.

Here is the code that returns the title:

on run
    tell application "System Events"
        tell process "Notification Center"
            set theseWindows to every window
            set theseTitles to {}
            repeat with thisWindow in theseWindows
                try
                    set thisTitle to the value of static text 1 of thisWindow
                    set the end of theseTitles to thisTitle
                end try
            end repeat
            return theseTitles
        end tell
    end tell
end run

Does anyone know how I can get the notification subtitle?

like image 365
Patricia Anastasia Avatar asked Sep 17 '25 13:09

Patricia Anastasia


1 Answers

Running the following example AppleScript code in Script Editor:

display notification "Body Text Line" with title "Title Text Line" subtitle "Subtitle Text Line"

Produces this notification:

enter image description here

Then running the following example AppleScript code in Script Editor:

tell application "System Events"
    tell application process "NotificationCenter"
        get value of static text 1 of window 1
        get value of static text 1 of scroll area 1 of window 1
        get value of static text 2 of scroll area 1 of window 1
    end tell
end tell

Shows the following output in the Replies pane of Script Editor:

tell application "System Events"
    get value of static text 1 of window 1 of application process "NotificationCenter"
        --> "Title Text Line"
    get value of static text 1 of scroll area 1 of window 1 of application process "NotificationCenter"
        --> "Subtitle Text Line"
    get value of static text 2 of scroll area 1 of window 1 of application process "NotificationCenter"
        --> "Body Text Line"
end tell


As you can see, it's the get value of static text 1 of scroll area 1 of window 1 which returns the subtitle.

like image 190
user3439894 Avatar answered Sep 20 '25 04:09

user3439894