Programmatically get time to sleep (and other energy-saving settings) on Mac OS X

I would like to programmatically get the Energy Saver settings in System Preferences on Mac OS X, paticularly, the "Display Sleep" or "Computer Sleep" settings for the small application I'm writing.

enter image description here

I know that you can get sleep settings, for example, using the command line from this SO answer

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

which prints 60 (my correct sleep time), but I would rather use my own API to get these parameters, if possible. Unfortunately, so far my search engine has not found anything useful. NSUserDefaults was the closest I received, but I could not figure out how to use this to get the settings I needed.

Anyone who can help?

+4
source share
2 answers

Yes, more than 4 years have passed since this question was asked ... It is unclear in which language the code is needed. Using Objective-C.

Energy saving settings are located in:

 /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"]; 

with a dictionary similar to:

 { 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; "Standby Delay" = 4200; "Standby Enabled" = 0; "System Sleep Timer" = 15; TTYSPreventSleep = 1; "Wake On AC Change" = 0; "Wake On Clamshell Open" = 1; }; }; } 
+1
source

Looking at the source of pmset , I came to the following:

 #include <SystemConfiguration/SystemConfiguration.h> #include <CoreFoundation/CoreFoundation.h> #define kIOPMDynamicStoreSettingsKey "State:/IOKit/PowerManagement/CurrentSettings" #define kIOPMSystemSleepKey "System Sleep Timer" 
 SCDynamicStoreRef dynamicStore = SCDynamicStoreCreate(NULL, CFSTR("get-sleep-time"), NULL, NULL); CFDictionaryRef dictionaryRef = SCDynamicStoreCopyValue(dynamicStore, CFSTR(kIOPMDynamicStoreSettingsKey)); CFTypeRef typeRef = CFDictionaryGetValue(dictionaryRef, CFSTR(kIOPMSystemSleepKey)); int minutes; CFNumberGetValue(typeRef, kCFNumberIntType, (void *)&minutes); CFRelease(dynamicStore); CFRelease(dictionaryRef); CFRelease(typeRef); 

minutes will contain the computer sleep value in minutes.

0
source

Source: https://habr.com/ru/post/1401767/


All Articles