【问题标题】:How to read do not disturb using applescript?如何使用 applescript 阅读请勿打扰?
【发布时间】:2022-11-28 09:33:40
【问题描述】:

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

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

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

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

【问题讨论】:

  • 仅供参考,我也尝试过没有 com.apple.notificationcenterui 的完整路径

标签: macos shell applescript macos-monterey do-not-disturb


【解决方案1】:

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

【讨论】:

  • 谢谢它有效,有没有办法在不打开控制中心的情况下读取值(从视觉角度来看)?
  • 我认为有一种方法可以通过比较存储配置的 plist 文件,在您通过 UI 更改设置之前和之后。据我所知,它不是那么清晰,不幸的是,我找不到关于它的注释,所以你需要谷歌它,祝你好运。
【解决方案2】:

在我的 Catalina 上,您的普通 Apple 脚本运行良好:

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

【讨论】:

  • 谢谢罗伯特,似乎这两种方法在蒙特雷都行不通。我相信 Apple 可能已经在通知方面做出了重大改变,也许与 Focus 模式有关。
【解决方案3】:

您也可以使用 shell 脚本:

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

【讨论】:

  • 它总是在蒙特雷返回“0”
【解决方案4】:

蒙特雷解决方案

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2013-03-10
    相关资源
    最近更新 更多