API Docs

A camera device attached to the system.

Obtain instances via the hs.camera module — do not instantiate directly.

Reading camera properties

const cam = hs.camera.all()[0]
console.log(cam.name + " uid=" + cam.uid + " inUse=" + cam.isInUse)

Watching for in-use state changes

const cam = hs.camera.all()[0]
const fn = (isInUse) => {
    console.log(cam.name + " is now " + (isInUse ? "in use" : "not in use"))
}
cam.on(fn)
// later…
cam.off(fn)

Capturing a still image

const cam = hs.camera.all()[0]
cam.captureImage()
    .then(img => img.saveToFile("/tmp/shot.png"))
    .catch(err => console.error("Capture failed: " + err))

Properties

typeName

string

The type name for JavaScript introspection. Always "HSCamera".

uid

string

The persistent unique identifier for this camera.

name

string

The human-readable name of this camera (e.g. "FaceTime HD Camera").

isInUse

boolean

Whether this camera is currently being used by any application.

Queries the underlying CoreMediaIO device state each time it is read.

Methods

on(listener) -> None

Register a listener that fires whenever this camera's in-use state changes.

The listener receives one argument: a boolean that is true when the camera starts being used and false when it is released. A camera only has one kind of event, so unlike the module-level watchers there is no event name to pass.

on(listener) -> None
Name Type Description
listener function A JavaScript function called with true when the camera starts being used and false when released
None
Throws an Error on failure; wrap calls in try/catch to handle it.
const cam = hs.camera.all()[0]
try {
    cam.on((inUse) => {
        console.log(cam.name + " is " + (inUse ? "now in use" : "no longer in use"))
    })
} catch (err) {
    console.error(err.message)
}

off(listener) -> None

Remove a previously registered per-camera in-use listener.

off(listener) -> None
Name Type Description
listener function The function originally passed to on(_:)
None
cam.off(myHandler)

once(listener) -> None

Register a listener that fires at most once, the next time this camera's in-use state changes.

once(listener) -> None
Name Type Description
listener function Called once, then automatically removed
None
Throws an Error on failure; wrap calls in try/catch to handle it.
try {
    cam.once((inUse) => console.log("First change:", inUse))
} catch (err) {
    console.error(err.message)
}

captureImage() -> Promise<HSImage>

Capture a still image from this camera.

Camera permission must be granted via hs.permissions.requestCamera() before calling this method. The returned HSImage can be saved, displayed in a UI element, or passed to other image-processing APIs.

captureImage() -> Promise<HSImage>
Promise<HSImage>
A Promise that resolves to an HSImage, or rejects on error
hs.camera.all()[0].captureImage()
    .then(img => img.saveToFile("/tmp/snapshot.png"))
    .catch(err => console.error("Capture failed: " + err))