system-configuration
v0.0.2
Published
system configuration framework
Downloads
1
Readme
Node macOS System Configuration Services
Request authorization and make changes to the macOS system configuration in JavaScript from Node.
Framework References:
Setup
npm i system-configuration
Example: Setting the System Proxy
const SC = require('system-configuration');
function setSystemProxy(host, port, callback) {
// 1. request authorization from the user to modify the systems network configuration
SC.requestAuthorization(
['system.services.systemconfiguration.network'],
(token, err) => {
if (err) {
// was not authorized by user
callback('not authorized');
}
// 2. get the primary network services configuration
const primaryServiceId = getPrimaryServiceId();
const systemPreferences = new SC.Preferences('name', token);
const servicePreferences = systemPreferences.getNetworkService(
primaryServiceId
);
// 3. grab the services proxy configuration
const proxy = servicePreferences.getProtocol('Proxies');
const configuration = proxy.getConfiguration();
// 4. make changes
configuration['HTTPEnable'] = true;
configuration['HTTPProxy'] = host;
configuration['HTTPPort'] = port;
// 5. update the configuration
proxy.setConfiguration(configuration);
// 6. apply the updated configuration to the system
if (!systemPreferences.commit() || !systemPreferences.apply()) {
callback('update failed');
}
// 7. Success!
callback(null);
}
);
}
// Find the primary network service identifier
function getPrimaryServiceId() {
const primaryServiceKey = SC.createGlobalNetworkEntityKey(
SC.Schema.Prefixes.State,
SC.Schema.NetworkEntity.IPv4
);
const dynamicStore = new SC.DynamicStore('name');
const primaryServiceId = dynamicStore.read(primaryServiceKey)[
'PrimaryService'
];
}