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

Principle:AI QL Tuui Platform Automator Dispatch

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

Overview

A cross-platform dispatch pattern that abstracts OS-specific automation APIs (AppleScript, VBScript, nut-js) behind a unified automator interface for text grabbing, clipboard manipulation, and focus management.

Description

Platform Automator Dispatch solves the problem of performing GUI automation (text selection copying, focus switching, pasting) across macOS, Windows, and Linux, which have fundamentally different automation APIs. The pattern uses a facade class (Automator) that dispatches to platform-specific implementations at construction time: MacosAutomator (nut-js + AppleScript), WindowsAutomator (nut-js + VBScript), or NutAutomator (nut-js only for Linux). All implementations conform to the same AutomatorImpl interface (getForemostApp, copySelectedText, pasteText, selectAll, etc.). The facade handles clipboard save/restore around automation operations to avoid clobbering the user's clipboard.

Usage

Use this principle when building desktop applications that need to interact with the OS GUI (grab selected text, paste, switch focus) across multiple platforms without writing platform-specific code at call sites.

Theoretical Basis

The dispatch uses the Strategy pattern with platform detection:

class Automator (Facade):
  constructor():
    if process.platform == 'darwin':  impl = MacosAutomator()
    elif process.platform == 'win32':  impl = WindowsAutomator()
    else:                               impl = NutAutomator()

  getSelectedText():
    1. savedClipboard = clipboard.readText()
    2. clipboard.writeText('')
    3. impl.copySelectedText()  # platform-specific key simulation
    4. selectedText = clipboard.readText()
    5. clipboard.writeText(savedClipboard)  # restore
    6. return selectedText

Platform implementations use different mechanisms:

  • macOS: AppleScript for focus and window management; nut-js for keyboard simulation
  • Windows: VBScript for app activation; nut-js for keyboard simulation
  • Linux: nut-js for all operations

The clipboard save/restore wrapper ensures the user's clipboard content is preserved across the copy-command used to extract selected text.

Related Pages

Implemented By

Uses Heuristic

Page Connections

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