如何 select 系统偏好设置中的特定复选框?

How to to select specific checkboxes in System Preferences?

我正在尝试 select 键盘菜单项和选项卡的第三个复选框,以使用脚本的一个 运行 更改基本的 fn 键功能。其余代码似乎工作正常,但我最近才开始尝试编写代码,所以我不知道。

这是我当前的代码:

tell application "System Preferences"
    activate
    set current pane to pane id "com.apple.preference.keyboard"
    delay 1
    tell application "System Events"
        tell (click checkbox 3 of tab group 1)
            delay 2
        end tell
    end tell
end tell
tell application "System Preferences" to quit

这是错误消息:

error "System Events got an error: Can’t get tab group 1." number -1728 from tab group 1

看来我只是没有正确定义它,但我不知道如何定义。感谢您的帮助!

此外,这不是必需的,但是否可以 运行 脚本而无需明显打开系统偏好设置应用程序?

以下示例 AppleScript codemacOS Catalina 下测试 并单击 复选框 后关闭键盘背光: 系统偏好设置 > 键盘 > 键盘

按照编码,它执行以下操作:

  • 检查 系统偏好设置 是否为 运行,如果是,则将其关闭以便不必看到 UI 在不同的 窗格中闪烁
  • 如果 系统偏好设置 不是 运行 它会打开目标 anchor/pane 而不会显示 UI.
  • 单击目标 复选框
  • 关闭系统偏好设置

示例 AppleScript 代码:

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
    delay 0.1
end if

repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

tell application "System Preferences" to ¬
    reveal anchor "keyboardTab" of pane id ¬
        "com.apple.preference.keyboard"

tell application "System Events"
    tell front window of application process "System Preferences"
        repeat until (exists checkbox 3 of tab group 1)
            delay 0.01
        end repeat
        click checkbox 3 of tab group 1
        delay 0.1
    end tell
end tell

tell application "System Preferences" to quit

注意:示例 AppleScript code 就是这样,没有任何包含的 错误处理 ,不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟