对于缺少 AppleScript 字典的第 3 方应用程序,您有两种选择。
选项 1:
使用上述系统事件对元素执行操作,例如单击按钮,将文本键入字段等。诀窍是在 Applescript 识别的语法中识别元素。除了上面提到的 UIElementInspector 可能会令人困惑并且偶尔会出错/不完整,您还可以在单独的 Applescript 编辑器中运行以下命令。例如,要获取 Telegram 中活动窗口(窗口 1)的所有 UI 元素:
tell application "System Events" to tell application process "Telegram" to tell window 1
UI elements
end tell
在 Telegram 中获取主菜单栏的所有 UI 元素:
tell application "System Events" to tell application process "Telegram" to tell menu bar 1
UI elements
end tell
在每种情况下,结果窗格都会显示该窗口或菜单栏中所有可用 UI 元素的逗号分隔列表。此外,Applescript 保证可以识别列出的语法。只需识别正确的元素并告诉系统事件告诉它该做什么。
例如,如果您想在 TextEdit 中单击菜单项“格式”,请先运行以下命令:
tell application "System Events" to tell application process "TextEdit" to tell menu bar 1
UI elements
end tell
“结果”窗格中的结果如下:
menu bar item "Format" of menu bar 1 of application process "TextEdit" of application "System Events"
将其转换为 Applescript,运行脚本,它将单击“格式”菜单:
tell application "TextEdit" to activate --you need TexEdit up and running to click its menu bar
tell application "System Events" to click menu bar item "Format" of menu bar 1 of application process "TextEdit"
对于子菜单等,您只需迭代请求子菜单的 UI 元素的过程。 GUI 脚本是迭代和经验的。
选项2:
下载免费的终端/命令行应用程序cliclick,它允许您单击屏幕上的任何点。按住command + shift + 4,可以用光标手动识别你要点击的屏幕坐标。