Automatically Clear "Disk Not Ejected Properly" notification ( and only that one?)

I keep a usb drive with unimportant data on it ( ie I don't care if I lose it) plugged into my Caldigit dock so that I can access it whenever I'm docked. Whe I unplug my macbook from the docker, I get the highly useful notification "Disk Not Ejected Properly" message , screenshot attached.

Screenshot 2022-11-14 at 7.38.31 AM

Is there a way to automatically dismiss that, and only that, notification when it appears?

I've found several macros to close all notifications, but none of them target a specific notification.

Example appscript from one of the macros I found:

activate application "NotificationCenter"
tell application "System Events"
	tell process "Notification Center"
		repeat
			
			try
				set theWindow to group 1 of UI element 1 of scroll area 1 of window "Notification Center"
			on error
				exit repeat
			end try
			
			try
				set theActions to actions of theWindow
				
				# Try to close the whole group first. If that fails, close individual windows.
				repeat with theAction in theActions
					if description of theAction is "Clear All" then
						set closed to true
						tell theWindow
							perform theAction
						end tell
						exit repeat
					end if
				end repeat
				
				repeat with theAction in theActions
					if description of theAction is "Close" then
						set closed to true
						tell theWindow
							perform theAction
						end tell
						exit repeat
					end if
				end repeat
				
			end try
		end repeat
	end tell
end tell

I think the trick would be to match the notification title with with the title of the message I want to dismiss, but I haven't yet found documentation with the correct field name.

If anyone has guidance to help me automate dismissing this specific notification, I'll appreciate it!

I got it working on Ventura 13.0.1 by taking another script for closing all notifications and modifying it to only act on this specific notification.

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

I also made a Monterey version for my macbook that hasn't been upgraded yet.

tell application "System Events"
	tell process "Notification Center"
		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 of group 1 of UI element 1 of scroll area 1
			end tell
			if temp contains "Disk Not Ejected Properly" then
				perform (first action of group 1 of UI element 1 of scroll area 1 of window i where description is "Close")
			end if
		end repeat
	end tell
end tell

Enjoy!

6 Likes

Hey – thanks for sharing.

I also use Monterey but the second one doesn't works well.

None error messages comes out.

Is there any condition to use it?

Thanks

None error messages comes out.

I don't know what you mean here. Do you mean it doesn't clear the "Disk Not Ejected Properly" notification? The only issue I've seen so far is when using focus modes to hide notifications. It can't clear notifications that are not on the screen. Once the focus mode changes, they appear and can be cleared.

Oh I got it. I misunderstood it since I followed your link from the topic
thanks anyway

I seems that I spoke too soon. After a week or 2 of working, it stopped working on Monterrey this morning. It still works on Ventura.

I'll try rebooting and see if that fixed the Monterrey not working.

Update: Rebooting fixed the issue.

This is a life saver, thanks for sharing.

Thank you very much. I couldn't get the former version to work on Ventura.

Hello,

I'm quite new to KM. I try to get rid of some specific notifications and I think I can use this AppleScript. But how do I trigger it?

I'd like to auto-close the notification right after it appears.

Is this possible?

Regards and thanks in advance.
Kosz

Hey @Tokosh,

Welcome to the forum!  :sunglasses:

This problem cannot be solved with Keyboard Maestro alone.

See this:

Monitor the Notification Center with AppleScriptObjC (ASObjC)

-Chris

I might have missed something, but what do you do with this script? Running it manually kind of defeats the purpose, right?