如何使用Javascript for Automation(Yosemite)在窗格中与控件交互

How to interact with controls in a pane with Javascript for Automation (Yosemite)

本文关键字:交互 控件 Javascript 何使用 for Automation Yosemite      更新时间:2023-09-26

我正在尝试将旧的Applescript转换为Javascript。脚本的目的是打开和关闭Internet共享。我已经能够打开"共享"窗格并找到"Internet共享"锚点,但无法弄清楚如何与窗格上的控件交互。在Applescript中,我让System Events告诉System Preferences单击特定的复选框,但到目前为止,我使用Javascript尝试的所有操作都返回了迟钝的错误。

到目前为止,我得到的是:

prefs = Application('System Preferences')
sharePane = prefs.panes.byName('Sharing')
anchors = sharePane.anchors()
netAnchor = ""
for (i in anchors) {
    if (anchors[i].name().search('net') > -1) {
        netAnchor = anchors[i]
    }
}

开始吧。

prefs = Application("System Preferences");
prefs.activate();
prefs.panes.byName("Sharing").reveal();
SystemEvents = Application("System Events");
procPref = SystemEvents.processes["System Preferences"];
// option 1: fixed row number
procPref.windows[0].groups[0].scrollAreas[0].tables[0].rows[6].checkboxes[0].click();

或者,如果您更喜欢不依赖于知道确切行号的解决方案:

prefs = Application("System Preferences");
prefs.activate();
prefs.panes.byName("Sharing").anchors.byName("Internet").reveal(); // achor needed for option #3
SystemEvents = Application("System Events");
procPref = SystemEvents.processes["System Preferences"];
// option 2: select row by label
procPref.windows[0].groups[0].scrollAreas[0].tables[0].rows().forEach(function(r) {
  if (r.staticTexts[0].name() === "Internet Sharing") r.checkboxes[0].click();
});
// option 3: let reveal select the correct row, then press space to toggle
procPref.windows[0].groups[0].scrollAreas[0].focused = true;
delay(1); // give it some time to get the window activated before pressing space
SystemEvents.keystroke(" ");

更新:添加选项#2和#3以及列表详细信息。

以下是可供选择的锚点列表:

/* Internet */
/* Services_PrinterSharing */
/* Services_ARDService */
/* Services_RemoteAppleEvent */
/* Services_BluetoothSharing */
/* Main */
/* Services_DVDorCDSharing */
/* Services_RemoteLogin */
/* Services_ScreenSharing */
/* Services_WindowsSharing */
/* Services_PersonalFileSharing */

行列表(在Yosemite 10.10中)显示行号和(英文)标签:

/* 0: Screen Sharing */
/* 1: File Sharing */
/* 2: Printer Sharing */
/* 3: Remote Login */
/* 4: Remote Management */
/* 5: Remote Apple Events */
/* 6: Internet Sharing */
/* 7: Bluetooth Sharing */

我最终选择了GUI脚本,因为出于某种原因,这对我来说似乎更容易。我在下面发布了我的脚本,但这是完整的脚本。然而,以上内容在没有GUI脚本的情况下实现了我的要求。

startPrefs()
Events = Application('System Events')
Prefs = Events.processes['System Preferences']
Prefs.windows[0].scrollAreas[0].buttons.byName("Sharing").click()
delay(1)
ShareWindow = Application("System Events").applicationProcesses.byName("System Preferences").windows.byName("Sharing")
ShareWindow.groups.at(0).scrollAreas.at(0).tables.at(0).rows.at(7).select()
ShareWindow.groups.at(0).popUpButtons.at(0).click().menus.at(0).menuItems.byName("Ethernet").click()
delay(1)
portRows = ShareWindow.groups.at(0).scrollAreas.at(1).tables.at(0).rows()
for (i in portRows) {
    if (portRows[i].textFields.at(0).value() == "Wi-Fi") {
        if (portRows[i].checkboxes.at(0).value() == 0) {
            portRows[i].select()
            portRows[i].checkboxes.at(0).click()
        }
    }
    else {
        if (portRows[i].checkboxes.at(0).value() == 1) {
            portRows[i].select()
            portRows[i].checkboxes.at(0).click()
        }
    }
}
ShareWindow.groups.at(0).scrollAreas.at(0).tables.at(0).rows.at(7).select()
ShareWindow.groups.at(0).scrollAreas.at(0).tables.at(0).rows.at(7).checkboxes.at(0).click()
delay(1)
ShareWindow.sheets.at(0).buttons.byName("Start").click()
delay(30)
ShareWindow.groups.at(0).scrollAreas.at(0).tables.at(0).rows.at(7).select()
ShareWindow.groups.at(0).scrollAreas.at(0).tables.at(0).rows.at(7).checkboxes.at(0).click()
Prefs.quit()
Prefs = Application('System Preferences')
Prefs.quit()
function startPrefs() {
    Prefs = Application('System Preferences')
    Prefs.activate()
    delay(2)
    if (Prefs.windows[0].name() != "System Preferences") {
        Prefs.quit()
        startPrefs()
    }
}