PreventSleep is a simple library to prevent a Mac from allowing a display to sleep, or from prevent idle sleep.
It differs from another common approach on Mac which is to handle a
import PreventSleep import IOKit.pwr_mgt let preventSleep = PreventSleep( sleepAssertionMsg: "Prevent idle sleep when playing audio.", sleepAssertionType: kIOPMAssertionTypeNoIdleSleep )! preventSleep.preventSleep() sleep(10) preventSlep.allowSleep()
To check that the power assertion was correctly created/removed, run
pmset -g assertions and look for your assertion.
github "jesse-c/PreventSleep" ~> VERSION_NUMBER
PreventSleep(sleepAssertionMsg: String, sleepAssertionType)
sleepAssertionMsg: String is the message that will be attached to the power assertion.
sleepAssertionType: String is either
kIOPMAssertionTypeNoDisplaySleep which are available after importing
preventSleep() -> Bool
Indefinitely prevents the type of sleep specified. Returns true if a power assertion was create successfully, false otherwise.
allowSleep() -> Bool
Allow the Mac to go to sleep following the user's energy prefernces. Returns true if a power assertion was removed successfully, false otherwise.
Check current sleeping status
canSleep() -> Bool
Returns true if there is currently no power assertion preventing sleeping, false otherwise.