clipboard
clipboard
¶
Insert text into the frontmost app without destroying the clipboard.
The current paste path (tools/desktop_tools.py: pbcopy then Cmd+V via
osascript) overwrites whatever the user had copied and never puts it back. For
a dictation tool that fires dozens of times an hour, silently eating the
clipboard is a daily papercut.
This module saves the pasteboard, writes the dictated text, sends Cmd+V, then restores the previous contents. The save/restore logic is separated from the PyObjC calls so it can be unit-tested against a fake pasteboard; the real NSPasteboard and CGEvent calls are thin wrappers at the bottom.
Classes¶
PasteboardLike
¶
Bases: Protocol
The slice of NSPasteboard we depend on — lets tests inject a fake.
Functions:¶
snapshot
¶
snapshot(pb: PasteboardLike) -> List[tuple[str, Any]]
Capture every representation currently on the pasteboard.
All types are kept, not just text: the user may have copied an image or a file, and restoring only the plain-text flavour would still be data loss.
Source code in src/diapason/desktop/clipboard.py
restore
¶
restore(
pb: PasteboardLike, saved: List[tuple[str, Any]]
) -> None
Put a previously captured snapshot back on the pasteboard.
lire_texte
¶
lire_texte(
pb: Optional[PasteboardLike] = None,
) -> Optional[str]
Le texte du presse-papiers — None s'il n'en contient pas.
Lecture seule, rien n'est écrit ni effacé. Ajoutée pour le geste « qu'est-ce que j'ai copié ? » (24 août 2026) — jusqu'ici ce module ne savait qu'écrire.
Source code in src/diapason/desktop/clipboard.py
paste_text
¶
paste_text(
text: str,
*,
restore_delay_s: float = 0.15,
pasteboard: Optional[PasteboardLike] = None,
send_paste: Optional[Any] = None,
sleep: Optional[Any] = None,
) -> bool
Paste text into the frontmost app, then restore the clipboard.
Returns True on success. The pasteboard / send_paste / sleep
parameters exist for tests; production uses the live defaults.
The restore is delayed because Cmd+V is asynchronous — the receiving app reads the pasteboard on its own run loop, and clobbering it back too soon would paste stale data. This is the detail the naive pbcopy path never had.