Skip to content

Commit 776800e

Browse files
authored
feat: update dismiss notifications script
Handle groups of stacked notifications
1 parent cb0cd05 commit 776800e

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

commands/system/dismiss-notifications.applescript

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,39 @@
1111

1212
# Documentation:
1313
# @raycast.description Close all notification alerts staying on screen, e.g., Calendar notifications.
14-
# @raycast.author dlvhdr
15-
# @raycast.authorURL github.com/dlvhdr
16-
17-
tell application "System Events" to tell application process "NotificationCenter"
18-
try
19-
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")
20-
if description of uiElement contains "Close" then
21-
perform uiElement
22-
end if
23-
if description of uiElement contains "Clear" then
24-
perform uiElement
25-
end if
14+
# @raycast.author benyn
15+
# @raycast.authorURL github.com/benyn
16+
17+
tell application "System Events" to tell process "NotificationCenter"
18+
-- Exit if there are no visible notifications.
19+
if not (window "Notification Center" exists) then return
20+
21+
-- `notificationContainer` refers to the UI element (of class `group`) holding notifications and can be either:
22+
-- - A single, individual notification, or
23+
-- - A collection of individual notifications and groups of stacked notifications.
24+
set notificationContainer to a reference to group 1 of scroll area 1 of group 1 of group 1 of window "Notification Center"
25+
26+
-- If it is a collection, close notifications and groups in reverse order to avoid index changes.
27+
set notificationGroups to a reference to groups of notificationContainer
28+
repeat with i from (number of notificationGroups) to 1 by -1
29+
set g to item i of notificationGroups
30+
repeat with a in (actions of g whose description is "Close" or description starts with "Clear")
31+
-- Ignore errors that happen if the last remaining item is an individual notification,
32+
-- and `notificationCenter` is no longer a `group` of `group`s.
33+
-- The final remaining notification will be closed in the second repeat statement below.
34+
ignoring application responses
35+
perform a
36+
end ignoring
2637
end repeat
27-
return ""
28-
end try
38+
end repeat
39+
40+
-- Close the `notificationContainer` itself. This handles:
41+
-- - A single, individual notification that was `notificationContainer` from the start, or
42+
-- - The last remaining individual notification after the loop above.
43+
repeat with a in (actions of notificationContainer whose description is "Close" or description starts with "Clear")
44+
perform a
45+
end repeat
2946
end tell
47+
48+
-- Prevent Raycast from displaying the successful result message.
49+
return

0 commit comments

Comments
 (0)