Caffeination
public class Caffeination
An instance of a sleep-prevention session.
-
A customization option for a
Caffeination.See moreNote
Any givenCaffeinationcan be passed only one of eachOpt. So, for instance, it is valid to pass a.timedand.processOpt; however, it is not possible to have two different.processOpts.Declaration
Swift
public enum Opt -
The expected location of the
caffeinateexecutable.Declaration
Swift
public static let caffeinatePath: URL -
Indicates whether the
caffeinateexecutable is in the correct location.Declaration
Swift
public static var caffeinateExists: Bool { get } -
If
true, will automatically set thecaffeinateprocess to terminate with this application’s termination (when the.processoption is not passed). Has no bearing on traps.Declaration
Swift
public var limitLifetime: Bool -
The function to be executed once the Caffeination terminates.
Declaration
Swift
public var terminationHandler: ((Caffeination) -> Void)? -
The options for the Caffeination.
Declaration
Swift
public var opts: [Opt] { get set } -
Whether a Caffeination is currently ongoing.
Declaration
Swift
public var isActive: Bool { get } -
Whether the Caffeination will be automatically terminated when the app is quit using the “Quit” menu or receives a termination signal. Setting this property will enable or disable this safety mechanism. Not to be confused
Declaration
Swift
public var interceptAppTermination: Bool { get set } -
Initializes a new Caffeination.
Declaration
Parameters
optsThe options with which to start the Caffeintaion.
safetyWhether to enable safety measures to ensure that no “zombie”
caffeinateprocesses can outlive the current application. Set totrueby default, which is recommended.terminationHandlerA handler that will be called when the Caffeination stops. Will be set to
nilif the parameter is not specified. -
Generates a closure that Caffeinates for the duration of its execution. Inherits the Caffeination object’s
opts, of which only.disk,.display,.idle, and.userwill be honored.Throws
CaffeinationError.alreadyActiveif a Caffeination is already active.Declaration
Swift
public func closure<Param, Ret>(_ sourceClosure: @escaping (_ parameter: Param) -> Ret) throws -> (Param) -> RetParameters
sourceClosureThe closure during which to Caffeinate.
parameterA parameter of any type.
Return Value
A closure that will create an active Caffeination for the duration of its synchronous execution.
-
Generates a Caffienated closure that prioritizes execution of the closure above Caffeination. Thus, a closure will be returned and be allowed to run even if
caffeinatecannot be started. Failures to startcaffeinatewill occur silently; manually useCaffeination.caffeinateExiststo check that the intended behavior will occur.Declaration
Swift
public func silentClosure<Param, Ret>(_ sourceClosure: @escaping (_ parameter: Param) -> Ret) -> (Param) -> RetParameters
sourceClosureThe closure for the duration of which the Caffeination will be active.
Return Value
A closure that will create an active Caffeination for the duration of its synchronous execution.
-
Starts preventing sleep (i.e., the Caffeination).
Throws
CaffeinationError.alreadyActiveif a Caffeination is already active,CaffeinationError.caffeinateNotFoundif the user does not have the caffeinate binary at the expected location, or any error generated byProcess.run()on macOS 10.13 or higher.Warning
This method could theoretically raise anNSInvalidArgumentExceptionif run under exceptional conditions on macOS < 10.13. While every attempt is made to eliminate potential causes of anNSInvalidArgumentException, it is still possible that such an exception—which cannot be caught in Swift—could be raised.Declaration
Swift
public func start() throws -
Starts preventing sleep (i.e., the Caffeination) with the specified options, overriding any that may have previously been set.
Throws
CaffeinationError.alreadyActiveif a Caffeination is already active,CaffeinationError.caffeinateNotFoundif the user does not have the caffeinate binary at the expected location, or any error generated byProcess.run()on macOS 10.13 or higher.Warning
This method could theoretically raise anNSInvalidArgumentExceptionif run under exceptional conditions on macOS < 10.13. While every attempt is made to eliminate potential causes of anNSInvalidArgumentException, it is still possible that such an exception—which cannot be caught in Swift—could be raised.Declaration
Swift
public func start(withOpts opts: [Opt]) throwsParameters
optsThe options with which to start the Caffeination (overrides the existing value of
self.opts). -
Stops the Caffeination if it is active.
Declaration
Swift
public func stop() -
Sets the verbosity level of logging.
Declaration
Swift
public static func setLogLevel(_ level: LogLevel)Parameters
levelA
LogLevelspecifying the types of logs to receive. Levels are cumulative (i.e., setting the log level to.infowill also print warnings and errors). Pass.noneto disable logging.
View on GitHub
Install in Dash
Caffeination Class Reference