#hs.caffeinate

Control system power states (sleeping, preventing sleep, screen locking, etc)

NOTE: Any sleep preventions will be removed when hs.reload() is called. A future version of the module will save/restore state across reloads.


#Submodules


#API Overview

Functions - API calls offered directly by the extension


#API Documentation

#Functions

Signaturehs.caffeinate.currentAssertions()
TypeFunction
DescriptionFetches information about processes which are currently asserting display/power sleep restrictions
Parameters
  • None
Returns
  • A table containing information about current power assertions, with process IDs (PID) as the keys, each of which may contain multiple assertions
NotesNone
ExamplesNone
Sourceextensions/caffeinate/libcaffeinate.m line 296

Signaturehs.caffeinate.declareUserActivity([id])
TypeFunction
DescriptionInforms the OS that the user performed some activity
Parameters
  • id - An option number containing the assertion ID returned by a previous call of this function
Returns
  • A number containing the ID of the assertion generated by this function
Notes
  • This is intended to simulate user activity, for example to prevent displays from sleeping, or to wake them up
  • It is not mandatory to re-use assertion IDs if you are calling this function multiple times, but it is recommended that you do so if the calls are related
ExamplesNone
Sourceextensions/caffeinate/libcaffeinate.m line 209

Signaturehs.caffeinate.fastUserSwitch()
TypeFunction
DescriptionShow the Fast User Switch screen (ie a login screen without logging out first)
Parameters
  • None
Returns
  • None
NotesNone
ExamplesNone
Sourceextensions/caffeinate/caffeinate.lua line 109

Signaturehs.caffeinate.get(sleepType) -> bool or nil
TypeFunction
DescriptionQueries whether a particular sleep type is being prevented
Parameters
  • sleepType - A string containing the type of sleep to inspect (see hs.caffeinate.set() for information about the possible values)
Returns
  • True if the specified type of sleep is being prevented, false if not. nil if sleepType was an invalid value
NotesNone
ExamplesNone
Sourceextensions/caffeinate/caffeinate.lua line 55

Signaturehs.caffeinate.lockScreen()
TypeFunction
DescriptionLocks the displays
Parameters
  • None
Returns
  • None
Notes
  • This function uses private Apple APIs and could therefore stop working in any given release of macOS without warning.
ExamplesNone
Sourceextensions/caffeinate/libcaffeinate.m line 237

Signaturehs.caffeinate.logOut()
TypeFunction
DescriptionRequest the system log out the current user
Parameters
  • None
Returns
  • None
NotesNone
ExamplesNone
Sourceextensions/caffeinate/caffeinate.lua line 135

Signaturehs.caffeinate.restartSystem()
TypeFunction
DescriptionRequest the system reboot
Parameters
  • None
Returns
  • None
NotesNone
ExamplesNone
Sourceextensions/caffeinate/caffeinate.lua line 148

Signaturehs.caffeinate.sessionProperties()
TypeFunction
DescriptionFetches information from the display server about the current session
Parameters
  • None
Returns
  • A table containing information about the current session, or nil if an error occurred
Notes
  • The keys in this dictionary will vary based on the current state of the system (e.g. local vs VNC login, screen locked vs unlocked).
ExamplesNone
Sourceextensions/caffeinate/libcaffeinate.m line 271

| | | | --------------------------------------------|-------------------------------------------------------------------------------------| | Signature | hs.caffeinate.set(sleepType, aValue, acAndBattery) | | Type | Function | | Description | Configures the sleep prevention settings | | Parameters |

  • sleepType - A string containing the type of sleep to be configured. The value should be one of: displayIdle - Controls whether the screen will be allowed to sleep (and also the system) if the user is idle. systemIdle - Controls whether the system will be allowed to sleep if the user is idle (display may still sleep). system - Controls whether the system will be allowed to sleep for any reason.
  • aValue - A boolean, true if the specified type of sleep should be prevented, false if it should be allowed
  • acAndBattery - A boolean, true if the sleep prevention should apply to both AC power and battery power, false if it should only apply to AC power.
| | Returns |
  • None
| | Notes |
  • These calls are not guaranteed to prevent the system sleep behaviours described above. The OS may override them if it feels it must (e.g. if your CPU temperature becomes dangerously high).
  • The acAndBattery argument only applies to the system sleep type.
  • You can toggle the acAndBattery state by calling hs.caffeinate.set() again and altering the acAndBattery value.
  • The acAndBattery option does not appear to work anymore - it is based on private API that is not allowed in macOS 10.15 when running with the Hardened Runtime (which Hammerspoon now uses).
| | Examples | None | | Source | extensions/caffeinate/caffeinate.lua line 11 |


Signaturehs.caffeinate.shutdownSystem()
TypeFunction
DescriptionRequest the system log out and power down
Parameters
  • None
Returns
  • None
NotesNone
ExamplesNone
Sourceextensions/caffeinate/caffeinate.lua line 161

Signaturehs.caffeinate.startScreensaver()
TypeFunction
DescriptionRequest the system start the screensaver (which may lock the screen if the OS is configured to do so)
Parameters
  • None
Returns
  • None
NotesNone
ExamplesNone
Sourceextensions/caffeinate/caffeinate.lua line 122

Signaturehs.caffeinate.systemSleep()
TypeFunction
DescriptionRequests the system to sleep immediately
Parameters
  • None
Returns
  • None
NotesNone
ExamplesNone
Sourceextensions/caffeinate/libcaffeinate.m line 193

Signaturehs.caffeinate.toggle(sleepType) -> bool or nil
TypeFunction
DescriptionToggles the current state of the specified type of sleep
Parameters
  • sleepType - A string containing the type of sleep to toggle (see hs.caffeinate.set() for information about the possible values)
Returns
  • True if the specified type of sleep is being prevented, false if not. nil if sleepType was an invalid value
Notes
  • If systemIdle is toggled to on, it will apply to AC only
ExamplesNone
Sourceextensions/caffeinate/caffeinate.lua line 82