binarybob
binarybob

Reputation: 3569

Programmatically get time until sleep (and other Energy Saver settings) on Mac OS X

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

enter image description here

I'm aware you can retrieve the 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'd prefer to use a native API to get these settings if possible. Unfortunately, my googling so far hasn't turned up anything useful. NSUserDefaults was the closest I got, but I couldn't see how that could be used to get the settings I'm after.

Anyone able to help?

Upvotes: 4

Views: 1994

Answers (2)

Dimitar Nestorov
Dimitar Nestorov

Reputation: 2604

After looking at the source of pmset this is what I've come up with:

#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.

Upvotes: 0

David
David

Reputation: 3323

Yes, it has been over 4 years since this question was asked... It is not clear in what language the code was desired. Using Objective-C.

The power saver preferences are found in:

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

From within a 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 the dictionary looking 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;
            "Standby Delay" = 4200;
            "Standby Enabled" = 0;
            "System Sleep Timer" = 15;
            TTYSPreventSleep = 1;
            "Wake On AC Change" = 0;
            "Wake On Clamshell Open" = 1;
        };
    };
}

Upvotes: 1

Related Questions