16 C
United States of America
Saturday, November 23, 2024

macos – Run AppleScript when notification is receieved


What macOS model are you on? I discovered one thing so I can auto-dismiss these annoying persistent updates out there notifications (which occur no matter auto-update settings), however apparently the Applescript capabilities for notifications change vastly between OS variations. My resolution is just not actually “act when receiving a notification”, however fairly it consistently checks seen notifications for sure strings. That is just about one of the best you are able to do with simply Applescript. It ought to work advantageous on no less than 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’ll add your dealing with. You must in all probability simply learn via your complete script anyway. =] As a bonus I’ve additionally included the auto-dismiss performance, which must be language unbiased. Additionally I am undecided if subtitles nonetheless exist, as a result of none confirmed up in my take a look at instances. However I added a remark the place you may attempt to retrieve them.

international 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
international closeStrings
set closeStrings to {"Shut", "Clear All"}

international accessCheckTitle
set accessCheckTitle to "Notifications Monitor"

-- An error message will likely be straight appended to this
international accessCheckSubtitle
set accessCheckSubtitle to "This app wants each Accessibility and Automation entry:nn"

international buttonOk
set buttonOk to "Shut"
international 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)
international reloadStringsInterval
set reloadStringsInterval to 100

-- END OF CONFIG

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

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

on give up
    proceed give up -- Permits the script to give up
finish give up

on checkNotifications()
    attempt
        -- All of those throw errors once they've explicitly been denied entry, in any other case they will immediate (no less than on the very first time)
        inform software 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 software id "com.apple.notificationcenterui" -- For Automation entry on Notification Centre
        inform ncenterProcess to exists window 1 -- For Accessibility entry
    on error errmsg
        -- A attempt is critical to deal with the cancel button with out "crashing" (thanks Apple)
        set openSysPrefs to false
        attempt
            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 attempt

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

        -- All the time have to give up right here in order that we will decide up on any newly granted permissions subsequent time we run
        delay 30
        give up
    finish attempt

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

    inform software id "com.apple.SystemEvents"
        inform ncenterProcess
            -- The primary window might not all the time (instantly/totally) exist apparently
            attempt
                set allNotifications to teams of UI ingredient 1 of scroll space 1 of window 1
            on error
                return
            finish attempt

            repeat with checkNotification in allNotifications
                -- It might have already (been) closed within the meantime
                attempt
                    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, however it did not exist in my take a look at instances

                    if nTitle is in notificationStrings then
                        -- That is the place you'll 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
                                attempt
                                    carry out checkAction
                                finish attempt
                                delay 2 -- Give it a while to shut
                            finish if
                        finish repeat
                    finish if
                finish attempt
            finish repeat
        finish inform
    finish inform
finish checkNotifications

Put it aside as an Utility, whereas disabling Present startup display screen and enabling Keep open after run handler. Including it to your startup objects ought to work simply advantageous.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles