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?