diff --git a/lib/helpers/macos/applescript.js b/lib/helpers/macos/applescript.js index 3743566..7418b67 100644 --- a/lib/helpers/macos/applescript.js +++ b/lib/helpers/macos/applescript.js @@ -52,32 +52,25 @@ const INDENT = ' '; */ exports.renderScript = function (command) { validateCommand(command); - const renderedKeyCodeLines = command.keyCodes - .map(code => { - const renderedKeyCode = KeyCode[code].toString(); - const renderedModifiers = renderModifiers(command); - return ` -${INDENT}${INDENT}key code ${renderedKeyCode}${renderedModifiers}`; - }) - .join(''); + // Key presses must be scripted as a nested series of "key down"/"key up" + // commands rather than a linear sequence of "key code" commands because + // VoiceOver does not recognize key combinations using the latter API (e.g. + // `key code {123, 124}`). + const renderedKeyCodeLines = [...command.modifiers, ...command.keyCodes] + .reverse() + .map(code => KeyCode[code].toString()) + .reduce((accum, next) => `key down ${next}\n${accum}\nkey up ${next}`, '') + .split('\n') + .map(line => `${INDENT}${INDENT}${line}`) + .join('\n'); + const script = `with timeout of ${APPLESCRIPT_TIMEOUT} seconds -${INDENT}tell application "System Events"${renderedKeyCodeLines} +${INDENT}tell application "System Events" +${renderedKeyCodeLines} ${INDENT}end tell end timeout`; - return applescript(script); - /** - * @param {Applescript.AppleKeyCodeCommand} command - * @returns {string} - */ - function renderModifiers(command) { - if (command.modifiers.length > 1) { - return ` using {${command.modifiers.map(mod => `${Modifier[mod]} down`).join(', ')}}`; - } else if (command.modifiers.length === 1) { - return ` using ${Modifier[command.modifiers[0]]} down`; - } - return ''; - } + return applescript(script); }; /** diff --git a/test/helpers/macos/applescript.js b/test/helpers/macos/applescript.js index df475c9..cf7310b 100644 --- a/test/helpers/macos/applescript.js +++ b/test/helpers/macos/applescript.js @@ -54,7 +54,9 @@ suite('helpers/macos/applescript', () => { applescript( 'with timeout of 5 seconds\n' + ' tell application "System Events"\n' + - ' key code 49\n' + + ' key down 49\n' + + ' \n' + + ' key up 49\n' + ' end tell\n' + 'end timeout', ), @@ -67,7 +69,11 @@ suite('helpers/macos/applescript', () => { applescript( 'with timeout of 5 seconds\n' + ' tell application "System Events"\n' + - ' key code 0 using option down\n' + + ' key down 58\n' + + ' key down 0\n' + + ' \n' + + ' key up 0\n' + + ' key up 58\n' + ' end tell\n' + 'end timeout', ), @@ -80,7 +86,15 @@ suite('helpers/macos/applescript', () => { applescript( 'with timeout of 5 seconds\n' + ' tell application "System Events"\n' + - ' key code 11 using {command down, option down, shift down}\n' + + ' key down 55\n' + + ' key down 58\n' + + ' key down 57\n' + + ' key down 11\n' + + ' \n' + + ' key up 11\n' + + ' key up 57\n' + + ' key up 58\n' + + ' key up 55\n' + ' end tell\n' + 'end timeout', ),