Clipboard

Plain text on the general pasteboard, through NSPasteboard. Registered on macOS under the name clipboard, so a page reaches it in a bundle and under crust dev alike.

Commands

commandargumentsanswers
readnone{ ok: true, text }
write{ text }{ ok: true, written: true }
readImagenone{ ok: true, png } — PNG bytes, or null for an empty pasteboard
writeImage{ png } or PNG bytes{ ok: true, written: true }
const { text } = await window.crust.invoke("clipboard", "read");

text is "" when the pasteboard holds no text. A pasteboard that cannot be read at all is a different answer: the native half reports null, and the command answers not-implemented naming NSPasteboard. An empty clipboard and an unreadable one never collapse into the same value.

write

const result = await window.crust.invoke("clipboard", "write", { text: "notes.md" });
// { ok: true, written: true }

written: true means -setString:forType: itself returned yes, not that the adapter tried. A pasteboard that refuses comes back write-failed with the verdict in the message.

The empty string is a real write. Only a non-string text is refused, because refusing "" would make "clear the clipboard" impossible.

Images

readImage answers { ok: true, png }: PNG bytes, or null when the pasteboard holds no image. writeImage takes PNG bytes ({ png }, or the bare bytes on the binary route) and answers { ok: true, written: true } when the pasteboard took them. Bytes that are not PNG are refused bad-request, and a pasteboard that refuses the write is write-failed. An image is a second object graph in C — NSImage in, PNG bytes out — so "no image" is png: null, not an empty result that would read as a refusal. Without an adapter both commands answer not-implemented naming NSPasteboard, as the text commands do.

Failure codes

codewhen
bad-requestwrite got no text, or a text that is not a string
write-failedthe pasteboard refused the string
unknown-adapterthe adapter has no readText or writeText member
handler-errorthe adapter threw; the message is the throw's own
not-implementedno pasteboard here, or the read answered null

The typed failures arrive with a non-200 status mapped from code through the same table CrustError uses (bad-request and write-failed → 400, unknown-adapter → 501, handler-error → 500); the body is still { ok: false, code, message }, so branch on result.ok. not-implemented is the bridge's own error instead, carrying requires: "NSPasteboard".