Last active
February 11, 2023 09:54
-
-
Save bezhermoso/5f45ace434a7b03c963a to your computer and use it in GitHub Desktop.
Handly AppleScripts to manage OS X notifications. (Use in Automator, bind to keyboard shortcuts via BetterTouchTool, etc.) Inspired by http://apple.stackexchange.com/a/155736
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Close all notifications | |
my closeNotifications() | |
on closeNotifications() | |
tell application "System Events" to tell process "Notification Center" | |
set theWindows to every window | |
repeat with i from 1 to number of items in theWindows | |
set this_item to item i of theWindows | |
try | |
click button 1 of this_item | |
on error | |
my closeNotifications() | |
end try | |
end repeat | |
end tell | |
end closeNotifications | |
# Dismiss only the top-most notification | |
my closeNotification() | |
on closeNotification() | |
tell application "System Events" to tell process "Notification Center" | |
click button 1 of last item of windows | |
end tell | |
end closeNotification | |
# Click top-most notification | |
my clickNotification() | |
on clickNotification() | |
tell application "System Events" to tell process "Notification Center" | |
click last item of windows | |
end tell | |
end clickNotification | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe this solves the looping issue:
Without the
delay
there seems to be a race condition between the last close completing and the next close being triggered, which halts operation. Other related code I've found online usedelay
as well (1, 2). Though0.5
is pretty conservative; you could probably get away with0.2
on a new or fast machine.cc @majorgear