macotron.camera
List the cameras, show a floating live preview, and take a still from that preview as a base64 PNG.
macotron.plugin({
title: "Camera",
description: "Preview the camera and copy a still.",
permissions: ["camera"],
});
macotron.command("Camera Preview", "Show a live camera preview", () => {
if (!macotron.camera.preview()) macotron.notify.toast("Could not start the camera");
});
macotron.command("Camera Snapshot", "Copy a preview still", () => {
const png = macotron.camera.snapshot();
if (png) macotron.clipboard.setImage(png);
else macotron.notify.toast("Start the preview first");
});
The camera needs Camera permission. Put "camera" in the permissions of macotron.plugin(). If macOS has not asked yet, the first preview() shows the prompt and returns false. Call it again after the user allows access.
Cameras
list(): Array<{ id, name }>
The video cameras macOS can see: built-in, Continuity Camera, Desk View, and external. id is the device's unique ID and name is the name macOS shows.
Preview
There is one preview at a time. Starting a new one closes the old one.
preview(opts?: { id?, width?, height? }): boolean
Opens a floating window with a live feed, titled with the camera's name. id comes from list(). Without it, or when no camera has that ID, the first camera is used. width and height are the window size in points: 640 by 480 by default, and at least 160 by 120.
Returns false when camera access is not granted, when there is no camera, or when the camera cannot start.
stopPreview(): void
Closes the preview window and stops the camera. Closing the window by hand does the same.
snapshot(): string | null
The newest frame from the running preview, as a base64 PNG at the camera's resolution. Returns null when no preview is running or no frame has arrived yet.
See also
- macotron.qr
.scan({ camera: true })to read a QR code with the camera. - macotron.clipboard
.setImage()to copy a snapshot. - The
record.jsplugin.