0

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

1 Answer 1

1

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

5
  • Thank you, I'm looking at it but I always get an error (-> error number -1728 from process "Notification Center") but I'm nearly sure it's something I'm doing wrong.
    – Kevin
    Commented Oct 3, 2020 at 15:05
  • Why all the try blocks ? They're unnecessary, it's lazy, and it's setting a bad example. And NO! to an infinite repeat loop. Never, ever. Thing is, I know you know all this, so that makes this script even more peculiar. I hope you'll consider revising the script, particularly the repeat loop.
    – CJK
    Commented Oct 8, 2020 at 9:08
  • 1
    @Kevin No, you're not doing it wrong. The script is wrong. In the en_GB locale, at least, the name of the process should be "NoficationCenter" (which is one word). However, this will vary by locale, and your best bet is to reference the process like so: tell app id "com.apple.SystemEvents" to tell the first process whose bundle identifier = "com.apple.notificationcenterui" to.... The bundle identifier will always be the same across the world, so that should get you the correct targeting reference. I wouldn't use this script if I were you, however (at least until it's amended).
    – CJK
    Commented Oct 8, 2020 at 9:26
  • OK guys my revised answer should be able to withstand scrutinizing now LOL
    – wch1zpink
    Commented Oct 8, 2020 at 17:07
  • Thank a lot, that work fine, I'm going off-topic but I changed the scriptToRun path to "property scriptToRun : "/Users/myusername/cloud/AppleScript/scripts/demo.scpt"" and I keep getting an error "scriptToRun is not defined" which I don't understand, when I run the same demo.scpt with the same path in a new code, it's work just fine
    – Kevin
    Commented Oct 9, 2020 at 14:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .