Programmatically wake display on OSX
Asked Answered
B

2

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.

Breach answered 6/10, 2014 at 22:45 Comment(4)
Perhaps try exploiting the "wake on network activity" feature. I think it's more commonly known as "Wake on LAN".Doxy
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.Oozy
Try this? The first answer seems relevant.Artisan
Possible duplicate of How do I wake from display sleep in OSX 10.7.4?Objectivity
B
8

Got it working with:

  IOPMAssertionID assertionID; 
  IOPMAssertionDeclareUserActivity(CFSTR(""), kIOPMUserActiveLocal, &assertionID);
Breach answered 6/10, 2014 at 23:46 Comment(3)
You should accept your own answer so people can easily find the solution.Crotty
This code was working fine till OSX 10.10 but does not seem to work on OSX El Capitan. Do we have an alternative?Personal
I didn't know, haven't upgraded yet. Please post a solution here if you find oneBreach
U
3

Here is the answer in Swift 3.x:

var assertionID : IOPMAssertionID = 0
 _ = IOPMAssertionDeclareUserActivity("" as CFString, kIOPMUserActiveLocal, &assertionID)
Untwist answered 22/2, 2017 at 4:48 Comment(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 functionBruiser

© 2022 - 2024 — McMap. All rights reserved.