Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ZevEisenberg/59fb699c2880ae16018171f3e5e8ac28 to your computer and use it in GitHub Desktop.
Save ZevEisenberg/59fb699c2880ae16018171f3e5e8ac28 to your computer and use it in GitHub Desktop.
Dismiss Disk Not Ejected Properly Dialogs
tell application "System Events" to tell process "Notification Center"
repeat with notificationWindow in windows
tell notificationWindow
set entireContents to entire contents
repeat with content in reverse of entireContents -- iterate backwards so we close bottom notifications first, if that matters
if class of content is group then
set groupStaticTexts to static texts of content
repeat with staticText in groupStaticTexts
set foundText to false
if value of staticText is equal to "Disk Not Ejected Properly" then
set foundText to true
exit repeat
end if -- text is the text we want to find
end repeat -- staticTexts in group
if foundText then
-- We'd like to look for buttons in the group called "Close", but recent macOS versions hide the Close button until you hover over the notification, and there's apparently no way to hover in AppleScript. Instead, we inspect the actions of the group, and look for the one called "Close".
-- actions trick via https://github.com/Ptujec/LaunchBar/blob/master/Notifications/Dismiss%20all%20notifications.lbaction/Contents/Scripts/default.applescript via https://www.reddit.com/r/applescript/comments/ycilyr/comment/iu5m3q5/?utm_source=reddit&utm_medium=web2x&context=3
repeat with groupAction in actions of content
if description of groupAction is equal to "Close" then
perform groupAction
exit repeat
end if
end repeat
end if
end if -- if class of content is group
end repeat -- entire contents of window
end tell -- notificationWindow
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment