javascript 自动点击 iTunes 中的下载按钮
javascript automation click download button in iTunes
我需要使用 JXA 以编程方式从 iTunes 下载应用程序。我做了以下事情:
var its = SystemEvents.processes.byName('iTunes');
delay(3);
its.windows[0].buttons.byName('Get').click();
没有选择元素。我试过单击按钮 [0...7],其中 none 是 'Get' 按钮。我假设我需要的按钮在文档中,但 JXA 文档明确指出按钮元素是 Window 的子元素,而不是 Document 的子元素。关于如何点击相应按钮的任何想法?
"leaf"UI 元素(例如按钮)将位于 UI 元素 的潜在复杂层次结构的底部,其中在 iTunes 的情况下尤其如此。
为了让您有所了解,下面是免费应用的 App Store 页面上 Get
按钮的示例对象说明符(假设您已经确保此页面处于活动状态):
Application("System Events")
.applicationProcesses.byName("iTunes")
.windows.byName("iTunes")
.splitterGroups.at(0)
.scrollAreas.at(0)
.uiElements.at(0)
.groups.at(3)
.buttons.at(0)
问题是这个对象说明符在不同的页面上是不同的,所以理想情况下你只需将 filter 应用到 all UI 元素(通过 window 的 entireContents
属性)检索感兴趣的按钮:
// Get an array of all UI elements in iTunes window.
uiElems = Application("System Events").applicationProcesses['iTunes']
.windows[0].entireContents()
// Find all buttons whose description contains 'Get'.
btns = uiElems.filter(function(el) {
try {
return el.role() == 'AXButton'
&&
el.description().match(/\bGet\b/)
} catch (e) {}
})
// Click on the 1st button found.
btns[0].click()
关键是:在我相当新的机器上,这大约需要 20 秒 (!)。
我认为 .whose
风格的过滤器会更快,但我无法在这种情况下使用它,因为必须捕获异常 - 如上所述 - 但 .whose
似乎不支持嵌入式异常处理程序。
如果您愿意假设可以在其子树中找到按钮的层次结构中的较低级别,则可以大大加快速度:
// Get the group UI elements in one of which the 'Get' button is located.
grps = Application("System Events").applicationProcesses['iTunes'].
windows[0].splitterGroups[0].scrollAreas[0].uiElements[0].groups
// Loop over groups
count = grps.length
for (i = 0; i < count; ++i) {
// In the group at hand, find all buttons whose description contains 'Get'.
btns = grps[i].entireContents().filter(function(el) {
try {
return el.role() == 'AXButton'
&&
el.description().match(/\bGet\b/)
} catch (e) {}
})
// Exit loop, if a 'Get' button was found.
if (btns.length > 0) break
}
if (btns.length == 0) {
console.log('ERROR: No "Get" button found.')
} else {
// Click on the 1st button found.
btns[0].click()
}
运行不到 1 秒。在我的机器上。
不幸的是,UI 自动化(GUI 脚本)是一项棘手的工作。
对于交互式探索,有 Xcode 附带的 Accessibility Inspector 开发人员工具,但使用它并非易事,尤其是在将发现转化为代码。
我需要使用 JXA 以编程方式从 iTunes 下载应用程序。我做了以下事情:
var its = SystemEvents.processes.byName('iTunes');
delay(3);
its.windows[0].buttons.byName('Get').click();
没有选择元素。我试过单击按钮 [0...7],其中 none 是 'Get' 按钮。我假设我需要的按钮在文档中,但 JXA 文档明确指出按钮元素是 Window 的子元素,而不是 Document 的子元素。关于如何点击相应按钮的任何想法?
"leaf"UI 元素(例如按钮)将位于 UI 元素 的潜在复杂层次结构的底部,其中在 iTunes 的情况下尤其如此。
为了让您有所了解,下面是免费应用的 App Store 页面上 Get
按钮的示例对象说明符(假设您已经确保此页面处于活动状态):
Application("System Events")
.applicationProcesses.byName("iTunes")
.windows.byName("iTunes")
.splitterGroups.at(0)
.scrollAreas.at(0)
.uiElements.at(0)
.groups.at(3)
.buttons.at(0)
问题是这个对象说明符在不同的页面上是不同的,所以理想情况下你只需将 filter 应用到 all UI 元素(通过 window 的 entireContents
属性)检索感兴趣的按钮:
// Get an array of all UI elements in iTunes window.
uiElems = Application("System Events").applicationProcesses['iTunes']
.windows[0].entireContents()
// Find all buttons whose description contains 'Get'.
btns = uiElems.filter(function(el) {
try {
return el.role() == 'AXButton'
&&
el.description().match(/\bGet\b/)
} catch (e) {}
})
// Click on the 1st button found.
btns[0].click()
关键是:在我相当新的机器上,这大约需要 20 秒 (!)。
我认为 .whose
风格的过滤器会更快,但我无法在这种情况下使用它,因为必须捕获异常 - 如上所述 - 但 .whose
似乎不支持嵌入式异常处理程序。
如果您愿意假设可以在其子树中找到按钮的层次结构中的较低级别,则可以大大加快速度:
// Get the group UI elements in one of which the 'Get' button is located.
grps = Application("System Events").applicationProcesses['iTunes'].
windows[0].splitterGroups[0].scrollAreas[0].uiElements[0].groups
// Loop over groups
count = grps.length
for (i = 0; i < count; ++i) {
// In the group at hand, find all buttons whose description contains 'Get'.
btns = grps[i].entireContents().filter(function(el) {
try {
return el.role() == 'AXButton'
&&
el.description().match(/\bGet\b/)
} catch (e) {}
})
// Exit loop, if a 'Get' button was found.
if (btns.length > 0) break
}
if (btns.length == 0) {
console.log('ERROR: No "Get" button found.')
} else {
// Click on the 1st button found.
btns[0].click()
}
运行不到 1 秒。在我的机器上。
不幸的是,
UI 自动化(GUI 脚本)是一项棘手的工作。
对于交互式探索,有 Xcode 附带的 Accessibility Inspector 开发人员工具,但使用它并非易事,尤其是在将发现转化为代码。