How to interact with controls in a panel using Javascript for Automation (Yosemite)
I am trying to convert old Applescript to Javascript. The purpose of the script is to enable and disable Internet sharing. I was able to open the Sharing panel and find the Internet Sharing binding, but I can't figure out how to interact with the controls in the panel. In Applescript, I had system events telling the system preferences to click a specific checkbox, but everything I've tried up to this point with Javascript returned dumb errors.
This is what I got so far:
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]
}
}
source to share
Here you go.
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();
Or, if you prefer a solution that doesn't depend on knowing the exact line number:
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(" ");
Update : added option # 2 and # 3 and detailed information about the list.
Here is a list of anchors to choose from:
/* Internet */
/* Services_PrinterSharing */
/* Services_ARDService */
/* Services_RemoteAppleEvent */
/* Services_BluetoothSharing */
/* Main */
/* Services_DVDorCDSharing */
/* Services_RemoteLogin */
/* Services_ScreenSharing */
/* Services_WindowsSharing */
/* Services_PersonalFileSharing */
And a list of lines (in Yosemite 10.10) showing the line number and (English):
/* 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 */
source to share
I ended up with GUI Scripting because for some reason I found it easier. I am posting the script below, but this is the complete script. However, the above does what I asked for without creating a 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()
}
}
source to share