1

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

user4150758
  • 384
  • 4
  • 17

4 Answers4

1

Solution for Monterey

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

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

Kubuntuer82
  • 1,487
  • 3
  • 18
  • 40
  • Hmm, I'm on Monterey. Above command always returns 0, but other solutions posted here don't work either. The system keyboard binding works thou. – Leo Ufimtsev Jan 31 '23 at 00:01
  • It works perfectly for me, perhaps it depends on some specific setting on your machine? – Kubuntuer82 Mar 01 '23 at 21:42
0

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
user3579815
  • 522
  • 3
  • 12
  • Thank you it works, is there a way to read the value without opening control center (from visual point of view)? – user4150758 Apr 10 '22 at 02:47
  • 1
    I think there's a way to do that by comparing the plist file that stores the configuration, before and after you change a setting via UI. It's not as clear cut as far as I remember and unfortunately, I couldn't find notes on that so you'll need to Google it, good luck. – user3579815 Apr 10 '22 at 08:08
0

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
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • Thanks Robert, seems like both approaches doesn't work in monterey. I believe Apple might have changed something significantly on notifications side perhaps something to do with Focus mode. – user4150758 Apr 11 '22 at 04:20
0

You can use a shell script too:

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