Applescript 系统偏好设置自动化

Applescript System Preferences automation

我正在研究自动设置系统首选项,但我遇到了问题。此代码应 select Wi-Fi 选项卡但滚动区域 1 不存在,除非我手动单击属于滚动区域的任何元素。我尝试用许多外部程序模拟点击,但即便如此我也无法访问滚动区域

tell application "System Preferences"
    activate
    reveal pane id "com.apple.preference.dock"
end tell
tell application "System Events" to tell application process "System Preferences"
    delay 1

    tell scroll area 1 of window 1
        select row 3 of outline 1
    end tell
end tell

是否有任何其他方法来更改停靠栏和菜单栏设置或仅访问滚动区域项目?

编辑:最终目标是从菜单栏中隐藏 Wi-Fi 图标。

The end goal is to hide Wi-Fi icon from menu bar.


UI 系统偏好设置 macOS Big Sur 中的 脚本 已成为一场噩梦,因为以前版本的 macOS 中使用的许多方法在 macOS Big Sur 中不再有效。许多 UI 元素 报告 使用 Accessibility Inspector[=111] 时,父元素未将元素报告为其子元素之一 =] 的 Xcode,这使得无法与他们沟通。或者某些 code 可能工作一次,然后下一次就不行了。我写了一些 代码 打开 Wi-Fi 然后点击 在菜单栏中显示 复选框。试过几次,现在不行了。

原来的代码我写的,偶尔能用我不会post,但是下面的例子AppleScript codemacOS Big Sur 11.4 下测试时确实始终如一地工作,尽管这是我认为的 kludgy UI 脚本,因为它在屏幕上可见,很容易由于时间问题而失败,或者如果 层次结构 UI element structures 由于 macOS updates/upgrades 而改变.

示例 AppleScript 代码,如下所示,在[=63= macOS Big Sur 11.4 下的脚本编辑器 系统偏好设置 中的语言和地区 设置] 设置为 English (US) — Primary 并为我工作没有问题1.

  • 1 假定在 系统偏好设置 > 安全和隐私 > 隐私 已根据需要 set/addressed。

脚本 要求使用键盘导航在控件之间移动焦点 复选框 是检查 系统偏好设置 > 键盘 > 快捷方式 选项卡,如编码所示,script 检查其状态并根据需要根据其当前状态切换 复选框

脚本 还首先检查Wi-Fi 图标是否显示在菜单栏 如果没有,则停止执行脚本,因为它的目的是仅当它显示在 菜单栏 .

上时才执行

示例 AppleScript 代码:

--  # Get the fully qualified POSIX pathname of the target .plist file.

set thePropertyListFilePath to ¬
    the POSIX path of ¬
        (path to preferences from user domain as string) & ¬
    "com.apple.controlcenter.plist"

-- Get the value of 'NSStatusItem Visible WiFi' to determine if the
-- Wi-Fi icon is showing on the Menu Bar, and if it's not, then halt
-- execution of the script, as its purpose is to act only if it is.

tell application "System Events" to ¬
    tell the property list file thePropertyListFilePath to ¬
        set |Wi-Fi Menu Bar Icon Status| to the value of ¬
            the property list item ¬
                "NSStatusItem Visible WiFi"

if |Wi-Fi Menu Bar Icon Status| is false then return


--  # Check to see if System Preferences is 
--  # running and if yes, then close it.
--  # 
--  # This is done so the script will not fail 
--  # if it is running and a modal sheet is 
--  # showing, hence the use of 'killall' 
--  # as 'quit' fails when done so, if it is.
--  #
--  # This is also done to allow default behaviors
--  # to be predictable from a clean occurrence.

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

--  # Make sure System Preferences is not running before
--  # opening it again. Otherwise there can be an issue
--  # when trying to reopen it while it's actually closing.

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

--  # Get the fully qualified POSIX pathname of the target .plist file.

set thePropertyListFilePath to ¬
    the POSIX path of ¬
        (path to preferences from user domain as string) & ¬
    ".GlobalPreferences.plist"

--  # Get the value of AppleKeyboardUIMode to determine if the
--  # 'Use keyboard navigation to move focus between controls'
--  # checkbox is checked on the System Preferences >  
--  # Keyboard > Shortcuts tab.

tell application "System Events" to ¬
    tell the property list file thePropertyListFilePath to ¬
        set keyboardNavigation to the value of ¬
            the property list item "AppleKeyboardUIMode"

if keyboardNavigation = 0 then
    --  # Check the checkbox.
    my toggleKeyboardNavagition()
end if

--  # Open System Preferences to the Dock & Menu Bar pane.
--  # 
--  # This UI Script needs it to be visible, hence the activate command.

tell application "System Preferences"
    activate
    reveal pane id "com.apple.preference.dock"
end tell

tell application "System Events"
    set i to 0
    repeat until exists window "Dock & Menu Bar" of ¬
        application process "System Preferences"
        delay 0.1
        set i to i + 1
        if i ≥ 30 then return
    end repeat
end tell

--  # Tab to the 'Show in Menu Bar' checkbox and uncheck it.

tell application "System Events"
    key code 48 -- # tab key
    delay 0.2
    key code 125 -- # down arrow key
    delay 0.2
    key code 48 -- # tab key
    delay 0.2
    key code 49 -- # spacebar
    delay 0.1
end tell

if keyboardNavigation = 0 then
    --  # Uncheck the checkbox if it
    --  # was previously unchecked.
    my toggleKeyboardNavagition()
end if

delay 0.2

tell application "System Preferences" to quit


--  # Handler(s) #


--  # Toggles checkbox: 'Use keyboard navigation 
--  # to move focus between controls'

on toggleKeyboardNavagition()
    tell application "System Preferences"
        activate
        reveal anchor "shortcutsTab" of ¬
            pane id "com.apple.preference.keyboard"
    end tell
    tell application "System Events"
        tell front window of ¬
            application process "System Preferences"
            set i to 0
            repeat until (exists checkbox 1 of tab group 1)
                delay 0.1
                set i to i + 1
                if i ≥ 30 then return
            end repeat
            click checkbox 1 of tab group 1
        end tell
    end tell
end toggleKeyboardNavagition

备注:

如果使用键盘导航在控件之间移动焦点 复选框 的正常状态未选中,则不要运行 脚本 立即背靠背,因为 [=63= 中 property list item "AppleKeyboardUIMode"value 需要一两秒]users global preferences file 更新更改。我提到这一点主要是为了在进行测试时比在正常生产使用时更多,因为这应该不是问题。


注意:示例 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,适当设置延迟