AppleScript UI Scripting点击失效及下拉菜单选择问题求助
搞定AppleScript UI自动化的点击与下拉菜单选择问题
一、表格行点击没反应?试试这几招
你说能获取到static text "General Info"的返回值,但点击没效果——这大概率是因为你选中的只是文本元素,而真正能触发交互的是它所在的row(行)。直接定位行来点击试试:
tell application "System Events" tell front window of application process "你的应用名称" -- 记得替换成目标应用的进程名 click row 1 of table 1 of scroll area 1 end tell end tell
要是还是不行,换用UI元素的perform action "AXPress"方法,有些顽固元素对这个命令更敏感:
tell application "System Events" tell front window of application process "你的应用名称" set targetRow to row 1 of table 1 of scroll area 1 perform action "AXPress" of targetRow end tell end tell
另外别忘了,必须给你的脚本工具(比如Script Editor)开启辅助功能权限——去系统设置>隐私与安全性>辅助功能里把它加进去,不然UI脚本根本跑不起来。
二、下拉菜单选不上指定选项?这么写就对了
要选中下拉菜单的第二个选项,得先把菜单展开,再定位到对应选项点击,步骤不能少:
tell application "System Events" tell application process "你的应用名称" -- 先点下拉按钮展开菜单 click pop up button 1 of front window -- 替换成你的下拉菜单元素路径 -- 加个短延迟,防止菜单还没展开就执行下一步 delay 0.5 -- 点击第二个菜单选项 click menu item 2 of menu 1 of pop up button 1 of front window end tell end tell
如果下拉菜单在更复杂的UI结构里(比如tab组、分组里),得补全定位路径,举个例子:
tell application "System Events" tell front window of application process "你的应用名称" click pop up button "设置名称" of group 1 of tab group 1 delay 0.3 click menu item 2 of menu 1 of pop up button "设置名称" of group 1 of tab group 1 end tell end tell
实用小工具推荐
要是拿不准元素路径,用系统自带的Accessibility Inspector(搜Spotlight就能找到,需要装Xcode开发者工具),它能实时查看应用的UI层级,帮你精准定位元素,少走弯路。
内容的提问来源于stack exchange,提问作者Kevin
相关产品推荐
相关产品推荐

