3

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?

1 Answers1

4

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.

user3439894
  • 7,266
  • 3
  • 17
  • 28
  • I get this error when running your applescript in ventura error "System Events got an error: Can’t get static text 1 of window 1 of application process \"NotificationCenter\". Invalid index." number -1719 from static text 1 of window 1 of application process "NotificationCenter" – majorgear Nov 14 '22 at 15:20
  • 1
    this no longer works on recent MacOS versions – Rosenpin Nov 25 '22 at 08:30