【问题标题】:Applescript System Preferences automationApplescript 系统偏好自动化
【发布时间】:2021-08-26 22:45:49
【问题描述】:

我正在努力自动设置系统首选项,但我遇到了一个问题。此代码应选择 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

有没有其他方法可以更改 Dock 和菜单栏设置或仅访问滚动区域项目?

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

【问题讨论】:

    标签: macos automation applescript system ui-automation


    【解决方案1】:

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


    UI Scriptingma​​cOS Big Sur 中的 System Preferences 已成为一场噩梦,就像许多过去工作的方法一样在以前版本的 ma​​cOS 中不再适用于 ma​​cOS Big Sur。当使用 XcodeAccessibility Inspector 时,许多 UI 元素 报告 Parent 不会将元素报告为其子元素之一,这然后让他们无法与他们交流。或者某些 代码 可能会工作一次,然后不能下一次。我编写了一些打开 Wi-Fi代码,然后单击了在菜单栏中显示 复选框。它工作了几次,现在不行了。

    我写的原始代码偶尔会起作用我不会发布,但是,以下示例 AppleScript 代码 em> 在 ma​​cOS Big Sur 11.4 下测试时始终如一地工作,尽管我认为它是 kludgy UI 脚本,因为它在屏幕上可见,由于时间问题,或者如果 分层 UI 元素 结构 由于 ma​​cOS 更新而发生更改/升级。

    示例 AppleScript 代码,如下所示,在 ma​​cOS Big 下的 Script Editor 中进行了测试Sur 11.4,System Preferences 中的 Language & Region 设置设置为 English (US) — Primary 并且为我工作没有问题1.

    • 1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要且适当的设置已设置/根据需要解决。

    脚本要求在系统偏好设置上选中使用键盘导航在控件之间移动焦点 复选框 > > Keyboard > Shortcuts 选项卡,并且按照编码,脚本检查其状态并切换复选框,如必要的,基于其当前状态。

    脚本还首先检查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
    

    注意事项:

    如果使用键盘导航在控件之间移动焦点 复选框的正常状态是未选中的,那么不要立即返回运行脚本要返回,因为 users 全局首选项文件 中的 property list item "AppleKeyboardUIMode" 需要一两秒才能更新更改。我提到这一点主要是为了在进行测试时比在正常生产使用时更多,因为那时它不应该成为问题。


    注意:示例 AppleScript 代码 就是这样,没有任何包含的错误处理不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2016-09-15
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多