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.
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
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.
I don't run it anymore, but I use to keep a USB drive attached to a USB dock and disconnect it quickly when I swapped my personal Macbook with my employer's Macbook from the dock at the beginning of the workday.
That caused the infamous "device not ejected properly" notification every time. I had KM run the script when it detected the USB drive was disconnected to clear the notification automatically.
I don't use an external USB drive anymore, so I no longer need the script.
I started using an external USB drive again. Since I had so many problems clearing the notifications, I have taken a different approach to make ejecting USB drives easier.
I created a macro activated with hyper key "e" that runs an AppleScript
set volumes_ to {"VolumeName"} -- volume names
tell application "Finder"
repeat with vol_ in volumes_
eject disk vol_
end repeat
end tell
which seems to do the trick. I initially tried an application called "Jettison" to do this with a hotkey, but it failed with the "drive in use message." So that was out.
I'll stick with this macro and tune it if I have any issues.
There is a specific USB drive I use for time machine backups that I want to write a macro the detects the drive is connected, does a TM backup , and ejects the drive when the backup is done. That last step woud be the hard part.