2

On macOS 12.x I was using a script to automatically close massive number of Disk Not Ejected Properly notifications that show up the next morning if I inadvertently leave my computer running with one or more external drives connected.

When the computer goes to sleep, I suspect it must be going to sleep and coming out of sleep repeatedly, because I can end up with 50+ notifications for the disk(s).

I got the script from here.

I see that it has been updated (in March 2023) to work on Ventura, yet it's not working on 13.2.1.

The script is:

tell application "System Events" to tell process "Notification Center"
    repeat with notificationWindow in windows
        tell notificationWindow
            set entireContents to entire contents
            repeat with content in reverse of entireContents -- iterate backwards so we close bottom notifications first, if that matters
                if class of content is group then
                    set groupStaticTexts to static texts of content
                    repeat with staticText in groupStaticTexts
                        set foundText to false
                        if value of staticText is equal to "Disk Not Ejected Properly" then
                            set foundText to true
                            exit repeat
                        end if -- text is the text we want to find
                    end repeat -- staticTexts in group
                    
                    if foundText then
                        -- We'd like to look for buttons in the group called "Close", but recent macOS versions hide the Close button until you hover over the notification, and there's apparently no way to hover in AppleScript. Instead, we inspect the actions of the group, and look for the one called "Close".
                        -- actions trick via https://github.com/Ptujec/LaunchBar/blob/master/Notifications/Dismiss%20all%20notifications.lbaction/Contents/Scripts/default.applescript via https://www.reddit.com/r/applescript/comments/ycilyr/comment/iu5m3q5/?utm_source=reddit&utm_medium=web2x&context=3
                        
                        repeat with groupAction in actions of content
                            if description of groupAction is equal to "Close" then
                                perform groupAction
                                exit repeat
                            end if
                        end repeat
                    end if
                end if -- if class of content is group
            end repeat -- entire contents of window
        end tell -- notificationWindow
    end repeat
end tell

It comes back with the error:

System Events got an error: Can’t get static text "Eject “AUDIO-VIDEO” before disconnecting or turning it off." of group 35 of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter".

To me this suggests it is getting the notification message text, not just the title. Although it has managed to identify the notification (presumably via the title "Disk Not Ejected Properly") and yet for some reason it "get the static text" (I'm not sure what the means though).

Can you suggest how to fix the script?

1
  • Have you ever figure this out? Commented Oct 8, 2023 at 16:47

1 Answer 1

0

Here is some code that works for me on MacOS Sonoma (14.2.1):

tell application "System Events" to tell application process "NotificationCenter"
    try
        log (actions of UI elements of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter" of application "System Events")
        perform (actions of UI elements of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter" of application "System Events" whose name starts with "Name:Close")
    end try
end tell

Instead of whose name starts with "Name:Close"you may use whose name description is "Name:Fermer" in French, or your own language. I added a log line so that you can check which action you wish to perform.

Anyway, this has been a PITA for so long, hope this helps.

Check this for the Monterey version: https://apple.stackexchange.com/a/467607/303345

You must log in to answer this question.

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