I'm looking at the open source code for the pmset command and attempting to modify the DisableSleep key in macOS's power management settings.
It seems to come down to running the following line of code where msdict contains a boolean value for "SleepDisabled":
let PMEyeIOCFPrefsPath = "com.apple.PowerManagement"
let PMEIOSystemPowerSettingsKey = "SystemPowerSettings"
CFPreferencesSetValue(PMEIOSystemPowerSettingsKey as CFString, msdict as CFDictionary,
PMEyeIOCFPrefsPath as CFString, kCFPreferencesAnyUser, kCFPreferencesCurrentHost)
You need to be root to do this, and this code won't run from the pmset command line tool unless run as root. When I run the above from a menubar app I'm writing, I get the following explanation...
2020-03-19 16:15:03.527875+0000 Open Eye[16320:301001] [User Defaults] attempt to set {
SleepDisabled = 1;
"Update DarkWakeBG Setting" = 1;
} for key in SystemPowerSettings in read-only (due to a previously logged write error) preferences domain CFPrefsPlistSource<0x600002c14e80> (Domain: com.apple.PowerManagement, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null),
Contents Need Refresh: No)
Ultimately I'm trying to run a few pmset commands from a menubar app so I don't have to continually lookup keys and open the terminal just to keep my system awake.
So how can I get this code to run with or without a popup to authenticate as root?