我想以编程方式在Mac OS X上的系统偏好设置中获取节能设置,特别是我正在编写的小应用程序的“显示睡眠”或“计算机睡眠”设置。
我知道您可以检索睡眠设置,例如,使用this SO answer
中的命令行 pmset -g | grep "^[ ]*sleep" | awk '{ print $2 }'
打印60
(我的正确睡眠时间),但我更愿意使用本机API来获取这些设置。不幸的是,到目前为止我的谷歌搜索没有发现任何有用的东西。 NSUserDefaults
是我得到的最接近的,但我看不出如何用它来获取我想要的设置。
任何人都可以提供帮助吗?
答案 0 :(得分:1)
是的,自问这个问题以来已经超过4年......目前尚不清楚代码所需的语言。使用Objective-C。
节电器偏好可在以下位置找到:
/Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist
在Mac应用程序中,我们现在可以使用:
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"];
字典看起来像:
{
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 :(得分:0)
在查看pmset的来源之后,这就是我想出的:
#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
将包含以分钟为单位的计算机睡眠值。