Reputation: 65
I'm trying to convert an old Applescript to Javascript. The purpose of the script is to toggle on and off Internet Sharing. I have been able to open up the Sharing pane and locate the Internet Sharing anchor, but cannot figure out how to interact with the controls on the pane. In Applescript I had System Events tell System Preferences to click a specific checkbox but everything I've tried to this point with Javascript has returned obtuse errors.
Here's what I've gotten 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]
}
}
Upvotes: 3
Views: 953
Reputation: 65
I ended up going with GUI Scripting because for some some reason it seemed easier for me. I'm posting my script below but it's the full script. However the above accomplishes what I asked for without the GUI scripting.
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()
}
}
Upvotes: 0
Reputation: 2663
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 does not depend on knowing the exact row 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 list details.
Here is the 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 the list of rows (in Yosemite 10.10) showing the row number and (English) label:
/* 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 */
Upvotes: 3