【问题标题】:How to access UI which applescript can not access?如何访问applescript无法访问的UI?
【发布时间】:2015-10-15 06:28:09
【问题描述】:

我正在寻找访问 applescript 无法访问的 UI 的方法。我知道 applescript 可以访问 UI 元素。但有时我会遇到无法通过 applescript 访问 UI 元素的情况。像下面...

tell application "System Preferences" to activate
delay 1
tell application "System Events"
    tell process "System Preferences"
        set frontmost to true
        click button 22 of scroll area 1 of window 1
        delay 1
        UI elements of last group of list 1 of window 1
    end tell
end tell

这只会返回 2 个 UI 元素 图像静态文本

{
image 1 of group 3 of list 1 of window "Users & Groups" of application process "System Preferences" of application "System Events",
static text "Login Options" of group 3 of list 1 of window "Users & Groups" of application process "System Preferences" of application "System Events"
}

显然有一个可点击的 UI 元素,它属于以上 2 个元素。因为我可以点击“登录选项”。

幸运的是通过发送keystrokekey code命令被正确接收。

set DownArrow to 125
set UpArrow to 126
tell application "System Preferences" to activate
delay 1
tell application "System Events"
    tell process "System Preferences"
        set frontmost to true
        click button 22 of scroll area 1 of window 1
        delay 1
        keystroke tab
        repeat 3 times
            delay 1
            key code DownArrow
            delay 1
            key code UpArrow
        end repeat
    end tell
end tell
tell application "System Preferences" to quit

我的问题是……

  • 为什么 applescript 似乎忽略了“登录选项”UI 元素?
  • 为什么我不能用鼠标或触控板点击“登录选项”UI 元素?
  • 为什么keystrokekey code命令无法访问“登录选项”UI元素?
  • 真的没有办法在不使用keystrokekey code命令的情况下选择“登录选项”UI元素吗?
  • 如何访问 applescript 不可访问的 UI 元素?

感谢任何提示。任何代码(python、c、objective-c 等)都更受欢迎。:)

谢谢。

【问题讨论】:

    标签: objective-c xcode macos applescript


    【解决方案1】:

    要解决其中许多问题,您不需要系统事件。例如,系统偏好设置足以编写脚本,至少可以做到这一点,这可以解决您的一些问题:

    tell application "System Preferences"
        activate
        set the current pane to pane id "com.apple.preferences.users"
        reveal anchor "loginOptionsPref" of current pane
    end tell
    

    所以,

    1. AppleScript 不会“忽略”登录选项控件;
    2. 您不需要点击,您可以使用“显示”(您的意思是输入“不能”而不是“可以”吗?-- 我已编辑您的帖子以更正确地格式化您的问题);
    3. 我认为这使得第三个问题没有实际意义;
    4. 我的示例展示了无需点击的方法
    5. 如何访问“不可访问”的项目——更大的问题:

    虽然系统偏好设置确实可以编写脚本,但有时您可能需要系统事件。例如,我有一个用于声音窗格的脚本,它使用以下处理程序(子例程):

    on doSysEvSelect(whatToSelect, whatWindow)
        tell application "System Events"
            tell process "System Preferences"
                select (row 1 of table 1 of scroll area 1 of tab group 1 of window whatWindow whose value of text field 1 is whatToSelect)
            end tell
        end tell
    end doSysEvSelect
    

    ,我可以通过这样做来使用,例如:

    doSysEvSelect("Internal Microphone", "Sound")
    

    使用系统事件可能会令人沮丧。但是当然有很多例子可以找到。如果你真的很纠结,你可以使用 Sikuli (http://sikuli.org/) 来自动化几乎任何事情,但这完全依赖于 UI 和点击。

    [编辑]

    下面是一个示例,说明在登录选项中如何处理项目(这是您需要使用系统事件的地方):

    tell application "System Events"
        tell process "System Preferences"
            if value of checkbox "Show password hints" of group 1 of window "Users & Groups" is 0 then
                click checkbox "Show password hints" of group 1 of window "Users & Groups"
            end if
        end tell
    end tell
    

    【讨论】:

    • 请注意,您可以执行name of anchors of current pane 之类的操作来查找正确的锚点名称。
    【解决方案2】:

    您可以像使用系统首选项一样直接模拟 clics,但您需要知道要单击哪个对象。 仅供参考,这是系统首选项单击并选择蓝牙的示例。它提供了一个想法:

    tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preferences.Bluetooth"
    tell application "System Events"
        tell process "System Preferences"
            -- select row containing the word "mouse"
            repeat with I from 1 to count of row of table 1 of scroll area 2 of group 1 of front window
                set Nom_Perif to (description of static text 1 of row I of table 1 of scroll area 2 of group 1 of front window) as string
                if Nom_Perif contains "Mouse" then
                    set selected of row I of table 1 of scroll area 2 of group 1 of front window to true
                    if Nom_Perif contains "not connected" then
                        -- sert connection  via item 1 of menu
                        click menu button of group 1 of front window
                        delay 1
                        click menu item "Connect" of menu of menu button of group 1 of front window
                        delay 3
                    end if
                end if
            end repeat
        end tell
    end tell
    end tell
    quit application "System Preferences"
    

    【讨论】:

      猜你喜欢
      • 2018-12-07
      • 2016-10-31
      • 1970-01-01
      • 2019-07-01
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多