Objective c 如何访问applescript无法访问的UI?
我正在寻找访问applescript无法访问的UI的方法。我知道applescript可以访问UI元素。但有时我会遇到无法通过applescript访问UI元素的情况。就像下面Objective c 如何访问applescript无法访问的UI?,objective-c,xcode,macos,applescript,Objective C,Xcode,Macos,Applescript,我正在寻找访问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
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元素,它包含两个以上的元素。因为我可以点击“登录选项”
幸运的是,通过发送按键笔划和按键代码命令,可以正确接收命令
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元素
- 为什么键笔划和键代码命令不能访问“登录选项”UI元素
- 如果不使用按键笔划和按键代码命令,是否真的无法选择“登录选项”UI元素
- 如何访问applescript不可访问的UI元素
谢谢。您可以像使用系统首选项时那样直接模拟CLIC,但您需要知道要单击哪个对象。 仅供参考,这里是一个系统首选项的示例,单击并选择蓝牙。它给出了一种方法:
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
所以
doSysEvSelect("Internal Microphone", "Sound")
使用系统事件可能会令人沮丧。当然,我们可以找到很多例子。如果你真的陷入困境,你可以使用Sikuli()来自动化任何事情,但这完全依赖于用户界面和点击
[编辑]
下面是一个示例,说明在登录选项中的一个项目出现后如何处理(这是您需要使用系统事件的地方):
请注意,要找到正确的锚点名称,可以执行诸如当前窗格的锚点名称之类的操作。
doSysEvSelect("Internal Microphone", "Sound")
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