Hi this is Gary with MacMost.com.
On this episode let's look at how to create a keyboard shortcut to get rid of notifications
like these.
MacMost is brought to you thanks to a great group of supporters.
You can go to MacMost.com/patreon.
There you can read more about it, join us, and get exclusive content.
So the annoying thing about getting notifications is if you're in the middle of something and
your hands are on the keyboard there's no way to dismiss them without lifting your hands
up, going to your mouse or trackpad, going over and clicking on them.
But you can setup a script to do this.
If you can setup a script to do this then you can set a keyboard shortcut for it.
So let's look at building a script in JAVA script, otherwise known as JXA, using Automator.
So first we need a way to test this.
It's not easy to bring up Notifications.
You can setup reminders to remind you to do something in one minute.
But it turns out it's very easy to do in Automator.
There's a Display Notification action.
Search for that in the Actions in Automator and add a bunch of them.
So I have three here.
If I run this it presents me with three notifications.
Now notice it has the Close and Show buttons.
The way I got those is I went to System Preferences and I made sure, under Notifications for Automator,
that I had Alert selected.
By default it's Banners.
So select Alerts and now you have a great way to throw a bunch of notifications so we
can test our script.
Now let's get to building something that will close them.
So I'm going to create a new Automator document.
I'm going to make it a Quick Action which is also known as a Service.
This way we can assign it to a keyboard shortcut.
I'll show you how to do that at the end.
So we'll create a Quick Action and it's going to receive No Input.
It's not going to do anything special and it's going to work in any application.
So let's get rid of all these for now.
We'll bring them back to test.
Now we can first search for Java Script.
Everything we do here is going to be in Java Script because unfortunately there's not action
in here that is going to allow us to get this done.
We're going to have to do it either in Apple Script or Java Script.
Java Script, also known as JXA Java Script for Automation, is the more modern way of
doing it.
So I'm going to paste in three lines of code.
That's all it's going to take to do this.
The first two lines are pretty standard for almost any Java Script.
So you've got an app, you're assigning an app.
In this case System Events because that's where those notifications are.
Then you're doing app.includesStandardAdditions which means I can now do actions on this System
Events app.
The line that actually does it is this one.
So let's dissect it.
So we're taking the current app, System Events.
We're looking at the processes.
There's a list of processes and actually it's about a hundred processes long depending upon
what you've got going.
We're looking, byName, for one called Notification Center.
So it's going to find out of the hundred or so processes that particular one.
Then it's going to look at the array of windows and it's going to say do something to all
of them.
So not pick out a particular window but do it to all of them.
For all of them go to the first button and perform the action click.
That should do it.
So let's Save this and let's try it out.
I'm going to use Command and Tilde to switch windows here and open up our test notifications.
There they are.
Now Command and Tilde to go back to close all notifications.
I can't quite see the Run button so I'll move the window down.
I'll hit Run and there it goes.
It closed all those notification windows.
Great!
I've already saved it as a Quick Action so it's ready to go.
Since I put No Input in any application I should see it appear in the Services menu.
So I go to Automator Services and I should see Close All Notifications.
There it is.
Now I can select it anywhere.
So let's test that out.
I'll use this test one here again.
I'll Run.
Let me close Automator.
I'm just here in the Finder.
Go to Finder, Services, Close All Notifications.
Now Security is going to stop it from working because basically it's asking the Finder to
control system events.
So that's fine.
I'm going to hit Okay and then I'm going to go to System Preferences and then go to Security.
Look under Accessibility and authenticate here by hitting the padlock and allow the
Finder Accessibility options here.
So now when I do it in the Finder it should work.
There you go.
You see it happens.
So the downside of this is you have to do it for every app.
But you typically use the same apps.
So let's, for instance, go into Pages and say I'm writing in Pages and this is where
I get this problem here.
So I have a test document open in Pages.
Let's go back to Automator and throw up those test notifications.
Great.
So now I try to do it in Pages, Services, Close All Notifications and I'm going to get
the same thing.
I'll say Okay.
Then I'm going to have to go back to System Preferences.
I'm going to go to Security & Privacy and then add under Privacy, Accessibility, Pages.
So I should see it here in the list and it now asks for permission.
I check it and it's okay.
I could preemptively accept some other things as well.
So I can checkoff some other things in here.
Now when I go back to Pages and I go to Services, Close All Notifications it works.
Now all that's left is to set a keyboard shortcut.
So I'll go to System Preferences.
This time I'll go to Keyboard and I'll Add a Shortcut.
I'll do an app shortcut here.
I'll add one by hitting the plus button and say In All Applications, use the Menu Title
which is exactly what I named it so it's exactly what's in Services here.
Close All Notifications.
They have to match perfectly.
I'll do a keyboard shortcut of let's say F9.
Now it should work just by hitting F9.
So I'll go into Automator here.
I'll do test.
Hide Automator.
I'm typing here in Pages.
Those are bugging me.
I hit F9 and they go away.
So it's far from perfect because you have to give all those permissions.
But as soon as you assign permission to the main apps that you use then it becomes a pretty
quick keyboard shortcut to get rid of all notifications.
Now note that you can modify this to actually just get rid of the first notification.
If after windows I say just do the first item, item number zero in windows, then it's going
to get rid of the first one.
So now I can test.
Bring those three up.
I'll switch to Pages.
Now when I hit F9 it'll just get rid of the very first one.
So I hit F9 again and again and again.
So if I prefer to get rid of them one at a time I can do it by making that small alteration
to the code by just adding that zero there.