Skip to main content
4 of 6
deleted 1 character in body
wch1zpink
  • 7.7k
  • 17
  • 35

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 : {}

repeat
    getNotificationTitles()
    delay 1
    if theseTitles contains lookForThisText then
        ---------------------------------------------------------------
        delay 7 -- Gives Banner Time To Self Close
        --tell current application to beep 5 -- Just For Testing
        run script alias scriptToRun
        return -- Remove This If You Want This To Keep Repeating
        ---------------------------------------------------------------
    end if
end repeat

on getNotificationTitles()
    try
        -- This Gets The Titles Of The Currently Displaying Notification Alerts And Banners
        tell application "System Events"
            tell process "Notification Center"
                set theseWindows to every window whose subrole is "AXNotificationCenterAlert" or subrole is "AXNotificationCenterBanner"
                set theseTitles to {}
                repeat with thisWindow in theseWindows
                    try
                        set titleText to the value of static text 1 of thisWindow
                        set the end of theseTitles to titleText
                    end try
                    try
                        set subTitleText to the value of static text 1 of scroll area 1 of thisWindow
                        set the end of theseTitles to subTitleText
                    end try
                    try
                        set notificationText to the value of static text 2 of scroll area 1 of thisWindow
                        set the end of theseTitles to notificationText
                    end try
                end repeat
            end tell
        end tell
    end try
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

wch1zpink
  • 7.7k
  • 17
  • 35