Close certain notifications using AppleScript
This is the script:
tell application "System Events"
tell process "NotificationCenter"
set numwins to (count windows)
repeat with i from numwins to 1 by -1
tell window i
set temp to value of static text 1
end tell
if temp contains "Disk Not Ejected Properly" then
click button "Close" of window i
end if
end repeat
end tell
end tell
And this is the error I'm getting:
error "System Events got an error: Can’t get static text 1 of window 1 of process \"NotificationCenter\". Invalid index." number -1719 from static text 1 of window 1 of process "NotificationCenter"
I'm running MacOS Big Sur 11.4
Solution 1:
Can’t get static text 1 of window 1
The Notification Center window does not contain a static text as an immediate child. You need to navigate the hierarchy down to the texts within a notification.
get static text 1 of group 1 of UI element 1 of scroll area 1 of window i
Can’t get button "Close"
You also can't click a button that doesn't exist. Perform the close action on the notifications instead, which you can get using actions … where description is "Close"
.
perform (first action of group 1 of UI element 1 of scroll area 1 of window i where description is "Close")