r/applescript • • Nov 20 '20

Clear all notifications with single script on Big Sur?

During my workflow I end up with a ton of notifications and in Big Sur it's a pain to close each one by either swiping away or clicking the tiny X.

Does anyone know how to batch clear notifications?

I saw this script, but it doesn't work in Big Sur, I get an error where it can't "get button 'Close' of window 1 of process 'NotificationCenter'":

tell application "System Events"

tell process "NotificationCenter"
    set numwins to (count windows)
    repeat with i from numwins to 1 by -1
        click button "Close" of window i
    end repeat
end tell

end tell

12 Upvotes

24 comments sorted by

View all comments

1

u/crazycreach Jan 07 '21

This is the JS AppleScript I use to clear all notifications. Note that you can also change the appName variable if you only want to close notifications for a specific app.

https://gist.github.com/lancethomps/a5ac103f334b171f70ce2ff983220b4f

2

u/l0st1 Jan 14 '21

Unfortunately, it does not work for me on Big Sur in German.

I want to get rid of my notifications of improperly ejected disks:

https://www.reddit.com/r/osx/comments/kwcgln/big_sur_how_to_clear_all_notifications/

2

u/crazycreach Jan 14 '21

Yea, that doesn’t surprise me since it looks for the buttons labeled “Clear All” or “Close”. You can try to change that to the German equivalent

1

u/l0st1 Jan 14 '21

Thanks! That's a good suggestion. I'll try it tomorrow.

1

u/Ptujec Jan 30 '21 edited Jan 30 '21

Thanks for the script! With the language fix it runs perfectly. At least most of the time. There is a very rare case mentioned below when it doesn't.

If you adjust these two lines of code like shown it will work in German:

const CLEAR_ALL_ACTION = "Alle entfernen";

const CLOSE_ACTION = "Schließen";

Without the change it will result in an endless loop. This also happens if you click on a group of notifications so seem them all. Running the script like that results in an endless loop as well. Maybe some code to end the routine after a while would be helpful. Unfortunately I don't know how to do that myself.

1

u/crazycreach Jan 30 '21

Yea I actually ran into that same problem myself the other day and fixed it. I still need to update the gist though

1

u/Ptujec Feb 06 '21

Cool. I'm following your gist so in theory I should get an email when you do. Thx!

1

u/crazycreach Feb 09 '21

just pushed an update that should fix this!

1

u/tsmith944 Jan 07 '21

Does this work on Big Sur? Can you advise how you run this? I tried in Script Editor, but I'm not sure how to run a .js apparently. This sounds promising, thank you!

1

u/crazycreach Jan 08 '21

Yea, it is meant for Big Sur. Open Automator, create a new document, choose Application for your type, add the "Run JavaScript" action, and paste the code there. You can then save the Application and any time you open it, it will run the code to close your notifications. I use BetterTouchTool to map it to a keyboard shortcut.

1

u/tsmith944 Jan 08 '21

That's perfect! Thank you!

1

u/leonseled Mar 05 '21

Hi u/crazycreach this is great. Thanks for this! One thing I noticed though is it just closes the notification. You think there's a way you can make it take action on the notification? i.e. clicking "Mark as read" in a Messages notif.

If it helps this is the script I was previously using pre-Big Sur to accomplish it:

var app = Application("System Events")

notificationCenter = app.processes.byName('NotificationCenter')

function closeWindow(window){
    window.buttons.whose({
        _or: [
            {name: "Close"},
            {name: "Mark as Read"}
        ]
    })().forEach(function(button){button.click()})
}

notificationCenter.windows().forEach(closeWindow)

1

u/crazycreach Mar 06 '21

So you want to mark all of the messages notifications as read and then just close the others? I haven’t tried to perform any actions outside of closing the notification, but I bet it’s possible. Let me know and I’ll try to take a look at some point over the next couple weeks when I get some time.

1

u/leonseled Mar 06 '21

Yep correct. in the previous script I was using, this behavior was controlled by the _or property which is an array of objects with the name property.

For some it would be “mark as read”, for others “close” etc. I was able to specify which button to target.