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

Implementation:AI QL Tuui Automation grabSelectedText

From Leeroopedia
Knowledge Sources
Domains Automation, Clipboard, Text_Grabbing
Last Updated 2026-10-24 00:00 GMT

Overview

Concrete tool for polling-based text grabbing from the focused application via platform automators, with clipboard write verification, provided by the tuui Electron application.

Description

The Automation class provides two static methods. grabSelectedText(automator, timeout) waits 10ms for focus, then polls automator.getSelectedText() every 100ms until non-empty text is found or the timeout expires. writeTextToClipboard(text) writes to clipboard and verifies the write succeeded by reading back, retrying up to 1 second.

The Automator.getSelectedText() method saves the current clipboard, clears it, invokes automator.copySelectedText() (platform-specific keyboard shortcut simulation), reads the clipboard to get the selected text, and restores the original clipboard.

Usage

Import Automation when you need to grab the currently selected text from any application. Import Automator when you need a platform-dispatching facade for macOS, Windows, or Linux automation.

Code Reference

Source Location

  • Repository: tuui
  • Files: src/main/aid/automation.ts, src/main/aid/automator.ts

Signature

export default class Automation {
  static grabSelectedText: (
    automator: Automator,
    timeout?: number
  ) => Promise<string>

  static writeTextToClipboard: (text: string) => Promise<boolean>
}

export default class Automator {
  constructor()  // Dispatches to MacosAutomator, WindowsAutomator, or NutAutomator

  getForemostApp(): Promise<Application | null>
  getSelectedText(): Promise<string>
  copySelectedText(): Promise<void>  // via platform impl
  pasteText(): Promise<void>        // via platform impl
  selectAll(): Promise<void>
  moveCaretBelow(): Promise<void>
  deleteSelectedText(): Promise<void>
  pasteText(textToPaste: string): Promise<boolean>
}

Import

import Automation from './aid/automation'
import Automator from './aid/automator'

I/O Contract

Inputs

Name Type Required Description
automator Automator Yes (grabSelectedText) Platform-dispatching automator instance
timeout number No (default 1000) Polling timeout in ms
text string Yes (writeTextToClipboard) Text to write to clipboard

Outputs

Name Type Description
grabSelectedText Promise<string> Selected text from focused app; may be null or empty
writeTextToClipboard Promise<boolean> True if clipboard write verified
getSelectedText Promise<string> Text from clipboard after copy; null on error

Usage Examples

Grab Selected Text

import Automator from './aid/automator'
import Automation from './aid/automation'

const automator = new Automator()
const text = await Automation.grabSelectedText(automator, 1000)

if (text && text.trim()) {
  console.log('Grabbed:', text.slice(0, 50))
} else {
  console.log('No text selected')
}

Automator Platform Dispatch

import Automator from './aid/automator'

const automator = new Automator()
// On macOS → MacosAutomator (nut + AppleScript)
// On Windows → WindowsAutomator (nut + VBScript)
// On Linux → NutAutomator (nut-js)

const app = await automator.getForemostApp()
// app = { id: 'com.apple.Terminal', name: 'Terminal', window: 'Terminal' }

await automator.pasteText('Hello from tuui!')

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

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