Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AI QL Tuui Clipboard Save Restore

From Leeroopedia
Knowledge Sources
Domains Automation, Clipboard, UX
Last Updated 2026-10-24 20:38 GMT

Overview

Save the user's clipboard before using it as a transfer buffer for text automation, then restore it afterward to avoid clobbering their content.

Description

GUI automation often needs to extract the currently selected text from an application. The only cross-platform way to do this is to simulate Ctrl+C/Cmd+C and read the clipboard — but this destroys whatever the user had in their clipboard. The save/restore pattern reads the clipboard before the copy operation, clears it (to detect "nothing selected"), performs the copy, reads the selected text, then writes the original content back. A retry loop with write verification ensures the restore is reliable.

Usage

Apply this heuristic whenever your automation uses the clipboard as a transfer mechanism for text grabbing or pasting. Without it, the user's clipboard content is silently destroyed.

The Insight (Rule of Thumb)

  • Action: Save clipboard.readText() before any clipboard-writing automation; restore it after.
  • Value: Clear clipboard (writeText()) before copy to distinguish "nothing selected" from "selected text equals clipboard."
  • Trade-off: Brief window where clipboard contains non-user content; if the user copies during automation, their copy is lost.
  • Verification: After writing to clipboard, read back and verify the value matches; retry up to 1 second.

Reasoning

The clipboard is a shared system resource. Using it as a temporary transfer buffer is the only reliable cross-platform method for extracting selected text (no API exists for "get selected text" without simulating copy). The clear-before-copy step is essential: without it, you cannot tell whether the clipboard contains old content or newly-copied selected text. The write verification loop handles rare clipboard race conditions where the write doesn't immediately take effect.

Code Evidence

Clipboard save/restore in getSelectedText from `src/main/aid/automator.ts:46-63`:

async getSelectedText(): Promise<string> {
  try {
    const clipboardText = clipboard.readText()  // save
    clipboard.writeText('')                      // clear
    await this.automator.copySelectedText()      // simulate Ctrl+C
    const selectedText = clipboard.readText()    // read result
    clipboard.writeText(clipboardText)            // restore
    return selectedText
  } catch (error) {
    console.error(error)
    return null
  }
}

Write verification loop from `src/main/aid/automation.ts:95-116`:

static writeTextToClipboard = async (text: string): Promise<boolean> => {
  const start = new Date().getTime()
  while (true) {
    try {
      clipboard.writeText(text)
      if (clipboard.readText() === text) {
        return true  // verified
      }
    } catch { /* empty */ }
    if (new Date().getTime() - start > kWriteTextTimeout) {
      return false  // 1000ms timeout
    }
    await wait(100)
  }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment