applescript: toggle Notification Centre (Yosemite)
Ever since Apple introduced Notification Centre, scripting it has been anything but easy.
I’ve seen brutal shell scripts and a variety of GUI scripts that variously work in Mountain Lion and Mavericks to turn ‘Do Not Disturb’ on and off. With Yosemite, Apple made yet another change to the ui process that controls Notification Centre, which means scripts like this one will choke.
If you’re wondering how to simply toggle whether Notification Centre is enabled or not with AppleScript on Yosemite, here’s the trick (whether this will continue to work in 10.11 is anyone’s guess** — see the comments below for the El Capitan version).
Enjoy it while it lasts! 🙂
tell application "System Events"
tell application process "SystemUIServer"
try
if exists menu bar item "NotificationCenter, Do Not Disturb enabled" of menu bar 2 then
key down option
click menu bar item "NotificationCenter, Do Not Disturb enabled" of menu bar 2
key up option
else
key down option
click menu bar item "Notification Center" of menu bar 2
key up option
end if
on error
key up option
end try
end tell
end tell
Posted on December 10, 2014, in AppleScript and tagged 10.10, applescript, Do Not Disturb, Notification Center, yosemite. Bookmark the permalink. 6 Comments.
Hello! Cool script 🙂
I was trying to run it at El Capitan 10.11.06 (the comments version) and it does not work.
Is it still working for you?
See the version below which works in 10.11.6
Thank you for the answer! 🙂
I get the following when running the script and nothing happens.
https://www.evernote.com/l/AkjxKJwskCJG7LiX1FiUwSrPxFQLTcJzBKs
I can’t reproduce that error. I’ve tested it on three different macs running 10.11.6 with no error here.
Your error -1719 is “illegal index”. First, try restarting the mac and testing it again. If the error continues after a restart, try adding a delay into the script. Just before each of the “click menu bar item” lines, create a new empty line and add the code
delay 1
Recompile the script and test again.
Unfortunately it does not work in 10.11. Has someone a working solution for El Capitan, please?
Yeah, it does, but you need to create a space in the two “NotificationCenter” words to read “Notification Center”. Looks like they changed it to make it more consistent. Here’s the full script for 10.11:
tell application "System Events"
tell application process "SystemUIServer"
try
if exists menu bar item "Notification Center, Do Not Disturb enabled" of menu bar 2 of application process "SystemUIServer" of application "System Events" then
key down option
click menu bar item "Notification Center, Do Not Disturb enabled" of menu bar 2
key up option
else
key down option
click menu bar item "Notification Center" of menu bar 2
key up option
end if
on error
key up option
end try
end tell
end tell