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

q9yhzks0  于 2022-11-30  发布在  Shell
关注(0)|答案(4)|浏览(141)

我正在尝试使用applescript读取请勿打扰或dnd的状态。
由于某种原因,无论dnd是否打开,它总是返回“1”。

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

堆栈编辑器:脚本编辑器创建并运行脚本OS:macOS Monterey

whhtz7ly

whhtz7ly1#

Monterey 解决方案

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

如果启用了“请勿打扰”,则返回1,否则返回0

m2xkgtsf

m2xkgtsf2#

如果你不介意通过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
7gcisfzg

7gcisfzg3#

在我的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
kgqe7b3p

kgqe7b3p4#

您也可以使用shell脚本:

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

相关问题