Skip to content

feat: update dismiss notifications script #990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions commands/system/dismiss-notifications.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,39 @@

# Documentation:
# @raycast.description Close all notification alerts staying on screen, e.g., Calendar notifications.
# @raycast.author dlvhdr
# @raycast.authorURL github.com/dlvhdr

tell application "System Events" to tell application process "NotificationCenter"
try
repeat with uiElement in (actions of UI elements of scroll area 1 of group 1 of group 1 of window "Notification Center" of application process "NotificationCenter" of application "System Events")
if description of uiElement contains "Close" then
perform uiElement
end if
if description of uiElement contains "Clear" then
perform uiElement
end if
# @raycast.author benyn
# @raycast.authorURL github.com/benyn

tell application "System Events" to tell process "NotificationCenter"
-- Exit if there are no visible notifications.
if not (window "Notification Center" exists) then return

-- `notificationContainer` refers to the UI element (of class `group`) holding notifications and can be either:
-- - A single, individual notification, or
-- - A collection of individual notifications and groups of stacked notifications.
set notificationContainer to a reference to group 1 of scroll area 1 of group 1 of group 1 of window "Notification Center"

-- If it is a collection, close notifications and groups in reverse order to avoid index changes.
set notificationGroups to a reference to groups of notificationContainer
repeat with i from (number of notificationGroups) to 1 by -1
set g to item i of notificationGroups
repeat with a in (actions of g whose description is "Close" or description starts with "Clear")
-- Ignore errors that happen if the last remaining item is an individual notification,
-- and `notificationCenter` is no longer a `group` of `group`s.
-- The final remaining notification will be closed in the second repeat statement below.
ignoring application responses
perform a
end ignoring
end repeat
return ""
end try
end repeat

-- Close the `notificationContainer` itself. This handles:
-- - A single, individual notification that was `notificationContainer` from the start, or
-- - The last remaining individual notification after the loop above.
repeat with a in (actions of notificationContainer whose description is "Close" or description starts with "Clear")
perform a
end repeat
end tell

-- Prevent Raycast from displaying the successful result message.
return