如何访问UI哪个applescript无法访问?

How to access UI which applescript can not access?

我正在寻找访问UI applescript 无法访问的方法。我知道 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

这只是 returns 2 UI 个元素 imagestatic text.

{
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 个元素。因为我可以点击"Login Options".

幸运的是通过发送击键键码命令被正确接收。

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

我的问题是...

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

谢谢。

您可以像使用系统偏好设置时一样直接模拟点击,但您需要知道要点击哪个对象。 仅供参考,这里是系统首选项点击和 select 蓝牙的示例。它给出了实现方法的想法:

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"

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

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 不是 "ignoring" 登录选项控件;
  2. 你不需要点击,你可以使用 "reveal"(你的意思是输入 "can't" 而不是 "can" 吗? -- 我已经编辑了你的 post 以更正确地格式化您的问题);
  3. 我认为这使得第 3 个问题没有实际意义;
  4. 我的例子展示了如何做到这一点w/o点击
  5. 如何访问 "non-accessable" 项——更大的问题:

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

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