Skip to content

Commit 6c6fa69

Browse files
committed
refactor(simulator-management): tidy keyboard shortcut helper
Use template literals in the AppleScript builders so the title predicate reads as a single expression rather than a chain of string concatenations, and route the JSON.parse error through toErrorMessage to match the rest of the codebase and stay safe for non-Error throws.
1 parent 7bceed1 commit 6c6fa69

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

src/mcp/tools/simulator-management/_keyboard_shortcut.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CommandExecutor } from '../../../utils/execution/index.ts';
22
import { log } from '../../../utils/logging/index.ts';
3+
import { toErrorMessage } from '../../../utils/errors.ts';
34

45
export type KeyboardShortcut = 'software-keyboard' | 'connect-hardware-keyboard';
56

@@ -27,17 +28,12 @@ function resolveDevice(list: SimctlList, simulatorId: string): SimctlDevice | un
2728

2829
function buildFocusScript(deviceName: string): string {
2930
const safeName = escapeAppleScriptStringLiteral(deviceName);
31+
const titlePredicate = `title is "${safeName}" or title starts with "${safeName} –" or title starts with "${safeName} -"`;
3032
return [
3133
'tell application "System Events"',
3234
' tell process "Simulator"',
3335
' set frontmost to true',
34-
' set matchingWindows to (every window whose (title is "' +
35-
safeName +
36-
'" or title starts with "' +
37-
safeName +
38-
' –" or title starts with "' +
39-
safeName +
40-
' -"))',
36+
` set matchingWindows to (every window whose (${titlePredicate}))`,
4137
' if (count of matchingWindows) is 0 then',
4238
' return "NO_WINDOW"',
4339
' end if',
@@ -54,7 +50,7 @@ function buildKeystrokeScript(shortcut: KeyboardShortcut): string {
5450
return [
5551
'tell application "System Events"',
5652
' tell process "Simulator"',
57-
' keystroke "k" using ' + modifiers,
53+
` keystroke "k" using ${modifiers}`,
5854
' end tell',
5955
'end tell',
6056
].join('\n');
@@ -85,7 +81,7 @@ export async function sendKeyboardShortcut(
8581
} catch (e) {
8682
return {
8783
success: false,
88-
error: `Failed to parse simulator list: ${(e as Error).message}`,
84+
error: `Failed to parse simulator list: ${toErrorMessage(e)}`,
8985
};
9086
}
9187

0 commit comments

Comments
 (0)