Principle:AI QL Tuui Platform Automator Dispatch
| 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
- Implementation:AI_QL_Tuui_Automation_grabSelectedText
- Implementation:AI_QL_Tuui_Commander
- Implementation:AI_QL_Tuui_aid_createWindow