gpt4 book ai didi

macos - 在 Mac OS X 上以编程方式获取 sleep 时间(和其他节能器设置)

转载 作者:行者123 更新时间:2023-12-01 05:34:13 29 4
gpt4 key购买 nike

我想以编程方式获取 Mac OS X 系统偏好设置中的节能器设置,特别是我正在编写的小应用程序的“显示 sleep ”或“计算机 sleep ”设置。

enter image description here

我知道您可以检索 sleep 设置,例如,使用来自 this SO answer 的命令行。

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

打印 60 (我的正确 sleep 时间),但如果可能的话,我更喜欢使用 native API 来获取这些设置。不幸的是,到目前为止,我的谷歌搜索没有发现任何有用的信息。 NSUserDefaults是我得到的最接近的,但我看不出如何使用它来获得我想要的设置。

任何人都可以提供帮助?

最佳答案

是的,自从提出这个问题以来已经有 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;
};
};
}

关于macos - 在 Mac OS X 上以编程方式获取 sleep 时间(和其他节能器设置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9734372/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com