Skip to content

Instantly share code, notes, and snippets.

@bkyle
Created January 15, 2022 15:50
Show Gist options
  • Save bkyle/0b49d6ebd0579abcad443228706c4701 to your computer and use it in GitHub Desktop.
Save bkyle/0b49d6ebd0579abcad443228706c4701 to your computer and use it in GitHub Desktop.
Applescript to dismiss all "drive not ejected properly" notifications. This can be run as a script at the terminal, setup as a cronjob, run through Alfred, etc.
#!/usr/bin/osascript
tell application "System Events"
tell process "NotificationCenter"
set vWindowCount to (count windows)
if vWindowCount is 0 then
return
end if
set vWindow to window 1
set vNotificationContainer to (UI element 1 of scroll area 1 of vWindow)
set vNotifications to (groups of vNotificationContainer)
set vNotificationCount to (count vNotifications)
repeat with vNotificationIndex from vNotificationCount to 1 by -1
set vNotification to (item vNotificationIndex of vNotifications)
set vNotificationTitle to (value of static text 1 of vNotification)
if vNotificationTitle is "Disk Not Ejected Properly" then
set vDismissAction to (action 3 of vNotification)
perform vDismissAction
-- Executing the dismiss action too quickly causes some of them to be ignored.
-- Adding a delay to allow the UI to catch up. Anecdotally, 0.1 doesn't appear
-- to work but 0.2 does. 0.5 is used to give a time buffer.
delay 0.5
end if
end repeat
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment