noracu 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/mac-ax.js CHANGED
@@ -1452,7 +1452,8 @@ function opObserveAfter(req) {
1452
1452
  // selection), AXValue replaces the whole value. No key events, so no input method can re-segment it
1453
1453
  // and the clipboard is not touched. Verified by reading the value back (secure fields cannot be read).
1454
1454
  function opInsertText(req) {
1455
- let el, appEl, how = 'focused', label = 'the focused element';
1455
+ // appName: the app whose focused element is written (no target) — the server names it in its result
1456
+ let el, appEl, how = 'focused', label = 'the focused element', appName;
1456
1457
  if (req.target) {
1457
1458
  const r = resolveTarget(req.target, 3000);
1458
1459
  if (r.error) return fail(notDelivered(r.error));
@@ -1462,7 +1463,8 @@ function opInsertText(req) {
1462
1463
  if (!app) return fail(notDelivered({ code: 'APP_NOT_FOUND', message: req.app ? `no running app named "${req.app}"` : 'no frontmost app' }));
1463
1464
  appEl = appElement(app);
1464
1465
  const f = attr(appEl, 'AXFocusedUIElement');
1465
- if (!f || !f.el) return fail(notDelivered({ code: 'ACTION_NOT_SUPPORTED', message: `"${app.name}" reports no focused element`, details: { kind: 'no_focus' } }));
1466
+ appName = app.name;
1467
+ if (!f || !f.el) return fail(notDelivered({ code: 'ACTION_NOT_SUPPORTED', message: `"${app.name}" reports no focused element`, details: { kind: 'no_focus', app: appName } }));
1466
1468
  el = f.el; label = `the focused element of "${app.name}"`;
1467
1469
  }
1468
1470
  const v = readNode(el);
@@ -1473,7 +1475,7 @@ function opInsertText(req) {
1473
1475
  // text-like but read-only through accessibility (a terminal, a custom editor) vs. not text at all
1474
1476
  // (a button, a list, a sheet): the server only falls back to keystrokes for the first kind
1475
1477
  const textlike = TEXT_ROLES.has(roleKey(v)) || v.AXRole === 'AXWebArea' || attr(el, 'AXSelectedTextRange') !== null || attr(el, 'AXInsertionPointLineNumber') !== null;
1476
- return fail(notDelivered({ code: 'ACTION_NOT_SUPPORTED', message: `${label} (${v.AXRole}${v.AXSubrole ? '/' + v.AXSubrole : ''}) takes no text through accessibility${textlike ? '' : ' and is not a text input — focus a text field first'}`, details: { kind: 'not_text', role: v.AXRole, textlike } }));
1478
+ return fail(notDelivered({ code: 'ACTION_NOT_SUPPORTED', message: `${label} (${v.AXRole}${v.AXSubrole ? '/' + v.AXSubrole : ''}) takes no text through accessibility${textlike ? '' : ' and is not a text input — focus a text field first'}`, details: { kind: 'not_text', role: v.AXRole, textlike, app: appName } }));
1477
1479
  }
1478
1480
  const text = String(req.text);
1479
1481
  const secure = v.AXSubrole === 'AXSecureTextField';
@@ -1482,7 +1484,7 @@ function opInsertText(req) {
1482
1484
  const selBefore = attr(el, 'AXSelectedTextRange');
1483
1485
  const beforeSel = JSON.stringify(selBefore);
1484
1486
  if (req.replace && !secure && sameText(before, text)) {
1485
- return { ok: true, mechanism: 'semantic', via: 'AXValue', how, role: v.AXRole, delivery: 'not_delivered', retry: 'safe', reason: 'already', note: `${label} already shows exactly this text — nothing sent` };
1487
+ return { ok: true, mechanism: 'semantic', via: 'AXValue', how, role: v.AXRole, app: appName, delivery: 'not_delivered', retry: 'safe', reason: 'already', note: `${label} already shows exactly this text — nothing sent` };
1486
1488
  }
1487
1489
  // R36: appending through AXValue rewrites the whole value; when the value cannot be read, that would
1488
1490
  // replace what is there with the text — refuse instead
@@ -1497,7 +1499,7 @@ function opInsertText(req) {
1497
1499
  const deadline = now() + WRITE_POLL_MS;
1498
1500
  sleep(150);
1499
1501
  let after = attr(el, 'AXValue');
1500
- const base = { ok: true, mechanism: 'semantic', via, how, role: v.AXRole };
1502
+ const base = { ok: true, mechanism: 'semantic', via, how, role: v.AXRole, app: appName };
1501
1503
  if (secure || typeof after !== 'string') return Object.assign(base, { delivery: 'delivered_unverified', retry: 'unsafe', reason: secure ? 'secure_field' : 'value_unreadable' });
1502
1504
  // CP: the value must be exactly what the write should give — the replaced text, the text over the
1503
1505
  // selection that was there (AXSelectedText), or the old value plus the text (append)
@@ -2174,14 +2176,13 @@ function keyboardLayoutId() {
2174
2176
  return String(ObjC.castRefToObject($.TISGetInputSourceProperty(s, $.kTISPropertyInputSourceID)).js);
2175
2177
  } catch (e) { return null; }
2176
2178
  }
2177
- // Key code 9 is V on QWERTY, QWERTZ, AZERTY and Colemak layouts; a Dvorak layout puts K there, so ⌘ with
2178
- // it would be ⌘K. The "Dvorak - QWERTY ⌘" layouts keep ⌘ shortcuts on QWERTY and are fine.
2179
- function vKeyIsElsewhere(layout) { return typeof layout === 'string' && /dvorak/i.test(layout) && !/qwerty/i.test(layout); }
2180
2179
  function opPaste(req) {
2181
2180
  const text = String(req.text === undefined || req.text === null ? '' : req.text);
2182
2181
  if (!text) return invalid('text required');
2183
- const layout = keyboardLayoutId();
2184
- if (vKeyIsElsewhere(layout)) return fail(notDelivered({ code: 'ACTION_NOT_SUPPORTED', message: `the keyboard layout ${layout} does not have V where ⌘V is sent from (key code 9) — nothing was pasted; mac_type types without the clipboard`, details: { kind: 'keyboard_layout' } }));
2182
+ // ⌘V goes to the key that carries V in this layout (0.2.6: key code 9 is K on Dvorak, C on Workman — ⌘C would
2183
+ // copy over the text noracu put on the clipboard)
2184
+ const vKey = keyCodeForChar('v', KEY_FLAGS.cmd);
2185
+ if (vKey === null) return fail(notDelivered({ code: 'ACTION_NOT_SUPPORTED', message: `no key types "v" in the keyboard layout ${keyboardLayoutId() || '(unknown)'}, so ⌘V cannot be sent — nothing was pasted; mac_type types without the clipboard`, details: { kind: 'keyboard_layout' } }));
2185
2186
  const front = frontApp();
2186
2187
  if (!front) return fail(notDelivered({ code: 'FOCUS_UNKNOWN', message: 'could not read the frontmost app — nothing was pasted' }));
2187
2188
  let target = front;
@@ -2242,10 +2243,10 @@ function opPaste(req) {
2242
2243
  const restored = putBack(ours);
2243
2244
  return fail(notDelivered({ code: 'FOCUS_LOST', message: `the frontmost app changed to "${f2 ? f2.name : 'unknown'}" just before the paste — nothing was pasted (clipboard restored: ${restored})` }));
2244
2245
  }
2245
- // ⌘V is the V key itself (key code 9) with the ⌘ flag, posted here. cliclick's `kd:cmd t:v ku:cmd`
2246
+ // ⌘V is the V key itself (vKey, found above) with the ⌘ flag, posted here. cliclick's `kd:cmd t:v ku:cmd`
2246
2247
  // types text, not that key: on macOS 27.2 TextEdit got a plain "v", or nothing, instead of a paste
2247
2248
  // (E2 run 2026-11-07, T7).
2248
- const sent = postKey(KEY_V, [[MOD_KEYCODES.cmd, KEY_FLAGS.cmd]], 0);
2249
+ const sent = postKey(vKey, [[MOD_KEYCODES.cmd, KEY_FLAGS.cmd]], 0);
2249
2250
  releaseStuckModifiers(['cmd']);
2250
2251
  if (!sent) {
2251
2252
  // the V key was never posted (postKey creates both events before posting either)
@@ -2293,6 +2294,9 @@ function opClipboard(req) {
2293
2294
  const out = { ok: true, types: types.slice(0, 20), change_count: Number(pb.changeCount) };
2294
2295
  // a concealed mark on any item (types lists only the first item's) withholds the whole clipboard
2295
2296
  if (types.indexOf('org.nspasteboard.ConcealedType') >= 0 || pbItemTypes().indexOf('org.nspasteboard.ConcealedType') >= 0) { out.concealed = true; out.note = 'the clipboard holds an item marked concealed (e.g. a password manager copy); its content is not returned'; return out; }
2297
+ // review 0.2.6 #4: Universal Clipboard content lives on the other device until it is read — say where
2298
+ // it came from (mac_paste refuses it; reading it is what this tool is for)
2299
+ if (types.indexOf(PB_REMOTE) >= 0) { out.remote = true; out.note = 'Universal Clipboard content copied on another device: reading it makes macOS fetch it from that device'; }
2296
2300
  const fmt = req.format || 'auto';
2297
2301
  if (fmt === 'auto' || fmt === 'files') {
2298
2302
  const items = pb.pasteboardItems;
@@ -2585,7 +2589,22 @@ function opMenu(req) {
2585
2589
  // keypad flag, as hardware sends them. The server's focus guard runs before this op.
2586
2590
  const KEY_FLAGS = { cmd: 0x100000, shift: 0x20000, alt: 0x80000, ctrl: 0x40000, fn: 0x800000 };
2587
2591
  const MOD_KEYCODES = { cmd: 55, shift: 56, alt: 58, ctrl: 59 }; // left-hand keys; fn is a flag only (pressing Fn alone can switch input source)
2588
- const KEY_V = 9; // the V key position (ANSI); mac_paste's ⌘V — see vKeyIsElsewhere for layouts that move it
2592
+ // The key that types `ch` in the current keyboard layout while `flags` are held: AppKit is asked what each key of
2593
+ // the main block gives (charactersIgnoringModifiers of a created, never posted, key event), so ⌘V and ⌘C land on
2594
+ // whatever key carries V or C in this layout — QWERTY, AZERTY, Dvorak, Workman… (0.2.6, review: a fixed key code
2595
+ // 9 is K on Dvorak and C on Workman). With ⌘ in the flags, layouts that switch to QWERTY for ⌘ shortcuts answer
2596
+ // for that. null when no key types it. Checked on macOS 27.2 with the Pinyin layout: v → 9, a → 0, c → 8.
2597
+ function keyCodeForChar(ch, flags) {
2598
+ const src = $.CGEventSourceCreate($.kCGEventSourceStateHIDSystemState);
2599
+ for (let k = 0; k <= 50; k++) {
2600
+ const e = $.CGEventCreateKeyboardEvent(src, k, true);
2601
+ if (!e) continue;
2602
+ $.CGEventSetFlags(e, flags);
2603
+ const ns = $.NSEvent.eventWithCGEvent(e);
2604
+ if (ns && !ns.isNil() && ns.charactersIgnoringModifiers.js === ch) return k;
2605
+ }
2606
+ return null;
2607
+ }
2589
2608
  const FLAG_NUMPAD = 0x200000;
2590
2609
  const HELD_MASK = KEY_FLAGS.cmd | KEY_FLAGS.shift | KEY_FLAGS.alt | KEY_FLAGS.ctrl;
2591
2610
  const EV_FLAGS_CHANGED = 12;
@@ -2637,9 +2656,27 @@ function releaseStuckModifiers(names) {
2637
2656
  return names.filter(m => MOD_KEYCODES[m] && (left & KEY_FLAGS[m]));
2638
2657
  }
2639
2658
  function opKey(req) {
2640
- const code = req.code;
2659
+ let code = req.code;
2660
+ let mods = Array.isArray(req.mods) ? req.mods.slice() : [];
2661
+ if (typeof req.char === 'string') {
2662
+ // a chord on a character key (cmd-c, cmd-shift-v…): the key that carries that character in this layout
2663
+ if (!/^[\x21-\x7e]$/.test(req.char)) return fail(notDelivered({ code: 'INVALID_ARGS', message: 'key: char must be one printable ASCII character' }));
2664
+ // the key is looked up by its unshifted character (AppKit reports "V" for the V key with Shift held); an
2665
+ // uppercase letter means Shift; a character found only with Shift ("+" on a US keyboard) adds Shift
2666
+ let ch = req.char;
2667
+ if (/^[A-Z]$/.test(ch)) { ch = ch.toLowerCase(); if (mods.indexOf('shift') < 0) mods.push('shift'); }
2668
+ const base = mods.indexOf('cmd') >= 0 ? KEY_FLAGS.cmd : 0;
2669
+ code = keyCodeForChar(ch, base);
2670
+ if (code === null) {
2671
+ code = keyCodeForChar(ch, base | KEY_FLAGS.shift);
2672
+ if (code !== null && mods.indexOf('shift') < 0) mods.push('shift');
2673
+ }
2674
+ if (code === null) {
2675
+ const layout = keyboardLayoutId();
2676
+ return fail(notDelivered({ code: 'ACTION_NOT_SUPPORTED', message: `no key types "${req.char}" in the keyboard layout ${layout || '(unknown)'} — nothing was sent`, details: { kind: 'keyboard_layout' } }));
2677
+ }
2678
+ }
2641
2679
  if (!Number.isInteger(code) || code < 0 || code > 127) return fail(notDelivered({ code: 'INVALID_ARGS', message: 'key: code must be an integer 0-127' }));
2642
- const mods = Array.isArray(req.mods) ? req.mods : [];
2643
2680
  const modKeys = [];
2644
2681
  let extra = 0;
2645
2682
  for (const m of mods) {
@@ -2652,7 +2689,7 @@ function opKey(req) {
2652
2689
  const sent = postKey(code, modKeys, extra);
2653
2690
  const stuck = releaseStuckModifiers(mods.filter(m => m !== 'fn'));
2654
2691
  if (!sent) return fail(notDelivered({ code: 'ACTION_FAILED', message: 'key: could not create the keyboard events', details: stuck.length ? { modifiers_still_held: stuck } : undefined }));
2655
- const out = { ok: true, delivery: 'delivered_unverified', retry: 'unsafe', flags: modKeys.reduce((a, [, f]) => a | f, 0) | extra };
2692
+ const out = { ok: true, delivery: 'delivered_unverified', retry: 'unsafe', flags: modKeys.reduce((a, [, f]) => a | f, 0) | extra, code };
2656
2693
  if (stuck.length) out.note = `the system still reports ${stuck.join('+')} held — press and release it on the keyboard`;
2657
2694
  return out;
2658
2695
  }