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.
@@ -28,7 +28,7 @@ import sys
28
28
  import tempfile
29
29
  import time
30
30
 
31
- from . import gestures, input, verify, vision
31
+ from . import gestures, input, pointer, verify, vision
32
32
  from .vision import CaptureError
33
33
  import subprocess
34
34
 
@@ -324,22 +324,49 @@ def _cmd_type(args) -> int:
324
324
  if guard is not None:
325
325
  return guard
326
326
  report = {}
327
- if input.type_text(args.text, replace=args.replace, report=report):
327
+ try:
328
+ typed_ok = input.type_text(args.text, replace=args.replace, report=report)
329
+ except input.PasteFocusLost:
330
+ # 0.2.7: another app came to the front during the wait after the clipboard write;
331
+ # ⌘V was called off rather than pulling iPhone Mirroring back to the front
332
+ sent = ("the select-all (⌘A) for replace was sent, but nothing was pasted" if args.replace
333
+ else "nothing was sent")
334
+ print("focus_lost: another app came to the front before ⌘V, so iPhone Mirroring was not "
335
+ f"brought back and the paste was called off — {sent}; the clipboard was put back "
336
+ "unless something else had copied meanwhile", file=sys.stderr)
337
+ return 6
338
+ if typed_ok:
328
339
  if report.get("seen", True):
329
340
  if _late_paste_probe(args.text) is None:
330
341
  # 0.2.5 (review Y6): one character that OCR saw appear is no proof of where it went,
331
342
  # and (review R38) no license to put the clipboard back
332
343
  print(f"typed {len(args.text)} chars (one character: OCR saw it appear, which is no "
333
- "proof of where it landed — check the field; the clipboard still holds the "
334
- "typed text)")
344
+ "proof of where it landed — check the field, which may hold the phone's own older "
345
+ "clipboard instead; the clipboard still holds the typed text)")
335
346
  return 0
336
347
  print(f"typed {len(args.text)} chars")
337
348
  return 0
338
349
  if _late_paste_probe(args.text) is None:
339
350
  # one character: OCR cannot read it back, only the picture changed
340
351
  print(f"typed {len(args.text)} chars (the screen changed after the paste, but "
341
- "OCR cannot read this text back — check the field; the clipboard still "
342
- "holds the typed text)")
352
+ "OCR cannot read this text back — check the field, which may hold the phone's own "
353
+ "older clipboard instead; the clipboard still holds the typed text)")
354
+ return 0
355
+ # review 0.2.6 #7: the screen changed and OCR could not confirm the text for a reason
356
+ # known now — say that one, instead of watching for a late paste that would only
357
+ # report a rise it cannot attribute
358
+ cause = report.get("unverifiable")
359
+ if cause == "too_short":
360
+ print(f"typed {len(args.text)} chars (the screen changed after the paste, but a text "
361
+ "of fewer than four characters, spaces and case aside, is never confirmed by "
362
+ "OCR — check the field, which may hold the phone's own older clipboard instead; the "
363
+ "clipboard still holds the typed text)")
364
+ return 0
365
+ if cause == "strip_only":
366
+ print(f"typed {len(args.text)} chars (OCR saw the text appear only in the strip at the "
367
+ "top of the screen, where a field cannot be told from a notification banner — "
368
+ "check the field, which may hold the phone's own older clipboard instead; the "
369
+ "clipboard still holds the typed text)")
343
370
  return 0
344
371
  # device run 2026-11-06: only the picture changed (a selection highlight, a
345
372
  # results list redraw) and OCR never saw the text — watch for it like an
@@ -373,8 +400,11 @@ def _cmd_type(args) -> int:
373
400
  "(phone_ocr / phone_screenshot): if the field shows the text, it "
374
401
  "holds it")
375
402
  else:
376
- advice = ("it may still land: read the screen (phone_ocr / phone_screenshot) "
377
- "before typing again, or retype with replace=true")
403
+ # device run 2026-11-07/08: the phone can paste its own older clipboard when the Mac's
404
+ # new clipboard has not reached it yet — the field may hold other text, not nothing
405
+ advice = ("it may still land, or the phone may have pasted its own older clipboard "
406
+ "instead: read the screen (phone_ocr / phone_screenshot) before typing "
407
+ "again, or retype with replace=true")
378
408
  print(
379
409
  "type_not_confirmed: the paste did not show on screen within the 2.5s "
380
410
  f"confirmation window{watched} — {advice}; the clipboard still holds the "
@@ -550,9 +580,14 @@ def _open_and_report(args) -> int:
550
580
  f"{', '.join(repr(x) for x in gestures.TOP_HIT_LABELS)} — no app by that "
551
581
  f"name, or NORACU_TOPHIT_LABELS does not match your iPhone's label){stuck}")
552
582
  else:
553
- print(f"app not found: {args.app!r} (Spotlight showed no Top Hit — no app by that "
554
- "name, or the iPhone is not set to English: phone_open reads Spotlight's "
555
- f"\"Top Hit\" label; set NORACU_TOPHIT_LABELS to the label your iPhone shows){stuck}")
583
+ # 2026-11-08: the top-field Spotlight shows no Top Hit header at all; there the
584
+ # evidence is the field completing the name with "— Open"
585
+ print(f"app not found: {args.app!r} (Spotlight showed no Top Hit, and its search "
586
+ "field did not complete the name with \"— Open\" — no app by that name, or "
587
+ "the iPhone is not set to English: phone_open reads Spotlight's \"Top Hit\" "
588
+ "label and \"Open\" completion; NORACU_TOPHIT_LABELS sets the Top Hit label for "
589
+ "another language, the \"Open\" completion is read in English only)"
590
+ f"{stuck}")
556
591
  return 1
557
592
  if result == "deadline":
558
593
  print(f"open_not_confirmed: ran out of time opening {args.app!r} (the Mac may be "
@@ -570,10 +605,27 @@ def _open_and_report(args) -> int:
570
605
  "its Home Screen icon instead (phone_screenshot, then phone_tap)", file=sys.stderr)
571
606
  return 4
572
607
  if result == "not_typed":
573
- print(f"open_not_confirmed: the name {args.app!r} was pasted into Spotlight but never "
608
+ # 0.2.7: the paste may have put the phone's own older clipboard into the field
609
+ # instead of the name — never claim the name was pasted
610
+ print(f"open_not_confirmed: ⌘V was sent to Spotlight but the name {args.app!r} never "
574
611
  "showed up on the phone (a paste can take several seconds to reach the iPhone, "
575
- "or be lost; an iPhone not set to English also needs NORACU_TOPHIT_LABELS) — "
576
- f"nothing was tapped; {home_note}; retry phone_open", file=sys.stderr)
612
+ "or be lost, and the phone may have pasted its own older clipboard into the "
613
+ "search field instead; an iPhone not set to English also needs "
614
+ f"NORACU_TOPHIT_LABELS) — nothing was tapped; {home_note}; retry phone_open, "
615
+ "which clears the search field first", file=sys.stderr)
616
+ return 4
617
+ if result == "focus_lost":
618
+ # 0.2.7: the paste was called off because another app came to the front; no trip
619
+ # Home either — that would pull iPhone Mirroring forward again
620
+ print(f"focus_lost: another app came to the front before ⌘V, so iPhone Mirroring was not "
621
+ f"brought back and {args.app!r} was not pasted or tapped — Spotlight was opened and "
622
+ "cleared on the phone and may still be open; the clipboard was put back unless "
623
+ "something else had copied meanwhile; retry phone_open", file=sys.stderr)
624
+ return 6
625
+ if result == "clipboard_changed":
626
+ print(f"open_not_confirmed: another app or device changed the clipboard before ⌘V, so "
627
+ f"{args.app!r} was not pasted or tapped and the new clipboard was left as it is; "
628
+ f"{home_note}; retry phone_open", file=sys.stderr)
577
629
  return 4
578
630
  if result == "spotlight_unavailable":
579
631
  print("spotlight unavailable (could not reach the Home Screen, the menu command failed, or the screen did not change)")
@@ -759,8 +811,20 @@ def main(argv: list[str] | None = None) -> int:
759
811
  parser = build_parser()
760
812
  args = parser.parse_args(argv)
761
813
  _stdin_values(args)
814
+ pointer.remember() # 0.2.7: a gesture's pointer goes back where it was (see pointer.py)
815
+ try:
816
+ return _run(args)
817
+ finally:
818
+ pointer.restore()
819
+
820
+
821
+ def _run(args) -> int:
762
822
  try:
763
823
  return args.func(args)
824
+ except input.KeyboardLayoutMismatch as exc:
825
+ # review 0.2.6 #5: raised before any event was posted or the clipboard was written
826
+ print(f"keyboard_layout: {exc}", file=sys.stderr)
827
+ return 2
764
828
  except input.FocusLost:
765
829
  # F26: the frontmost check failed right before an event — either
766
830
  # nothing was sent at all, or (mid-scroll) nothing further was.
@@ -339,12 +339,13 @@ def open_spotlight_checked(settle_s=1.0, *, luma=None, sleep=time.sleep, opener=
339
339
  # a Top Hit card header near the top of the window (Spotlight's sits at ny ≈ 0.14; a
340
340
  # "Top Hit" line far down is no evidence), "none", or "unreadable"
341
341
  try:
342
- header = _tophit_header(read_screen())
342
+ hits = read_screen()
343
343
  except Exception as exc:
344
344
  if not verify.is_capture_error(exc):
345
345
  raise
346
346
  return "unreadable"
347
- return "card" if header is not None and header.ny < SPOTLIGHT_HEADER_MAX_NY else "none"
347
+ # 2026-11-08: or the top-field layout's field (a completion, or "Search or Ask")
348
+ return "card" if _spotlight_up(hits) else "none"
348
349
 
349
350
  before_home = card()
350
351
  if not home():
@@ -469,19 +470,36 @@ def choose_app_hit(hits: list, name: str, ambiguous: list | None = None) -> visi
469
470
  label inside the card → None: "not found" beats opening the
470
471
  wrong thing. Suggestion, website and Settings deep-link rows of
471
472
  the same name are never candidates.
473
+
474
+ 2026-11-08 (iOS 27 "Search or Ask"): Spotlight may put the search
475
+ field at the TOP and show no "Top Hit" header. Then the label band
476
+ under the field counts (more than TOP_FIELD_LABEL_MIN and at most
477
+ TOP_FIELD_CARD_SPAN below it: not the field line, not the Home Screen
478
+ showing through above the label, not the first section header) — but only when the field shows exactly ``name``
479
+ completed with "— Open" (OPEN_COMPLETION_ACTIONS): Spotlight's own
480
+ statement that its top result is something of that name that opens.
481
+ Any other completion, or none, → None.
472
482
  """
473
483
  want = _norm_label(name)
474
484
  header = _tophit_header(hits)
475
- if header is None:
476
- return None
485
+ if header is not None:
486
+ lo, hi = header.ny, header.ny + TOP_HIT_CARD_SPAN
487
+ else:
488
+ # 2026-11-08, top-field layout: no "Top Hit" header. Only when the field itself
489
+ # completes the exact name with "— Open" is the block right under it the top result;
490
+ # the field line (and any part of it OCR split off) is never a candidate.
491
+ field = _top_field_for(hits, name)
492
+ if field is None or field[1] not in OPEN_COMPLETION_ACTIONS:
493
+ return None
494
+ lo, hi = field[0] + TOP_FIELD_LABEL_MIN, field[0] + TOP_FIELD_CARD_SPAN
477
495
  found = []
478
496
  for hit in hits:
479
497
  if _norm_label(hit.text) != want:
480
498
  continue
481
499
  if hit.ny >= SEARCH_FIELD_NY:
482
500
  continue # the search field echoing the query
483
- if not header.ny < hit.ny <= header.ny + TOP_HIT_CARD_SPAN:
484
- continue # outside the Top Hit card (F35)
501
+ if not lo < hit.ny <= hi:
502
+ continue # outside the Top Hit card (F35) / the block under the top field
485
503
  found.append(hit)
486
504
  if not found:
487
505
  return None
@@ -609,6 +627,89 @@ def _field_echo(hits, name) -> bool:
609
627
  # the magnifier glyph may come back as "Q", "Q." or "Q:" in front of the text
610
628
  if _echo_matches(got, want) or _echo_matches(re.sub(r"^q[\s.,:;·•]*", "", got, count=1), want):
611
629
  return True
630
+ # 2026-11-08: the top-field layout echoes the query at the top, often with a completion
631
+ # ("Calculator — Open"); the query part alone is compared, with the same OCR-slip tolerance
632
+ return any(_echo_matches(query, want) for _, query, _a in _top_field_lines(hits))
633
+
634
+
635
+ # 2026-11-08 device run (iOS 27, the "Search or Ask" Spotlight): the search field can sit at the
636
+ # TOP of the window (ny ≈ 0.12). Typing an app's exact name then shows the query with an inline
637
+ # completion in the field — read as "Calculator -Open" — and the results start right under it with
638
+ # that app's icon and label (label at ny ≈ 0.28) under NO "Top Hit" header; the first section
639
+ # header ("Chrome", "Settings", …) follows at ny ≈ 0.32, and a Settings row of the same name lower
640
+ # down (ny ≈ 0.49). phone_open's every read failed there: no header, and the bottom-field echo
641
+ # never matched (0.2.6 failed the same way). The field's own completion is the evidence this layout
642
+ # offers: "<name> — Open" says Spotlight's top result is something named <name> that opens.
643
+ TOP_FIELD_MAX_NY = 0.16 # the top search field line (measured 0.12)
644
+ TOP_FIELD_LINE_EPS = 0.02 # hits this close to the field line are parts of it (OCR may split it)
645
+ # the top result's label band under the field: label measured at +0.16, the first section header at
646
+ # +0.20, and the dimmed Home Screen behind the panel shows through above the label (a widget's
647
+ # "SUNDAY" at +0.06). Only (field + TOP_FIELD_LABEL_MIN, field + TOP_FIELD_CARD_SPAN] counts, so
648
+ # neither that background text nor a section header that carries the app's name ("Chrome" over
649
+ # Chrome's own results) is ever a candidate.
650
+ TOP_FIELD_LABEL_MIN = 0.10
651
+ TOP_FIELD_CARD_SPAN = 0.18
652
+ OPEN_COMPLETION_ACTIONS = ("open",) # the completion word that marks something that opens (English)
653
+ # completion words that also show Spotlight is up (a previous query that was no app: "— Ask Siri")
654
+ SPOTLIGHT_UP_ACTIONS = OPEN_COMPLETION_ACTIONS + ("ask siri",)
655
+ # the dash needs whitespace on at least one side: OCR reads the completion as "Calculator -Open" or
656
+ # "zq7pdcan- Ask Siri" (both seen on the device); a hyphen inside a name ("Wi-Fi") has none
657
+ _COMPLETION = re.compile(r"^(?P<q>.*\S)(?:\s+[—–-]\s*|[—–-]\s+)(?P<a>\S.*)$")
658
+
659
+
660
+ def _top_field_lines(hits):
661
+ """(ny, query, action) for each line in the top search-field zone: the hits within
662
+ TOP_FIELD_LINE_EPS of each other joined left to right, split at a " — <action>" completion
663
+ (action None without one). Query and action are _norm_label'ed."""
664
+ top = sorted((h for h in hits if h.ny < TOP_FIELD_MAX_NY), key=lambda h: (h.ny, h.nx))
665
+ lines, used = [], set()
666
+ for i, h in enumerate(top):
667
+ if i in used:
668
+ continue
669
+ group = [j for j in range(i, len(top)) if j not in used and abs(top[j].ny - h.ny) <= TOP_FIELD_LINE_EPS]
670
+ used.update(group)
671
+ text = " ".join(top[j].text for j in sorted(group, key=lambda j: top[j].nx))
672
+ m = _COMPLETION.match(unicodedata.normalize("NFKC", text).strip())
673
+ if m:
674
+ lines.append((h.ny, _norm_label(m.group("q")), _norm_label(m.group("a"))))
675
+ else:
676
+ lines.append((h.ny, _norm_label(text), None))
677
+ return lines
678
+
679
+
680
+ def _top_field_for(hits, name):
681
+ """(ny, action) of the top search field when it shows ``name`` (exactly, after _norm_label),
682
+ with or without a completion; None otherwise."""
683
+ want = _norm_label(name)
684
+ for ny, query, action in _top_field_lines(hits):
685
+ if query == want:
686
+ return ny, action
687
+ return None
688
+
689
+
690
+ def _spotlight_up(hits, name=None) -> bool:
691
+ """Spotlight is on screen: a "Top Hit" header near the top, or (top-field layout) the empty
692
+ field's "Search or Ask", or a field line completed with a SPOTLIGHT_UP_ACTIONS word whose
693
+ query is exactly ``name`` — or, without a name (the F43 check), completed with "Open" AND
694
+ with that query's label in the label band under it. Any other line in that zone (the
695
+ notification strip: a banner can read "X - Y" or even "X - Open") is no evidence."""
696
+ header = _tophit_header(hits)
697
+ if header is not None and header.ny < SPOTLIGHT_HEADER_MAX_NY:
698
+ return True
699
+ want = None if name is None else _norm_label(name)
700
+ for ny, query, action in _top_field_lines(hits):
701
+ if query == "search or ask":
702
+ return True
703
+ if want is not None:
704
+ if action in SPOTLIGHT_UP_ACTIONS and query == want:
705
+ return True
706
+ elif action in OPEN_COMPLETION_ACTIONS and any(
707
+ _norm_label(h.text) == query and ny + TOP_FIELD_LABEL_MIN < h.ny <= ny + TOP_FIELD_CARD_SPAN
708
+ for h in hits):
709
+ # without a name to match (the F43 check after Home), an "Open" completion counts
710
+ # only with Spotlight's own structure under it — that query's label in the label band;
711
+ # a banner that happens to read "Calendar - Open" has none
712
+ return True
612
713
  return False
613
714
 
614
715
 
@@ -636,7 +737,7 @@ def home_via_menu(timeout: float = TIDY_OSASCRIPT_TIMEOUT_S) -> bool:
636
737
  return _run_view_menu_command("Home Screen", timeout=timeout)
637
738
 
638
739
 
639
- def _tap_and_confirm_open(nx, ny, *, luma, sleep, tap, read_screen):
740
+ def _tap_and_confirm_open(nx, ny, *, luma, sleep, tap, read_screen, name=None):
640
741
  """Tap (nx, ny) and confirm the app actually opened (F24 ②).
641
742
 
642
743
  One luma frame before the tap, one after a 1.0s settle, then one
@@ -654,8 +755,10 @@ def _tap_and_confirm_open(nx, ny, *, luma, sleep, tap, read_screen):
654
755
  sleep(1.0)
655
756
  after = luma()
656
757
  hits = read_screen()
758
+ # 2026-11-08: Spotlight still up in either layout (a Top Hit header near the top, or the top
759
+ # field with its completion / "Search or Ask") means the tap did not open anything
657
760
  opened = (verify.mean_abs_diff(before, after) >= OPEN_MIN_DIFF
658
- and _tophit_header(hits) is None)
761
+ and _tophit_header(hits) is None and not _spotlight_up(hits, name))
659
762
  return opened, hits
660
763
 
661
764
 
@@ -731,8 +834,14 @@ def open_app(name: str, *, spotlight_open: bool = False,
731
834
  "deadline" (the OPEN_DEADLINE_S budget ran out before a tap; back on
732
835
  the Home Screen); "ambiguous" (Y6: the Top Hit card showed the name at
733
836
  two or more spots and no read showed it at one — nothing was tapped;
734
- back on the Home Screen); or "spotlight_unavailable" (Spotlight could
735
- not be opened; nothing was typed or tapped).
837
+ back on the Home Screen); "focus_lost" (another app came to the front
838
+ during type_text's wait after the clipboard write: the paste was called
839
+ off and nothing was tapped; Spotlight is left as it is — going Home
840
+ would pull the mirror forward again); "clipboard_changed" (another app or
841
+ device wrote the clipboard before ⌘V: nothing was pasted or tapped, the
842
+ new clipboard is left alone; back on the Home Screen); or
843
+ "spotlight_unavailable" (Spotlight could not be opened; nothing was
844
+ typed or tapped).
736
845
 
737
846
  ``read_screen`` (default vision.read_screen), ``luma`` (default
738
847
  verify.window_luma), ``sleep``, ``tap`` (default inp.tap),
@@ -809,7 +918,21 @@ def open_app(name: str, *, spotlight_open: bool = False,
809
918
  return "deadline"
810
919
  clear_text() # iOS kept the last query; new input would append (F17)
811
920
  sleep(0.3)
812
- typed = type_text(name)
921
+ try:
922
+ typed = type_text(name)
923
+ except inp.PasteFocusLost:
924
+ # 0.2.7: another app came to the front during the wait after the clipboard write;
925
+ # the paste was called off. No trip Home: that would pull the mirror forward again, the
926
+ # very thing the paste refused to do (Spotlight closes by itself when left idle).
927
+ return "focus_lost"
928
+ except inp.ClipboardChanged:
929
+ # another app or device wrote the clipboard before ⌘V: nothing was pasted and their
930
+ # clipboard is left alone. Tidy up only while the mirror is still in front — if the
931
+ # user also moved away, going Home would pull it forward: report focus_lost instead
932
+ if not inp._focus_check():
933
+ return "focus_lost"
934
+ tidy()
935
+ return "clipboard_changed"
813
936
  sleep(1.5) # wait for Spotlight results
814
937
  hit = None
815
938
  if typed is not False:
@@ -894,7 +1017,7 @@ def open_app(name: str, *, spotlight_open: bool = False,
894
1017
  # so ③ can find the label again (F32/F35).
895
1018
  opened, hits = _tap_and_confirm_open(
896
1019
  hit.nx, hit.ny, luma=luma, sleep=sleep, tap=tap,
897
- read_screen=reread)
1020
+ read_screen=reread, name=name)
898
1021
  if opened:
899
1022
  return "opened"
900
1023
 
@@ -906,7 +1029,7 @@ def open_app(name: str, *, spotlight_open: bool = False,
906
1029
  if again is not None and not out_of_time():
907
1030
  opened, _ = _tap_and_confirm_open(
908
1031
  again.nx, again.ny - SPOTLIGHT_ICON_ABOVE_LABEL,
909
- luma=luma, sleep=sleep, tap=tap, read_screen=reread)
1032
+ luma=luma, sleep=sleep, tap=tap, read_screen=reread, name=name)
910
1033
  if opened:
911
1034
  return "opened"
912
1035
  except Exception as exc: