Advertisements

Monthly Archives: December 2014

applescript: toggle Notification Centre (Yosemite)

Notifications Center

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

Advertisements
%d bloggers like this: