macos – Run AppleScript when notification is receieved

0
17
macos – Run AppleScript when notification is receieved


What macOS model are you on? I found out one thing so I can auto-dismiss these annoying persistent updates obtainable notifications (which occur no matter auto-update settings), however apparently the Applescript capabilities for notifications change vastly between OS variations. My answer is just not actually “act when receiving a notification”, however relatively it continuously checks seen notifications for sure strings. That is just about the perfect you are able to do with simply Applescript. It ought to work wonderful on a minimum of Monterey and presumably something newer.

The highest half is a few config, then on the very backside I’ve added a remark to point the place you’d add your dealing with. It’s best to most likely simply learn by way of the complete script anyway. =] As a bonus I’ve additionally included the auto-dismiss performance, which ought to be language unbiased. Additionally I am undecided if subtitles nonetheless exist, as a result of none confirmed up in my check instances. However I added a remark the place you could possibly attempt to retrieve them.

world notificationStrings
set notificationStrings to {"Bricks + Agent"}

-- These *should* be the English names, since they are going to be translated to no matter's at present on the notification
world closeStrings
set closeStrings to {"Shut", "Clear All"}

world accessCheckTitle
set accessCheckTitle to "Notifications Monitor"

-- An error message might be immediately appended to this
world accessCheckSubtitle
set accessCheckSubtitle to "This app wants each Accessibility and Automation entry:nn"

world buttonOk
set buttonOk to "Shut"
world buttonSysPrefs
set buttonSysPrefs to "Open System Preferences"

-- Quantity of ticks earlier than we reload the strings (at return 3 that is about 300 seconds, or 5 min)
world reloadStringsInterval
set reloadStringsInterval to 100

-- END OF CONFIG

world reloadStringsTicks
set reloadStringsTicks to -1
world closeStringsl10n
set closeStringsl10n to {}

on idle
    checkNotifications()
    return 3 -- Delay for subsequent run (seconds)
finish idle

on stop
    proceed stop -- Permits the script to stop
finish stop

on checkNotifications()
    strive
        -- All of those throw errors once they've explicitly been denied entry, in any other case they will immediate (a minimum of on the very first time)
        inform utility id "com.apple.SystemEvents" to set ncenterProcess to the primary course of whose bundle identifier = "com.apple.notificationcenterui" -- For Automation entry on System Occasions
        set ncenter to path to utility id "com.apple.notificationcenterui" -- For Automation entry on Notification Centre
        inform ncenterProcess to exists window 1 -- For Accessibility entry
    on error errmsg
        -- A strive is critical to deal with the cancel button with out "crashing" (thanks Apple)
        set openSysPrefs to false
        strive
            set res to (show dialog (accessCheckSubtitle & errmsg) with title accessCheckTitle buttons {buttonOk, buttonSysPrefs} default button buttonSysPrefs cancel button buttonOk with icon cease)
            if button returned of res is the same as buttonSysPrefs then
                set openSysPrefs to true
            finish if
        finish strive

        if openSysPrefs then
            open location "x-apple.systempreferences:com.apple.desire.safety?Privacy_Accessibility"
        finish if

        -- All the time must stop right here in order that we are able to choose up on any newly granted permissions subsequent time we run
        delay 30
        stop
    finish strive

    set reloadStringsTicks to reloadStringsTicks + 1
    if reloadStringsTicks is the same as 0 or reloadStringsTicks is larger than or equal to reloadStringsInterval then
        set l10nTmp to {}
        strive
            repeat with closeString in closeStrings
                set the top of l10nTmp to localized string closeString in bundle ncenter
            finish repeat
        on error
            -- I feel this may occur if this script runs too early after logging in, so let's be sure that we'll instantly strive once more on the following run
            set reloadStringsTicks to -1
            return
        finish strive
        set closeStringsl10n to l10nTmp
        set reloadStringsTicks to 0
    finish if

    inform utility id "com.apple.SystemEvents"
        inform ncenterProcess
            -- The primary window might not at all times (instantly/totally) exist apparently
            strive
                set allNotifications to teams of UI component 1 of scroll space 1 of window 1
            on error
                return
            finish strive

            repeat with checkNotification in allNotifications
                -- It might have already (been) closed within the meantime
                strive
                    set nTitle to the worth of static textual content 1 of checkNotification
                    --set nContents to the worth of static textual content 2 of checkNotification
                    -- There *might* be a static textual content 3 for subtitles, nevertheless it did not exist in my check instances

                    if nTitle is in notificationStrings then
                        -- That is the place you'd add your dialog and different dealing with, for instance (to dismiss the notification afterwards):
                        repeat with checkAction in actions of checkNotification
                            if description of checkAction is in closeStringsl10n then
                                strive
                                    carry out checkAction
                                finish strive
                                delay 2 -- Give it a while to shut
                            finish if
                        finish repeat
                    finish if
                finish strive
            finish repeat
        finish inform
    finish inform
finish checkNotifications

Reserve it as an Software, whereas disabling Present startup display and enabling Keep open after run handler. Including it to your startup objects ought to work simply wonderful.

LEAVE A REPLY

Please enter your comment!
Please enter your name here