简体   繁体   English

如何使用Mac上的系统首选项以编程方式安排系统唤醒?

[英]How to programmatically schedule system wake up using system preferences on a Mac?

I would like to programmatically wake up a Mac. 我想以编程方式唤醒Mac。 I started off by using IOPMSchedulePowerEvent, however, that method requires admin access and privileged helpers. 我开始使用IOPMSchedulePowerEvent,但是,该方法需要管理员访问权限和特权帮助程序。 I've implemented and it works but is way too complicated. 我已经实现了它的工作但是太复杂了。

I've looked around for a better approach and found that you can schedule the system to wake up by going to, System Preferences -> Energy Saver -> Schedule. 我已经四处寻找更好的方法,并发现您可以通过转到系统偏好设置 - >节能器 - >计划来安排系统唤醒。 Is there a way to programmatically schedule a wake up time using this method? 有没有办法以编程方式使用此方法安排唤醒时间?

Thanks. 谢谢。

Edit: I understand the system requires admin access to schedule system wakeup and sleep time. 编辑:我了解系统需要管理员访问来安排系统唤醒和睡眠时间。 That is ok. 那没问题。 I would like to be able to do this programmatically. 我希望能够以编程方式执行此操作。 Having the user authorize the functionality is ok provided that they are only asked once. 让用户授权该功能是可以的,前提是它们只被询问一次。 Subsequent requests to set the system wakeup time should not have to require user authorization. 设置系统唤醒时间的后续请求不必要求用户授权。

Using IOPMSchedulePowerEvent is the right thing to do. 使用IOPMSchedulePowerEvent是正确的做法。 However, if you create a privileged helper tool you only need to prompt the user once for his/her password. 但是,如果您创建特权帮助工具,则只需提示用户一次输入他/她的密码。 Apple's BetterAuthorizationSample code provides an example of how to do this; Apple的BetterAuthorizationSample代码提供了如何执行此操作的示例; there's also a third-party modification, AuthorizedTasksInCocoa , which might be a bit easier to use (I haven't tried it). 还有一个第三方修改, AuthorizedTasksInCocoa ,它可能更容易使用(我还没有尝试过)。

I don't have a full answer for you, but this should help immensely: http://www.opensource.apple.com/source/PowerManagement/PowerManagement-211/pmset/pmset.c 我没有给你一个完整的答案,但这应该有很大的帮助: http//www.opensource.apple.com/source/PowerManagement/PowerManagement-211/pmset/pmset.c

What you'll want to do is schedule a kIOPMAutoWake . 你想要做的是安排一个kIOPMAutoWake Find that constant (not a variation thereof) in the code and start reading. 在代码中找到常量(不是其变体)并开始阅读。 It's not THAT long. 这不长。

You can probably even use pmset directly, but that would be pretty inelegant. 您甚至可以直接使用pmset ,但这样会非常不优雅。

As for elevating, Apple talks about it in Secure Coding Guide . 至于提升,Apple在“ 安全编码指南”中对此进行了讨论。

I'm pretty sure this is intended to require admin access. 我很确定这是为了要求管理员访问。 For example, the options in System Preferences -> Energy Saver -> Schedule are unavailable unless you're logged in as an admin or have authenticated to System Preferences as an admin. 例如,系统偏好设置 - >节能器 - >计划中的选项不可用,除非您以管理员身份登录或已作为管理员身份验证到系统偏好设置。 If you found a way to do it without any special privs, I'd expect Apple to regard that as a security bug, and fix it (thus breaking your program). 如果你找到了一种没有任何特殊权限的方法,我希望Apple将其视为一个安全漏洞并修复它(从而破坏你的程序)。

You can schedule wake up events with IOPMSchedulePowerEvent through the power manager. 您可以通过电源管理器使用IOPMSchedulePowerEvent安排唤醒事件。 You may be able to schedule an immediate wake up. 您可以安排立即醒来。 pmset is a command line wrapper for the power manager. pmset是电源管理器的命令行包装器。 You can also prevent sleep with IOCancelPowerChange in certain cases. 在某些情况下,您还可以使用IOCancelPowerChange来预防睡眠。

You may be able to prevent sleep or wake up by generating a mouse or key event. 您可以通过生成鼠标或按键事件来阻止睡眠或唤醒。 One way to generate events is with CGPostKeyboardEvent. 生成事件的一种方法是使用CGPostKeyboardEvent。

==>Normal sleep is different from clamshell closed sleep. ==>正常睡眠不同于翻盖闭合睡眠。 To affect the latter you must write a kernel extension like Insomnia. 要影响后者,你必须编写像Insomnia这样的内核扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM