How to read do not disturb using applescript?

741 Views Asked by At

I am trying to read status of do not disturb or dnd using applescript.

For some reason, it always return "1" no matter what if the dnd is on or off.

do shell script "defaults -currentHost read ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb"

Stack Editor: Script Editor to create and run the script OS: macOS Monterey

4

There are 4 best solutions below

3
On BEST ANSWER

Solution for Monterey

#!/bin/bash
defaults read com.apple.controlcenter "NSStatusItem Visible FocusModes"

It returns 1 if "Do Not Disturb" is enabled, 0 otherwise.

2
On

If you don't mind reading it via the UI on Mac OS Monterey

log getDNDStatus()

on getDNDStatus()
    set currentState to 1
    tell application "System Events" to tell process "ControlCenter"
        click of menu bar item "Control Center" of menu bar 1
        if exists (first checkbox of front window whose title is "Focus") then set currentState to 0
    end tell
    tell application "System Events" to key code 53 -- Escape to close the control center popup
    currentState
end getDNDStatus
1
On

On my Catalina your plain Apple-script works fine:

set DNDStatus to (do shell script "defaults -currentHost read com.apple.notificationcenterui  doNotDisturb") as integer as boolean

Here is AppleScript Objective-C solution:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set defaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.apple.notificationcenterui"
set DNDStatus to (defaults's valueForKey:"doNotDisturb") as boolean
1
On

You can use a shell script too:

#!/bin/bash
DNDStatus=$(defaults -currentHost read com.apple.notificationcenterui  doNotDisturb)
echo $DNDStatus