windows.ts
1 import { Application } from './types' 2 import { Configuration } from './config' 3 import { runVbs } from '@el3um4s/run-vbs' 4 import NutAutomator from './nut' 5 6 export default class extends NutAutomator { 7 config: Configuration 8 9 constructor() { 10 super() 11 this.setup() 12 } 13 14 async getForemostApp(): Promise<Application | null> { 15 console.warn('getForemostApp not implemented (expected)') 16 return null 17 } 18 19 async selectAll() { 20 const script = ` 21 Set WshShell = WScript.CreateObject("WScript.Shell") 22 WshShell.SendKeys "^a" 23 WScript.Sleep 200 24 ` 25 26 // run it 27 await runVbs({ vbs: script }) 28 } 29 30 async moveCaretBelow() { 31 const script = ` 32 Set WshShell = WScript.CreateObject("WScript.Shell") 33 WshShell.SendKeys "{DOWN}{ENTER}" 34 WScript.Sleep 200 35 ` 36 37 // run it 38 await runVbs({ vbs: script }) 39 } 40 41 async copySelectedText() { 42 try { 43 if (!(await this.setup())) throw new Error('nutjs not loaded') 44 await this.nut().keyboard.pressKey(this.commandKey(), this.nut().Key.C) 45 await this.nut().keyboard.releaseKey(this.commandKey(), this.nut().Key.C) 46 } catch { 47 // fallback to vbs 48 const script = ` 49 Set WshShell = WScript.CreateObject("WScript.Shell") 50 WshShell.SendKeys "^c" 51 WScript.Sleep 20 52 ` 53 54 // run it 55 await runVbs({ vbs: script }) 56 } 57 } 58 59 async pasteText() { 60 try { 61 // nut is faster but not always available 62 if (!(await this.setup())) throw new Error('nutjs not loaded') 63 await this.nut().keyboard.pressKey(this.commandKey(), this.nut().Key.V) 64 await this.nut().keyboard.releaseKey(this.commandKey(), this.nut().Key.V) 65 } catch { 66 // fallback to vbs 67 const script = ` 68 Set WshShell = WScript.CreateObject("WScript.Shell") 69 WshShell.SendKeys "^v" 70 WScript.Sleep 20 71 ` 72 73 // run it 74 await runVbs({ vbs: script }) 75 } 76 } 77 78 async deleteSelectedText() { 79 const script = ` 80 Set WshShell = WScript.CreateObject("WScript.Shell") 81 WshShell.SendKeys "{DELETE}" 82 WScript.Sleep 200 83 ` 84 85 // run it 86 await runVbs({ vbs: script }) 87 } 88 89 async activateApp(title: string) { 90 const script = ` 91 Set WshShell = WScript.CreateObject("WScript.Shell") 92 WshShell.AppActivate "${title}" 93 WScript.Sleep 200 94 ` 95 96 // run it 97 await runVbs({ vbs: script }) 98 } 99 }