02/05/13 11:12:53

Automate Notification Center with Keyboard Maestro

I’ve just been reading this great tip over at TUAW. So I’ve revisited "Turn off Notification Center while your working macro" macro as well and changed it accordingly. The new method is more stable because the Notification Center’s current state can be measured with a defaults read. My previous solution could only “invert” whatever state Notification Center was currently in. The download has been updated as well.

Toggle Notification Center’s current state:

-- read current state
set notificationState to do shell script "defaults read ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist doNotDisturb"

-- turn NC on/off
if notificationState contains "0" then
    do shell script "defaults write ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist doNotDisturb -boolean true"
else if notificationState contains "1" then
    do shell script "defaults write ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist doNotDisturb -boolean false"
end if

-- tell NC when it was toggled (required)
set theDate to quoted form of (do shell script "date +\"%Y-%m-%d %I:%M:%S +0000\"")
do shell script "defaults write ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist doNotDisturbDate -date " & theDate 

-- restart NC process for changes to take effect
do shell script "killall NotificationCenter"

Similarly you can set NC on or off using this:

do shell script "defaults write ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist doNotDisturb -boolean true"
set theDate to quoted form of (do shell script "date +\"%Y-%m-%d %I:%M:%S +0000\"")
do shell script "defaults write ~/Library/Preferences/ByHost/com.apple.notificationcenterui.*.plist doNotDisturbDate -date " & theDate 
do shell script "killall NotificationCenter"