MacOS – get sleep time on Mac OS X (and other energy-saving settings)

I want to programmatically get the energy-saving settings in the system preferences on Mac OS X, especially the “display sleep” or “computer sleep” settings of the small application I am writing .

I know you can retrieve sleep settings, for example, using the command line of this SO answer

pmset -g | grep "^[ ]*sleep" | awk'{ print $2 }'

Print 60 (my correct sleep time), but I prefer to use the native API to get these settings. Unfortunately, so far I Googling did not find anything useful. NSUserDefaults is the closest I got, but I can’t see how to use it to get the settings I am after.

Can anyone help?

Yes, it’s been more than 4 years since I asked this question… It’s not clear what the code requires Language. Use Objective-C.

Power saving preferences can be found in the following locations:

/Library/Preferences/SystemConfiguration/com. apple.PowerManagement.plist

In the Mac application, we can now use:

NSString *powerMgt = @"/Library/Preferences/SystemConfiguration/ com.apple.PowerManagement.plist";
NSDictionary *power = [NSDictionary dictionaryWithContentsOfFile:powerMgt];
// for example the sleep time on AC power
NSNumber *sleepyTime = [[[power objectForKey:@"Custom Profile"] objectForKey:@"AC Power"] objectForKey:@"System Sleep Timer"];

Use a dictionary to look like:

{
ActivePowerProfiles = {
"AC Power" = "-1";
"Battery Power" = "-1";
};
" Custom Profile" = {
"AC Power" = {
"Disk Sleep Timer" = 10;
"Display Sleep Timer" = 10;
"Display Sleep Uses Dim" = 1;
GPUSwitch = 2;
"Hibernate File" = "/var/vm/sleepimage";
"Hibernate Mode" = 3;
"Mobile Motion Module" = 1 ;
PrioritizeNetworkReachabilityOverSleep = 0;
"Standby Delay" = 4200;
"Standby Enabled" = 0;
"System Sleep Timer" = 0;
TTYSPreventSleep = 1 ;
"Wake On AC Change" = 0;
"Wake On Clamshell Open" = 1;
"Wake On LAN" = 1;
};
" Battery Power" = {
"Disk Sleep Timer" = 10;
"Display Sleep Timer" = 10;
"Display Sleep Uses Dim" = 1;
GPUSwitch = 2;
"Hibernate File" = "/var/vm/sleepimage";
"Hibernate Mode" = 3;
"Mobile Motion Module" = 1;
ReduceBrightness = 1;< br /> "Standby Delay" = 4200;
"Standby En abled" = 0;
"System Sleep Timer" = 15;
TTYSPreventSleep = 1;
"Wake On AC Change" = 0;
"Wake On Clamshell Open" = 1 ;
};
};
}

I want to programmatically get energy saving in System Preferences on Mac OS X Settings, especially the “display sleep” or “computer sleep” settings for the small app I’m writing.

I know you can retrieve the sleep settings, for example, using the command line of this SO answer

pmset -g | grep "^[ ]*sleep" | awk'{ print $2 }'

Print 60 (my correct sleep time) , But I prefer to use the native API to get these settings. Unfortunately, my googling so far did not find anything useful. NSUserDefaults is the closest I got, but I can’t see how to use it Get the settings I am after.

Can anyone help?

Yes, it has been more than 4 years since I asked this question… It is not clear what language the code requires. Use Objective-C.

< /p>

Power saving preferences can be found in the following locations:

/Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist

In the Mac application, we can now use:

NSString *powerMgt = @"/Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist";
NSDictionary *power = [NSDictionary dictionaryWithContentsOfFile:powerMgt];
// for example the sleep time on AC power
NSNumber *sleepyTime = [[[power objectForKey:@"Custom Profile"] objectForKey:@" AC Power"] objectForKey:@"System Sleep Timer"];

Use a dictionary to look like:

{
ActivePowerProfiles = {< br /> "AC Power" = "-1";
"Battery Power" = "-1";
};
"Custom Profile" = {
"AC Power "= {
"Disk Sleep Timer" = 10;
"Display Sleep Timer" = 10;
"Display Sleep Uses Dim" = 1;
GPUSwitch = 2;
"Hibern ate File" = "/var/vm/sleepimage";
"Hibernate Mode" = 3;
"Mobile Motion Module" = 1;
PrioritizeNetworkReachabilityOverSleep = 0;
"Standby Delay" = 4200;
"Standby Enabled" = 0;
"System Sleep Timer" = 0;
TTYSPreventSleep = 1;
"Wake On AC Change" = 0;< br /> "Wake On Clamshell Open" = 1;
"Wake On LAN" = 1;
};
"Battery Power" = {
"Disk Sleep Timer" = 10;
"Display Sleep Timer" = 10;
"Display Sleep Uses Dim" = 1;
GPUSwitch = 2;
"Hibernate File" = "/var/vm/ sleepimage";
"Hibernate Mode" = 3;
"Mobile Motion Module" = 1;
ReduceBrightness = 1;
"Standby Delay" = 4200;
" Standby Enabled" = 0;
"System Sleep Timer" = 15;
TTYSPreventSleep = 1;
"Wake On AC Change" = 0;
"Wake On Clamshell Open" = 1;
};
};
}

Leave a Comment

Your email address will not be published.