3

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

0

2 Answers 2

4

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")
4
  • did OP ever get this working? using the text inc comments of this question, I cannot get it to work. it would be great to post the working solution in the answer.
    – majorgear
    Commented Nov 14, 2022 at 17:36
  • @majorgear The comments don't add anything that isn't given in the answer itself. What do you mean by "cannot get it to work", what error do you get? What version of macOS are you using?
    – grg
    Commented Nov 14, 2022 at 18:57
  • Works on Montery 12.6. With Ventura 13.0.1 , gets message - error "System Events got an error: Can’t get scroll area 1 of window 1 of process \"Notification Center\". Invalid index." number -1719 from scroll area 1 of window 1 of process "Notification Center" "
    – majorgear
    Commented Nov 15, 2022 at 2:18
  • 2
    Got it working on Ventura tell application "System Events" try set _groups to groups of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter" repeat with _group in _groups set temp to value of static text 1 of _group if temp contains "Disk Not Ejected Properly" then perform (first action of _group where description is "Close") end if end repeat end try end tell
    – majorgear
    Commented Nov 15, 2022 at 3:13
6

From the comment by @majorgear, I was able to make it work with a bunch of "Background Items Added" notifications that appeared after upgrading to Mac OS Ventura:

tell application "System Events"
   try
     set _groups to groups of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter"
    repeat with _group in _groups
      set temp to value of static text 1 of _group
      if temp contains "Background Items Added" then
        perform (first action of _group where description is "Close")
       end if
    end repeat
  end try 
end tell
1
  • This works, unlike that useless Google Gemini. Commented May 9 at 22:26

You must log in to answer this question.

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