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

3k views Asked by At

I would like to programmatically wake up a Mac. I started off by using IOPMSchedulePowerEvent, however, that method requires admin access and privileged helpers. 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.

4

There are 4 answers

6
Nicholas Riley On BEST ANSWER

Using IOPMSchedulePowerEvent is the right thing to do. 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; there's also a third-party modification, AuthorizedTasksInCocoa, which might be a bit easier to use (I haven't tried it).

1
Gordon Davisson On

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

0
Steven Fisher On

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

What you'll want to do is schedule a 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.

As for elevating, Apple talks about it in Secure Coding Guide.

0
Akshay On

You can schedule wake up events with IOPMSchedulePowerEvent through the power manager. You may be able to schedule an immediate wake up. pmset is a command line wrapper for the power manager. You can also prevent sleep with IOCancelPowerChange in certain cases.

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.

==>Normal sleep is different from clamshell closed sleep. To affect the latter you must write a kernel extension like Insomnia.