slh*_*hck 10 applescript macos
假设我想使用 AppleScript 单击出现在窗口中某处的按钮。click button工作,但你必须知道你想按哪个按钮。
例如:当您总是要等待扫描仪完成,然后切换到 Image Capture.app,然后再次单击扫描时,扫描有点麻烦。
所以,我想在这里自动点击这个按钮……

好吧,我认为这会相对容易,但一开始就我得到了:
tell application "System Events"
tell process "Image Capture"
click button "Scan" of window "Image Capture"
end tell
end tell
Run Code Online (Sandbox Code Playgroud)
当然,按钮埋得更深(即在某些拆分组中)。
使用Accessibility Inspector我可以看到它在哪里,但我真的必须沿着 UI 元素树找到按钮吗?有什么捷径吗?如果没有,我错过了click button "Scan" of window "Image Capture"什么?

更一般的:我在哪里可以找到 UI 元素层次结构中的按钮?
slh*_*hck 10
如果按钮不能直接使用,您可能不得不猜测。
在 High Sierra 中有效的是:
tell application "System Events"
tell process "Image Capture"
click button "Scan" of group 2 of splitter group 1 of window "Image Capture"
end tell
end tell
Run Code Online (Sandbox Code Playgroud)
在较旧的 macOS 版本上,group 1可能必须使用。
另一种解决方法是激活窗口并按空格键:
tell application "Image Capture"
activate
tell application "System Events" to key code 49
end tell
Run Code Online (Sandbox Code Playgroud)
您实际上可以尝试通过运行以下命令来关闭它:
UI elements of window 1
UI elements of splitter group 1 of window 1
UI elements of group 1 of splitter group 1 of window 1
Run Code Online (Sandbox Code Playgroud)
或者,正如@Lri 在评论中所说:
properties of UI elements of window 1
properties of UI elements of UI elements of window 1
Run Code Online (Sandbox Code Playgroud)
这将向您显示包含的元素列表,您可以从那里猜出您的方式。