4.4 KiB
name, description
| name | description |
|---|---|
| selection-capture | How Sprachmittler detects the double copy, handles the pasteboard, replaces a selection safely and requests permissions. Load before changing the watcher, clipboard snapshot, Accessibility insertion, Services or the panel trigger. |
Selection capture and replacement
Module: SprachmittlerCapture. Read docs/SELECTION_AND_CLIPBOARD.md for the full contract.
Principles
- Act only on an explicit user action: double copy, menu item, Services.
- No global key tap, no Input Monitoring, no Accessibility observers, no screen capture.
- Read the pasteboard string only after
changeCountchanges. - Never replace text without an explicit click on Replace.
- Verify the source app and selection before changing it.
- If uncertain, offer Copy.
Double copy
poll changeCount every 120 ms
changed → is it ours/transient? ignore and suppress 750 ms
→ read string, trim; empty? reset
→ first change: remember hash/text/time
→ second change within 500 ms with identical text? trigger
→ otherwise: remember this copy instead
- Window 500 ms (300–1000 configurable).
- Identical text after line-ending normalisation and trimming; do not collapse internal whitespace.
- At least one non-whitespace character.
- Length limit: 4000 Latin / 2000 dense script; error before any request.
- Keep at most two strings; clear after the session starts or the window expires.
- Log only changeCount deltas and a short hash at
privacy: .private.
Own writes
Every clipboard write by the app adds:
com.schihei.sprachmittler.ownorg.nspasteboard.TransientTypeorg.nspasteboard.ConcealedType
The watcher ignores those and suppresses all changes for 750 ms after a write. This prevents translating the translation and re-triggering after a Replace paste.
Clipboard snapshot
Used only for the paste fallback:
- Snapshot every item and type; force lazy data.
- Record
changeCount. - Write the translation with the markers.
- Post ⌘V to the target PID.
- Wait up to 300 ms.
- Restore only if
changeCountstill matches our write; otherwise leave the clipboard alone.
Document the limits: unresolvable lazy data, types with no data, a copy that lands after the restore window.
Replace
Order:
- AX direct set: verify the focused element's
kAXSelectedTextAttributeequals the source, then set it to the translation. No clipboard mutation. - Synthetic paste: verify the target app is frontmost, snapshot, write, post ⌘V to the PID, restore.
Abort with insertionTargetChanged if the source app or selected text changed. Never insert into secure input fields. Block Replace when validation has a blocking warning. On failure, keep the panel open and offer Copy.
Permissions
- Accessibility: only for Replace. Check
AXIsProcessTrusted(); prompt only from onboarding or an explicit Replace click. - Deep link:
x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility. - Never request Input Monitoring, Screen Recording, Camera, Microphone or Apple Events.
- Double copy, translation, Copy and Services work without Accessibility.
Services
NSServicesin Info.plist withNSMessage = translateSelection.AppDelegatesetsNSApp.servicesProvidertoServicesProviderinSprachmittlerCapture.- No polling, no Accessibility. The system provides the pasteboard.
- After first install, a logout/login or
/System/Library/CoreServices/pbs -updatemay be needed.
Manual testing
For each app, test:
- A plain paragraph.
- Several paragraphs.
- A URL and an e-mail.
- An empty selection (nothing should happen).
- Copy, then Replace; check ⌘Z and the previous clipboard (rich text or image).
- Double copy with two different texts quickly (no trigger).
- Double copy with the same text slowly (no trigger).
Fill the matrix in docs/SELECTION_AND_CLIPBOARD.md. An untested cell is not a claim.
Common mistakes to reject
- Adding a global key monitor "to be reliable". No; the pasteboard is enough.
- Triggering on any two copies without comparing text. No; false positives.
- Replacing automatically after translation. No; explicit Replace only.
- Restoring the clipboard unconditionally. No; check
changeCount. - Reading
kAXValueAttributeof the whole document. No; only the focused selection. - Logging the selected text or the app name. No.