5

I've managed to get the display to sleep immediately with

pmset displaysleepnow

in terminal, however for waking the display I've only found

pmset schedule wake "12/24/2009 00:00:00" 

which fails to work if I try to schedule anything earlier than at least 10secs from now.

Is there any way to programmatically wake the display in Cocoa? Someone has suggested IOPMAssertionDeclareUserActivity but I couldn't find any examples on this so I am struggling.

I am using Swift by the way, but Objective C code is also welcome.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lee Andrew
  • 798
  • 7
  • 28
  • Perhaps try exploiting the "wake on network activity" feature. I think it's more commonly known as "Wake on LAN". – Abhi Beckert Oct 06 '14 at 23:02
  • 1
    what *exactly* are you trying to wake the display programmatically for? I dont see a point in waking a display when a user isn't present. – Brad Allred Oct 06 '14 at 23:04
  • Try [this](http://stackoverflow.com/questions/3315685/how-to-wake-from-sleep-programmatically)? The first answer seems relevant. – Travis Oct 06 '14 at 23:06
  • Possible duplicate of [How do I wake from display sleep in OSX 10.7.4?](https://stackoverflow.com/questions/10598809/how-do-i-wake-from-display-sleep-in-osx-10-7-4) – rogerdpack Dec 15 '17 at 18:58

2 Answers2

8

Got it working with:

  IOPMAssertionID assertionID; 
  IOPMAssertionDeclareUserActivity(CFSTR(""), kIOPMUserActiveLocal, &assertionID);
Lee Andrew
  • 798
  • 7
  • 28
3

Here is the answer in Swift 3.x:

var assertionID : IOPMAssertionID = 0
 _ = IOPMAssertionDeclareUserActivity("" as CFString, kIOPMUserActiveLocal, &assertionID)
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Tornado
  • 115
  • 6
  • 1
    thanks, - but for anyone else who's trying this - took me half an hour to discover you find the import you need (which is IOKit.pwr_mgt) to get the above function – Darren Oakey Aug 28 '21 at 01:20