| 336 | argsSchema = ListWindowsArgs; |
| 337 | |
| 338 | async execute(args: z.infer<typeof ListWindowsArgs>, _ctx: ToolContext): Promise<ToolResult> { |
| 339 | if (!isMacos()) return macosOnly(this.name); |
| 340 | try { |
| 341 | const script = args.app |
| 342 | ? `tell application "System Events" to tell process "${args.app.replace(/"/g, '\\"')}" to return (name of every window)` |
| 343 | : `tell application "System Events" |
| 344 | set out to "" |
| 345 | repeat with p in (every process whose visible is true and background only is false) |
| 346 | set procName to name of p |
| 347 | try |
| 348 | set winNames to name of every window of p |
| 349 | set out to out & procName & ": " & (winNames as string) & linefeed |
| 350 | on error |
| 351 | set out to out & procName & ": (no windows)" & linefeed |
| 352 | end try |
| 353 | end repeat |
| 354 | return out |
| 355 | end tell`; |
| 356 | const { stdout } = await runCmd('osascript', ['-e', script], { timeoutMs: 10_000 }); |
| 357 | return { |
| 358 | content: args.app |
| 359 | ? `Windows of "${args.app}":\n${stdout.split(',').map(s => ' - ' + s.trim()).join('\n')}` |
| 360 | : stdout.trim(), |
| 361 | }; |
| 362 | } catch (e: any) { |
| 363 | return { content: `[COMPUTER_USE_ERROR] ${e?.message ?? e}`, isError: true }; |
| 364 | } |
| 365 | } |
| 366 | } |