如何使用 JXA 单击 UI 元素

too*_*tik 2 macos automator javascript-automation

请告诉我如何在应用程序窗口中单击点坐标?我尝试使用 JXA 技术在 OSX 10.10 上对我的应用程序进行 UI 自动化。在文档中我发现可以使用点击事件。我是 JXA 的初学者,找不到如何拨打电话。我在脚本编辑器中尝试过的代码片段:

var app = Application('my_application_path')
app.window.click.at('{100,100}')
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

小智 6

您可以使用系统事件应用程序与应用程序的用户界面进行交互。下面是一个在 Safari 中点击特定坐标的脚本:

// Activate Safari, so you will be able to click like a user

Application("Safari").activate()

// Access the Safari process of System Events

var SystemEvents = Application("System Events")
var Safari = SystemEvents.processes["Safari"]

// Call the click command, sending an array of coordinates [x, y]

Safari.click({ at: [300, 100] })
Run Code Online (Sandbox Code Playgroud)

如果您想单击特定按钮(或用户界面的其他元素),则单击该特定元素更为合适。例如:

// Click the third button of Safari's first window to minimize it

Safari.windows[0].buttons[2].click()
Run Code Online (Sandbox Code Playgroud)

要了解可以与哪些用户界面元素交互以及如何交互,请查看系统事件脚本字典中的流程套件。要打开字​​典,请在脚本编辑器的菜单栏中选择“窗口”>“库”,然后在“库”窗口中选择“系统事件”。