Repository navigation
Description
Context
The clipboard API on Scope covers three kinds of content, and each kind is read and written on its own. As of aadbe7e:
- The public surface is
SetClipboard(plain text),SetClipboardImage,ReadClipboard,HasClipboardText,ReadClipboardImage(always comes back as a PNG),HasClipboardImageandReadClipboardFiles(file: URLs) (core/services.go:101-172).docs/architecture/services.mdlists exactly these (docs/architecture/services.md:17-21). - The driver has five fixed read kinds and nothing else (internal/driver/services.go:391-409).
- macOS:
ItemSetClipboardcallsclearContentsand then writes onlypublic.utf8-plain-text(internal/platform/macos/macos.go:1439-1443).setClipboardImageclears and writes PNG plus TIFF. Reads check a fixed list of types throughavailableTypeFromArray:and never look atpasteboardItemsortypes(internal/platform/macos/services.go:27-89). - Windows:
setClipboardcallsEmptyClipboardand sets onlyCF_UNICODETEXT(internal/platform/windows/services.go:45-67).readClipboardchecks onlyCF_UNICODETEXT,CF_HDROP,CF_DIBand the registered PNG format (internal/platform/windows/system.go:76-100). - There is no way to list the types or formats on the clipboard, read one by name, write several at once, write file URLs, or tell whether the clipboard changed (
NSPasteboard.changeCount,GetClipboardSequenceNumber).
MyGo's Clipboard.Read() / Clipboard.Write(data) copy every representation and write them back, and ReadFormat reads one by name. The roadmap already ports other parts of MyGo's clipboard.go (the MathML row, docs/roadmap.md:139), but has no row for this.
Why it matters
Some desktop apps have to use the clipboard for a moment and then give the user's content back:
- A dictionary or translation app that looks up the selected text in another app. When Accessibility gives no selected text, the usual fallback is to save the clipboard, send Copy, read the text, and restore the clipboard. The app can do the Accessibility and keystroke parts itself, but the restore is NSGo's job, and NSGo can only put back plain text. If the user had rich text, HTML, files from Finder or Explorer, or an app-specific type on the clipboard, it is lost.
- Clipboard managers and "paste as" tools need to read every representation and write several together. Today
SetClipboardandSetClipboardImageeach clear the clipboard, so an app cannot even write text and an image in one item. - Without a change count, an app cannot tell whether a Copy it sent did anything. It has to compare text, which fails when the same text was copied twice.
Today an app has to bypass NSGo and drive NSPasteboard through purego and the Win32 clipboard itself. On Windows that also needs its own message-only window, because OpenClipboard(NULL) followed by EmptyClipboard makes SetClipboardData fail, and NSGo does not expose its HWND.
Minimal repro:
// Before this runs, the user copies a formatted paragraph in TextEdit or Word
// (RTF + HTML + plain text), or a file in Finder / Explorer.
cx.ReadClipboard(func(prev string) {
cx.SetClipboard("temporary")
// ... use the clipboard ...
cx.SetClipboard(prev)
// Paste in the original app: only plain text comes back. The RTF, HTML,
// file URLs and any custom types are gone, and if the user had copied
// only a file, prev was "" and the clipboard is now empty text.
})Proposal
Scope.ReadClipboardItems(done func(items []ClipboardItem, err error)), where eachClipboardItemholds its representations as(type string, data []byte)pairs: UTIs on macOS and iOS, format names on Windows (registered names, or the standardCF_*names for the predefined formats), MIME types on Linux and the web. Use the same size cap asMaxClipboardImageand fail withCodeQuotaExceededwhen it is exceeded.Scope.WriteClipboardItems(items []ClipboardItem), which clears the clipboard once and writes every representation (macOSwriteObjects:withNSPasteboardItem, Windows oneOpenClipboardwithSetClipboardDataper format, Linux the offered targets). Writing back whatReadClipboardItemsreturned restores the clipboard. An empty slice clears it.Scope.ClipboardChangeCount(done func(int64)), fromchangeCounton macOS/iOS andGetClipboardSequenceNumberon Windows, and a counter on platforms that only give change events.- Platforms that cannot do this (iOS paste notice rules, the web's async clipboard permissions, tvOS and watchOS) answer
ErrUnsupportedor their platform's refusal, as the image reads do. - Windows formats that are GDI handles rather than
HGLOBAL(CF_BITMAP,CF_ENHMETAFILE,CF_PALETTE) are converted (CF_DIB,CF_ENHMETAFILEbytes) or left out, and the docs say which. nsgotestmodels the items and the change count, so apps can test a save-and-restore without a real clipboard.
Acceptance
- On macOS and Windows, an app that calls
ReadClipboardItems, thenSetClipboard("x"), thenWriteClipboardItemswith what it read, leaves the clipboard as it was. Rich text from TextEdit or Word pastes with its formatting, and a file copied in Finder or Explorer pastes as the file. ClipboardChangeCountgoes up after another app copies, and also after the app's ownSetClipboard.docs/architecture/services.mdanddocs/public/*/services.mddescribe the new calls and what each platform keeps.- An
nsgotesttest covers the save, overwrite and restore round trip.
Activity
houko commentedon Nov 20, 2026
I audited #264 against the latest
origin/main. The public API still exposes separate text, image, and file clipboard operations only. There is no representation listing/read-write API, multi-format write, change count, or nsgotest model for preserving arbitrary clipboard data.A complete implementation requires a coordinated driver protocol and native format handling on macOS, Windows, Linux, and the web, including ownership and size limits for platform-specific clipboard handles. I did not submit a partial text/image extension that would still lose rich text, custom formats, or files during save-and-restore. The issue remains open for that cross-platform clipboard representation work.
Heyana commentedon Nov 21, 2026
Adding a clipboard-manager / history-watcher use case here as well, since it directly overlaps with
ClipboardChangeCountand reading clipboard formats in this issue:ClipboardChangeCountand only reads clipboard content when the count changes.org.nspasteboard.TransientType,org.nspasteboard.AutoGeneratedType,org.nspasteboard.ConcealedType,com.agilebits.onepassword.nspasteboard.orgUTIs; password managers and Windows Clipboard History look at registered format names (>= 0xC000), notably"Clipboard Viewer Ignore","ExcludeClipboardContentFromMonitorProcessing", and"CanIncludeInClipboardHistory"(where aDWORDvalue of0forbids keeping it, while a non-zeroDWORDexplicitly allows keeping it — so returning raw format names alone without either exposing the data or notingCanIncludeInClipboardHistory's value can misclassify allowed copies as private).Hope this helps shape the cross-platform format/representation design in #264!