Clear Notifications - Ventura Macro (v10.2)

Download: Clear Notifications - Ventura.kmmacros (2.2 KB)

4 Likes

This is a good site for generating test notifications. Test Web Push Notifications - CleverPush

I don't see a question, but I assume you're wondering why it doesn't work.

Try this instead:

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 _actions to actions of _group
			
			repeat with _action in _actions
				if description of _action is in {"Close", "Clear All"} then
					perform _action
					
				end if
			end repeat
			
		end repeat
		
	end try
end tell
1 Like

Thank you, your suggestion is a good way to repeat over a list. The original solution works but checks for "Clear All" first before moving on to check for "Close". With the list you suggested, for some reason a group of alerts that is expanded, this will close all but the last alert. ¯_(ツ)_/¯

1 Like

Yours does work better! The reason it didn't work for me was the end tel typo, which I missed at first. Once that was fixed, it works great and, as you mentioned, doesn't leave one notification at the end. :+1:t3:

Ahh... I missed that too. Must have hit the delete key right before I shared this. I updated the macro and the jpeg to show the missing "L".

For anyone who doesn't know what is going on (because its since been updated), the original macro was missing 1 "L" in the last "tell" of the script, so it was actually "tel" ... which will break.

Thanks for the good catch @noisneil

1 Like

Thanks for that macro. Clearing notifications was going to be my next project. Now I'll have to think of something else to do.

Emyr

My pleasure. I'm sure this macro can be tweak to perform better. I'd love to see your updates if you work on it.

hello @noisneil
great macro. Clearing notifications is always a big pain in the neck. Thank you very much. The problem is that when there are many notifications, I have to run the macro multiple times. This morning for example with 10+ notifications when I opened my mac, I had to run the script 3 times.
Should I just sequentially run the macro multiple times with the repeat action or would you have a solution which you would integrate into the script itself.
thanks very much

Here's a new version that should be more robust:

tell application "System Events"
    repeat
        -- Assume no notifications by default
        set hasNotifications to false
        
        try
            -- Get the list of notification groups
            set _groups to groups of UI element 1 of scroll area 1 of group 1 of window "Notification Center" of application process "NotificationCenter"
            
            -- If we find at least one notification group, set the flag to true
            if (count of _groups) > 0 then
                set hasNotifications to true
            end if
            
            -- Iterate over the notification groups and clear them
            repeat with _group in _groups
                try
                    -- Try to perform the "Clear All" action if available
                    perform (first action of _group where description is "Clear All")
                on error
                    -- If "Clear All" isn't available, try the "Close" action
                    perform (first action of _group where description is "Close")
                end try
            end repeat
            
            -- Add a brief delay to allow Notification Center to update
            delay 2
            
        on error errorMessage
            -- Handle any errors that might occur and log them
            log "Error encountered: " & errorMessage
        end try
        
        -- Exit the loop if no notifications were found
        if not hasNotifications then exit repeat
        
        -- Additional delay for rechecking notifications
        delay 2
        
    end repeat
end tell

1 Like

Nothing happens. Engine error message below.
thank you for taking the time to rewrite the script.

2023-11-13 15:38:37 Execute macro “Clear all Notifications” from trigger Editor
2023-11-13 15:38:37 Action 15233024 failed: Execute an AppleScript failed with script error: text-script:1192:1192: script error: Expected end of line, etc. but found end of script. (-2741)
2023-11-13 15:38:37 Execute an AppleScript failed with script error: text-script:1192:1192: script error: Expected end of line, etc. but found end of script. (-2741). Macro “Clear all Notifications” cancelled (while executing Execute AppleScript).

Did you incorporate it into a macro or try to run it alone? That may be the problem. This works nicely for me on Ventura:

Clear All Notifications.kmmacros (48 KB)

Macro screenshot

1 Like

Now it's fine. Thanks very much !!

1 Like