focus_window
Bring any window to the foreground by specifying part of its title (case-insensitive substring matching).
Instructions
Bring a window matching the given title pattern (substring, case-insensitive per wmctrl) to the foreground.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| title_pattern | Yes |
Implementation Reference
- server.js:186-193 (handler)The handler function for the 'focus_window' tool. Uses wmctrl -a with the given title_pattern to bring a matching window to the foreground.
async function focusWindow(args) { const missing = requireBin('wmctrl'); if (missing) return errorResult(missing); if (!args.title_pattern) return errorResult('title_pattern is required'); const r = await run(BIN.wmctrl, ['-a', args.title_pattern]); if (r.code !== 0) return errorResult(`no window matched "${args.title_pattern}"`); return textResult({ matched: args.title_pattern, focused: true }); } - server.js:403-412 (schema)The tool definition/schema for 'focus_window' with the required title_pattern string parameter.
{ name: 'focus_window', description: 'Bring a window matching the given title pattern (substring, case-insensitive per wmctrl) to the foreground.', annotations: { title: 'Focus window', destructiveHint: true }, inputSchema: { type: 'object', properties: { title_pattern: { type: 'string' } }, required: ['title_pattern'], }, }, - server.js:553-556 (registration)Registration of focus_window handler in the HANDLERS map, mapping the tool name to the focusWindow function.
const HANDLERS = { screenshot, list_windows: listWindows, focus_window: focusWindow, - server.js:57-62 (helper)Helper function that checks whether the required binary (wmctrl) is available before executing focus_window.
function requireBin(name) { if (!BIN[name]) { return `Required system tool "${name}" is not installed. Install with: sudo apt install ${name === 'gnomeShot' ? 'gnome-screenshot' : name === 'xclip' ? 'xclip' : name === 'xdotool' ? 'xdotool' : 'wmctrl'}`; } return null; } - server.js:64-82 (helper)Helper function that spawns a child process (used to run wmctrl) and captures stdout/stderr.
function run(cmd, args, opts = {}) { return new Promise((resolve) => { const child = spawn(cmd, args, { stdio: ['pipe', 'pipe', 'pipe'], ...opts }); let out = Buffer.alloc(0); let err = Buffer.alloc(0); child.stdout.on('data', (d) => { out = Buffer.concat([out, d]); }); child.stderr.on('data', (d) => { err = Buffer.concat([err, d]); }); if (opts.stdin !== undefined) { child.stdin.end(opts.stdin); } else { child.stdin.end(); } child.on('error', (e) => resolve({ code: -1, stdout: '', stderr: e.message })); child.on('close', (code) => resolve({ code, stdout: out.toString('utf8'), stderr: err.toString('utf8'), })); });