如何使用 applescript 阅读请勿打扰?

How to read do not disturb using applescript?

我正在尝试使用 applescript 读取请勿打扰或请勿打扰的状态。

出于某种原因,无论 dnd 打开还是关闭,它总是 return“1”。

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

堆栈 编辑器:用于创建和 运行 脚本的脚本编辑器 OS: macOS 蒙特雷

如果您不介意在 Mac OS Monterey

上通过 UI 阅读它
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

在我的 Catalina 上,你的普通 Apple-script 工作正常:

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

这是 AppleScript Objective-C 解决方案:

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

您也可以使用 shell 脚本:

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