如何阅读不打扰使用苹果说明书



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

出于某种原因,它总是返回";1〃;不管dnd是开还是关。

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

堆栈编辑器:用于创建和运行脚本的脚本编辑器操作系统:macOS蒙特利

蒙特雷解决方案

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

如果"是",则返回CCD_;请勿打扰";否则为0

如果您不介意通过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上,您的纯苹果脚本运行良好:

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

最新更新