AppleScript read and trigger other script base on received notification

applescriptnotifications

Is they a way to read macOS Catalina notification from AppleScript?

it's seems that they are not longer stored with ~/Library/Application Support/ , I'm not even sure of the process name, "Notification Center" seems to failed

I would like to run an AppleScript which can detect and read new notification then trigger other script based on the notification

Best Answer

This following AppleScript code should work if you save it as a “stay open” application. Just set the property values for scriptToRun and lookForThisText and it should be good to go.

Don’t forget to grant permissions in System Preferences for your new stay open application to control your computer

property scriptToRun : (path to desktop as text) & "your.scpt" -- your path to .scpt file
property lookForThisText : "Search For This Text In Notification Windows" -- your search term
property theseTitles : {}

on idle
    getNotificationTitles()
    delay 0.1
    if theseTitles contains lookForThisText then
        ---------------------------------------------------------------
        delay 6 -- Gives Banner Time To Self Close
        --tell current application to beep 5 -- Just For Testing
        run script alias scriptToRun
        ---------------------------------------------------------------
    end if
    return 1 -- in seconds
end idle

on quit
    --  Executed when the script quits
    continue quit -- allows the script to quit
end quit

on getNotificationTitles()
    -- This Gets The Titles Of The Currently Displaying Notification Alerts And Banners
    tell application id "com.apple.SystemEvents"
        tell (the first process whose bundle identifier = "com.apple.notificationcenterui")
            set theseWindows to every window whose subrole is ¬
                "AXNotificationCenterAlert" or subrole is "AXNotificationCenterBanner"
            set theseTitles to {}
            repeat with thisWindow in theseWindows
                set titleText to the value of static text 1 of thisWindow
                set the end of theseTitles to titleText
                set subTitleText to the value of static text 1 of scroll area 1 of thisWindow
                set the end of theseTitles to subTitleText
                set notificationText to the value of static text 2 of scroll area 1 of thisWindow
                set the end of theseTitles to notificationText
            end repeat
        end tell
    end tell
end getNotificationTitles

Here is a quick animation showing the process in action. I created an AppleScript app named Test Notification.app whose only purpose is to display a notification… display notification "Blah" with title "BLAH BLAH" subtitle "DUH” Then, using the code I used as the answer to this post, I created a “Stay Open Application”, whose only purpose is to monitor all incoming notification windows for the text in which I define in the variable lookForThisText. Once the text is identified, it triggers another script "Merge Every Finder Window.scpt" which as the name says… merges every Finder window.

enter image description here

enter image description here