noracu 0.1.3 → 0.1.4
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/CHANGELOG.md +74 -0
- package/README.md +9 -2
- package/README.zh-CN.md +9 -2
- package/docs/tools.md +5 -5
- package/docs/usage.md +21 -8
- package/imctl/README.md +4 -4
- package/imctl/src/imctl/cli.py +177 -11
- package/imctl/src/imctl/gestures.py +292 -71
- package/imctl/src/imctl/input.py +113 -10
- package/imctl/src/imctl/verify.py +39 -0
- package/imctl/src/imctl/vision.py +55 -4
- package/imctl/src/imctl/window.py +51 -6
- package/package.json +1 -1
- package/src/server.mjs +279 -64
package/imctl/src/imctl/input.py
CHANGED
|
@@ -375,10 +375,11 @@ class PendingRestore:
|
|
|
375
375
|
|
|
376
376
|
|
|
377
377
|
def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
378
|
-
chord=None, before_check=None, ocr_check=None,
|
|
378
|
+
chord=None, before_check=None, ocr_check=None, hud_check=None,
|
|
379
379
|
sleep=time.sleep, clock=time.monotonic,
|
|
380
380
|
timeout_s: float = 2.5, poll_s: float = 0.15,
|
|
381
|
-
min_hold_s: float = 0.3, pending: list | None = None
|
|
381
|
+
min_hold_s: float = 0.3, pending: list | None = None,
|
|
382
|
+
report: dict | None = None, restore: bool = True) -> bool:
|
|
382
383
|
"""Type text via clipboard paste (IME-safe), confirmed on screen.
|
|
383
384
|
|
|
384
385
|
Direct CGEvent keystroke synthesis breaks under CJK IMEs and is
|
|
@@ -394,7 +395,8 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
394
395
|
``landed`` — did the paste show up? This decides the return
|
|
395
396
|
value. The whole-window luma frame diff is the fast path; when
|
|
396
397
|
it never fires within ``timeout_s``, a single OCR probe is the
|
|
397
|
-
fallback. Either one confirms the paste
|
|
398
|
+
fallback. Either one confirms the paste (the frame diff not
|
|
399
|
+
while the phone shows its paste-progress box — F44 below).
|
|
398
400
|
|
|
399
401
|
``restore_ok`` — may the user's clipboard be restored? ONLY an
|
|
400
402
|
OCR hit grants this. The frame diff merely proves "the picture
|
|
@@ -442,6 +444,39 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
442
444
|
only when the caller has established the focus (e.g. guarded by
|
|
443
445
|
a preceding expect/verify step).
|
|
444
446
|
|
|
447
|
+
F41: for text of two or more characters the default OCR check used to
|
|
448
|
+
ask only "is the text on screen?" — so text that was already there (a
|
|
449
|
+
search-history chip with the same query, an older message) licensed a
|
|
450
|
+
restore at once, and a paste that the phone read late could then paste
|
|
451
|
+
the user's ORIGINAL clipboard. The default checks now count the text's
|
|
452
|
+
probe before the pasteboard write — the higher of two reads 0.3 s
|
|
453
|
+
apart, so a read that misses an occurrence cannot fake a rise — and
|
|
454
|
+
license a restore only when the count RISES over it, in two reads 0.3 s
|
|
455
|
+
apart. Without a pre-paste count (a read failed) OCR never licenses a
|
|
456
|
+
restore for such text, and the paste can only be confirmed by the
|
|
457
|
+
frame diff. ``report`` (a dict), when given, receives that pre-paste
|
|
458
|
+
count as ``count_before`` (None: unknown), for a caller that keeps
|
|
459
|
+
watching after a False result.
|
|
460
|
+
|
|
461
|
+
``restore=False``: once Cmd+V may be on its way the user's clipboard is
|
|
462
|
+
never put back — no restore here and no PendingRestore — and it keeps
|
|
463
|
+
``text``; only an error before Cmd+V still restores it. open_app uses
|
|
464
|
+
this (F42): a paste can reach the phone more than 10 s late, and
|
|
465
|
+
Spotlight can show the name in its own suggestions before that, so a
|
|
466
|
+
licensed restore could still paste the user's clipboard into the search.
|
|
467
|
+
|
|
468
|
+
F44: a paste the phone is slow to fetch puts up its "Pasting from <Mac>…"
|
|
469
|
+
progress box, and that box alone moves the frame diff. So when the frame
|
|
470
|
+
diff fired but OCR did not see the text, ``hud_check`` (one more screen
|
|
471
|
+
read) looks for the box: while it shows, the paste is still on its way —
|
|
472
|
+
not landed, the result is False and the caller's late watch takes over. A
|
|
473
|
+
capture failure there keeps the frame diff's verdict. ``hud_check`` is
|
|
474
|
+
injectable; by default it reads the screen only with the real frame diff
|
|
475
|
+
(an injected ``before_check`` gets no real read here). The default OCR
|
|
476
|
+
checks never license a restore from a read that shows the box — a paste,
|
|
477
|
+
this one or an earlier one, is still on its way and could fetch the
|
|
478
|
+
restored clipboard — and the box's own words never count as the text.
|
|
479
|
+
|
|
445
480
|
``pending`` (F33): when a one-character paste was not licensed for a
|
|
446
481
|
restore here — not seen within the checks, or landed while OCR stayed
|
|
447
482
|
blind — and the caller passes a list, a PendingRestore is appended to
|
|
@@ -474,6 +509,9 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
474
509
|
# too the clipboard keeps holding ``text``, as before.
|
|
475
510
|
single = verify.is_short_text(text, limit=1)
|
|
476
511
|
sweep_before = [None]
|
|
512
|
+
# F41: the probe count of a 2+ character text before the paste (None = unknown)
|
|
513
|
+
multi = not single and verify.text_probe(text) is not None
|
|
514
|
+
count_before = [None]
|
|
477
515
|
|
|
478
516
|
def sweep_read():
|
|
479
517
|
return vision.read_screen(language_correction=False, sweep=True)
|
|
@@ -481,15 +519,32 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
481
519
|
def defer_restore():
|
|
482
520
|
# F33: hand the caller what it needs to restore later, on evidence.
|
|
483
521
|
before = sweep_before[0]
|
|
484
|
-
if pending is None or not single or before is None:
|
|
522
|
+
if pending is None or not single or before is None or not restore:
|
|
485
523
|
return
|
|
486
524
|
|
|
487
|
-
|
|
488
|
-
|
|
525
|
+
sighting = verify.RepeatedSighting(text, before)
|
|
526
|
+
|
|
527
|
+
def confirm(hits):
|
|
528
|
+
# F44: as in ocr_check — a read showing the paste-progress box is
|
|
529
|
+
# no evidence (a paste is still on its way)
|
|
530
|
+
return not verify.shows_paste_hud(hits) and sighting.add(hits)
|
|
531
|
+
|
|
532
|
+
pending.append(PendingRestore(pasteboard, saved, saved_change, confirm,
|
|
489
533
|
sent_at=t0, clock=clock,
|
|
490
534
|
min_hold_s=min_hold_s))
|
|
491
535
|
|
|
492
536
|
pre_read = single and before_check is None
|
|
537
|
+
pre_count = multi and ocr_check is None
|
|
538
|
+
if hud_check is None:
|
|
539
|
+
if before_check is None:
|
|
540
|
+
def hud_check() -> bool:
|
|
541
|
+
# F44: one more look for the phone's paste-progress box (a
|
|
542
|
+
# sweep for a single character, like every read of it)
|
|
543
|
+
return verify.shows_paste_hud(sweep_read() if single else vision.read_screen())
|
|
544
|
+
else:
|
|
545
|
+
def hud_check() -> bool:
|
|
546
|
+
# an injected frame diff (tests) gets no real screen read here
|
|
547
|
+
return False
|
|
493
548
|
if before_check is None:
|
|
494
549
|
def before_check():
|
|
495
550
|
# Baseline luma is captured here, right before Cmd+V, so the
|
|
@@ -511,11 +566,30 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
511
566
|
evidence = verify.RepeatedSighting(text, sweep_before[0])
|
|
512
567
|
deadline = clock() + 4.0
|
|
513
568
|
while True:
|
|
514
|
-
|
|
569
|
+
hits = sweep_read()
|
|
570
|
+
# F44: a read showing the paste-progress box is no evidence —
|
|
571
|
+
# a paste is still on its way, and a restore now could hand it
|
|
572
|
+
# the user's clipboard
|
|
573
|
+
if not verify.shows_paste_hud(hits) and evidence.add(hits):
|
|
515
574
|
return True
|
|
516
575
|
if clock() >= deadline:
|
|
517
576
|
return False
|
|
518
577
|
sleep(0.3)
|
|
578
|
+
if multi:
|
|
579
|
+
# F41: only a rise over the pre-paste count is the paste, and it
|
|
580
|
+
# must show in two reads (one misread is no license to restore)
|
|
581
|
+
if count_before[0] is None:
|
|
582
|
+
return False
|
|
583
|
+
for i in range(2):
|
|
584
|
+
if i:
|
|
585
|
+
sleep(0.3)
|
|
586
|
+
hits = vision.read_screen()
|
|
587
|
+
# F44: no license while the paste-progress box shows — a paste
|
|
588
|
+
# (this one or an earlier one) is still on its way
|
|
589
|
+
if (verify.shows_paste_hud(hits)
|
|
590
|
+
or verify.probe_count(text, hits) <= count_before[0]):
|
|
591
|
+
return False
|
|
592
|
+
return True
|
|
519
593
|
return verify.screen_shows_text(text, vision.read_screen)
|
|
520
594
|
|
|
521
595
|
sent = [False] # True from the moment a Cmd+V may be on its way
|
|
@@ -540,6 +614,22 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
540
614
|
raise
|
|
541
615
|
sweep_before[0] = None
|
|
542
616
|
|
|
617
|
+
if pre_count:
|
|
618
|
+
# F41: count the text before the paste — the higher of two reads
|
|
619
|
+
# 0.3 s apart (a redraw or the select-all highlight can hide an
|
|
620
|
+
# occurrence from one read). A capture failure in either leaves it
|
|
621
|
+
# unknown: then OCR can never license a restore for this paste.
|
|
622
|
+
try:
|
|
623
|
+
first = verify.probe_count(text, vision.read_screen())
|
|
624
|
+
sleep(0.3)
|
|
625
|
+
count_before[0] = max(first, verify.probe_count(text, vision.read_screen()))
|
|
626
|
+
except Exception as exc:
|
|
627
|
+
if not verify.is_capture_error(exc):
|
|
628
|
+
raise
|
|
629
|
+
count_before[0] = None
|
|
630
|
+
if report is not None:
|
|
631
|
+
report["count_before"] = count_before[0]
|
|
632
|
+
|
|
543
633
|
saved = pasteboard.dataForType_("public.utf8-plain-text")
|
|
544
634
|
saved_change = pasteboard.changeCount()
|
|
545
635
|
|
|
@@ -594,6 +684,18 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
594
684
|
if not verify.is_capture_error(exc):
|
|
595
685
|
raise
|
|
596
686
|
restore_ok = False
|
|
687
|
+
if landed and not restore_ok:
|
|
688
|
+
# F44: the frame diff fired but OCR did not see the text. If the
|
|
689
|
+
# phone shows its "Pasting from …" progress box, the box was the
|
|
690
|
+
# change and the paste is still on its way: not landed. A capture
|
|
691
|
+
# failure tells nothing either way and keeps the frame diff's
|
|
692
|
+
# verdict; any other error propagates (after Cmd+V: no restore).
|
|
693
|
+
try:
|
|
694
|
+
if hud_check():
|
|
695
|
+
landed = False
|
|
696
|
+
except Exception as exc:
|
|
697
|
+
if not verify.is_capture_error(exc):
|
|
698
|
+
raise
|
|
597
699
|
if not landed:
|
|
598
700
|
landed = restore_ok
|
|
599
701
|
|
|
@@ -603,7 +705,7 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
603
705
|
defer_restore()
|
|
604
706
|
return False
|
|
605
707
|
|
|
606
|
-
if restore_ok:
|
|
708
|
+
if restore_ok and restore:
|
|
607
709
|
# Hold the paste long enough for the asynchronous
|
|
608
710
|
# pasteboard read before putting the user's data back.
|
|
609
711
|
elapsed = clock() - t0
|
|
@@ -615,8 +717,9 @@ def type_text(text: str, *, replace: bool = False, pasteboard=None,
|
|
|
615
717
|
if saved is not None:
|
|
616
718
|
pasteboard.setData_forType_(saved, "public.utf8-plain-text")
|
|
617
719
|
else:
|
|
618
|
-
# landed but OCR is blind to it (short/cropped text)
|
|
619
|
-
#
|
|
720
|
+
# landed but OCR is blind to it (short/cropped text), or the
|
|
721
|
+
# caller keeps the clipboard (restore=False): success, but the
|
|
722
|
+
# clipboard keeps `text` — see the docstring's tradeoff.
|
|
620
723
|
defer_restore()
|
|
621
724
|
return True
|
|
622
725
|
except BaseException:
|
|
@@ -179,6 +179,45 @@ def mean_abs_diff(a: bytes, b: bytes) -> float:
|
|
|
179
179
|
TYPE_MIN_DIFF = 0.5
|
|
180
180
|
|
|
181
181
|
|
|
182
|
+
def text_probe(text: str) -> str | None:
|
|
183
|
+
"""The OCR probe for ``text`` (screen_shows_text's rule: the first ≤12
|
|
184
|
+
characters after NFKC, lowercasing and whitespace removal), or None when
|
|
185
|
+
it is shorter than 2 characters — too short for OCR to confirm."""
|
|
186
|
+
probe = _compact(text)[:12]
|
|
187
|
+
return probe if len(probe) >= 2 else None
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def probe_count(text: str, hits) -> int | None:
|
|
191
|
+
"""How many times ``text``'s probe occurs in the joined OCR ``hits``;
|
|
192
|
+
None when the text is too short for a probe. The phone's "Pasting from …"
|
|
193
|
+
box is left out (F44): its words are never the typed text, even when the
|
|
194
|
+
probe is a piece of them ("from", "pro" in a Mac named "M5pro")."""
|
|
195
|
+
probe = text_probe(text)
|
|
196
|
+
if probe is None:
|
|
197
|
+
return None
|
|
198
|
+
return _compact("".join(getattr(hit, "text", hit) for hit in hits
|
|
199
|
+
if not _is_paste_hud(hit))).count(probe)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
# F44 (2026-11-05 real device, load ~25): when the phone is slow to fetch a paste
|
|
203
|
+
# it shows a "Pasting from <Mac name>…" progress box mid-screen. That box alone
|
|
204
|
+
# moves the whole-window frame diff past TYPE_MIN_DIFF while the text has not
|
|
205
|
+
# arrived, and type_text() reported such pastes as typed. English UI only: an
|
|
206
|
+
# iPhone in another language shows the box in that language, unrecognized here.
|
|
207
|
+
PASTE_HUD_PROBE = "pastingfrom"
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _is_paste_hud(hit) -> bool:
|
|
211
|
+
return PASTE_HUD_PROBE in _compact(getattr(hit, "text", hit))
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def shows_paste_hud(hits) -> bool:
|
|
215
|
+
"""True when one OCR hit reads like the phone's "Pasting from …" progress
|
|
216
|
+
box (compared like probes: NFKC, lowercase, whitespace removed). While it
|
|
217
|
+
shows, a paste is still on its way to the phone."""
|
|
218
|
+
return any(_is_paste_hud(hit) for hit in hits)
|
|
219
|
+
|
|
220
|
+
|
|
182
221
|
def screen_shows_text(text: str, read_screen) -> bool:
|
|
183
222
|
"""Report whether the OCR'd screen shows ``text`` (probe match).
|
|
184
223
|
|
|
@@ -13,8 +13,10 @@ from __future__ import annotations
|
|
|
13
13
|
|
|
14
14
|
import atexit
|
|
15
15
|
import os
|
|
16
|
+
import re
|
|
16
17
|
import shutil
|
|
17
18
|
import subprocess
|
|
19
|
+
import sys
|
|
18
20
|
import tempfile
|
|
19
21
|
import time
|
|
20
22
|
from dataclasses import dataclass
|
|
@@ -129,7 +131,56 @@ def capture_window(path: str | None = None) -> str:
|
|
|
129
131
|
raise CaptureError("could not capture the iPhone Mirroring window by id")
|
|
130
132
|
|
|
131
133
|
|
|
132
|
-
|
|
134
|
+
# F36 (2026-11-05, macOS 27.2): Vision weights the languages in the order given. With
|
|
135
|
+
# English first it returned no Chinese at all on a Spotlight frame (0 CJK hits; the
|
|
136
|
+
# Top Hit label 拼多多 was missing, so phone_open could not find the app and every
|
|
137
|
+
# Chinese `expect` failed). Chinese first read the same frame's Chinese AND its English
|
|
138
|
+
# labels (it even read "Suggestions" correctly where English-first read
|
|
139
|
+
# "Sugestions"), slightly faster. One shared default for ocr() and ocr_sweep().
|
|
140
|
+
# NORACU_OCR_LANGUAGES (comma-separated, e.g. "zh-Hant,en-US" or "en-US") overrides it.
|
|
141
|
+
DEFAULT_OCR_LANGUAGES: tuple[str, ...] = ("zh-Hans", "en-US")
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _ocr_languages(raw: str | None) -> tuple[str, ...]:
|
|
145
|
+
# ASCII and full-width commas, and the ideographic enumeration comma
|
|
146
|
+
langs = tuple(x.strip() for x in re.split(r"[,,、]", raw or "") if x.strip())
|
|
147
|
+
return langs or DEFAULT_OCR_LANGUAGES
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
OCR_LANGUAGES: tuple[str, ...] = _ocr_languages(os.environ.get("NORACU_OCR_LANGUAGES"))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
_SUPPORTED_LANGUAGES: set | None = None # read once per process
|
|
154
|
+
_WARNED_LANGUAGES: set = set()
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _supported_languages(request, languages):
|
|
158
|
+
"""``languages`` minus any Vision does not support (a typo in
|
|
159
|
+
NORACU_OCR_LANGUAGES must not break every OCR call); none left → the
|
|
160
|
+
defaults. Exact codes from Vision's list only (e.g. "en-US", not "en");
|
|
161
|
+
a dropped code is reported once on stderr. When the supported list cannot
|
|
162
|
+
be read, ``languages`` as given."""
|
|
163
|
+
global _SUPPORTED_LANGUAGES
|
|
164
|
+
if _SUPPORTED_LANGUAGES is None:
|
|
165
|
+
try:
|
|
166
|
+
res = request.supportedRecognitionLanguagesAndReturnError_(None)
|
|
167
|
+
found = res[0] if isinstance(res, tuple) else res
|
|
168
|
+
_SUPPORTED_LANGUAGES = {str(x) for x in (found or [])}
|
|
169
|
+
except Exception:
|
|
170
|
+
_SUPPORTED_LANGUAGES = set()
|
|
171
|
+
supported = _SUPPORTED_LANGUAGES
|
|
172
|
+
if not supported:
|
|
173
|
+
return tuple(languages)
|
|
174
|
+
kept = tuple(x for x in languages if x in supported)
|
|
175
|
+
for x in languages:
|
|
176
|
+
if x not in supported and x not in _WARNED_LANGUAGES:
|
|
177
|
+
_WARNED_LANGUAGES.add(x)
|
|
178
|
+
print(f"imctl: OCR language {x!r} is not supported by Vision here; skipped",
|
|
179
|
+
file=sys.stderr)
|
|
180
|
+
return kept or tuple(x for x in DEFAULT_OCR_LANGUAGES if x in supported) or tuple(languages)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def ocr(image_path: str, languages: tuple[str, ...] = OCR_LANGUAGES,
|
|
133
184
|
language_correction: bool = True) -> list[OCRHit]:
|
|
134
185
|
"""Run Vision OCR on an image; return hits sorted reading-order.
|
|
135
186
|
|
|
@@ -162,7 +213,7 @@ def ocr(image_path: str, languages: tuple[str, ...] = ("en-US", "zh-Hans"),
|
|
|
162
213
|
request = Vision.VNRecognizeTextRequest.alloc().init()
|
|
163
214
|
request.setRecognitionLevel_(Vision.VNRequestTextRecognitionLevelAccurate)
|
|
164
215
|
if hasattr(request, "setRecognitionLanguages_"):
|
|
165
|
-
request.setRecognitionLanguages_(list(languages))
|
|
216
|
+
request.setRecognitionLanguages_(list(_supported_languages(request, languages)))
|
|
166
217
|
request.setUsesLanguageCorrection_(language_correction)
|
|
167
218
|
|
|
168
219
|
handler = Vision.VNImageRequestHandler.alloc().initWithCGImage_options_(
|
|
@@ -222,7 +273,7 @@ def dedupe_hits(hits: list[OCRHit], eps: float = 0.02) -> list[OCRHit]:
|
|
|
222
273
|
SWEEP_MIN_TEXT_HEIGHTS = (0.018, 0.02, 0.022, 0.025, 0.03)
|
|
223
274
|
|
|
224
275
|
|
|
225
|
-
def ocr_sweep(image_path: str, languages: tuple[str, ...] =
|
|
276
|
+
def ocr_sweep(image_path: str, languages: tuple[str, ...] = OCR_LANGUAGES,
|
|
226
277
|
language_correction: bool = False,
|
|
227
278
|
heights: tuple[float, ...] = SWEEP_MIN_TEXT_HEIGHTS) -> list[OCRHit]:
|
|
228
279
|
"""Full-frame OCR once per minimumTextHeight in ``heights``; merged hits (F27).
|
|
@@ -250,7 +301,7 @@ def ocr_sweep(image_path: str, languages: tuple[str, ...] = ("en-US", "zh-Hans")
|
|
|
250
301
|
request = Vision.VNRecognizeTextRequest.alloc().init()
|
|
251
302
|
request.setRecognitionLevel_(Vision.VNRequestTextRecognitionLevelAccurate)
|
|
252
303
|
if hasattr(request, "setRecognitionLanguages_"):
|
|
253
|
-
request.setRecognitionLanguages_(list(languages))
|
|
304
|
+
request.setRecognitionLanguages_(list(_supported_languages(request, languages)))
|
|
254
305
|
request.setUsesLanguageCorrection_(language_correction)
|
|
255
306
|
request.setMinimumTextHeight_(min_height)
|
|
256
307
|
handler = Vision.VNImageRequestHandler.alloc().initWithCGImage_options_(
|
|
@@ -64,12 +64,12 @@ end tell
|
|
|
64
64
|
'''
|
|
65
65
|
|
|
66
66
|
|
|
67
|
-
def _run_osascript(script: str, *, strip: bool = True) -> str:
|
|
67
|
+
def _run_osascript(script: str, *, strip: bool = True, timeout: float = 10) -> str:
|
|
68
68
|
result = subprocess.run(
|
|
69
69
|
["osascript", "-e", script],
|
|
70
70
|
capture_output=True,
|
|
71
71
|
text=True,
|
|
72
|
-
timeout=
|
|
72
|
+
timeout=timeout,
|
|
73
73
|
)
|
|
74
74
|
if result.returncode != 0:
|
|
75
75
|
raise WindowError(f"osascript failed: {result.stderr.strip()}")
|
|
@@ -101,7 +101,7 @@ def ensure_running() -> None:
|
|
|
101
101
|
raise WindowError("iPhone Mirroring did not start within 10s")
|
|
102
102
|
|
|
103
103
|
|
|
104
|
-
def ensure_frontmost() -> None:
|
|
104
|
+
def ensure_frontmost(timeout: float = 10) -> None:
|
|
105
105
|
"""Bring the iPhone Mirroring window to the front.
|
|
106
106
|
|
|
107
107
|
CGEvents are delivered to the frontmost app; if another window
|
|
@@ -109,18 +109,19 @@ def ensure_frontmost() -> None:
|
|
|
109
109
|
F31: when the app had to be activated (possibly switching Spaces), wait
|
|
110
110
|
until the window yields a real frame before returning, so the caller's
|
|
111
111
|
next capture is not a transition frame refused as capture_failed.
|
|
112
|
+
``timeout`` bounds each osascript run (a tidy-up passes a short one).
|
|
112
113
|
"""
|
|
113
|
-
if _run_osascript(_FRONTMOST_SCRIPT) == "true":
|
|
114
|
+
if _run_osascript(_FRONTMOST_SCRIPT, timeout=timeout) == "true":
|
|
114
115
|
return
|
|
115
116
|
subprocess.run(
|
|
116
117
|
["osascript", "-e", f'tell application "{APP_NAME}" to activate'],
|
|
117
118
|
capture_output=True,
|
|
118
|
-
timeout=
|
|
119
|
+
timeout=timeout,
|
|
119
120
|
)
|
|
120
121
|
import time
|
|
121
122
|
|
|
122
123
|
time.sleep(0.4) # let the window server settle
|
|
123
|
-
if _run_osascript(_FRONTMOST_SCRIPT) != "true":
|
|
124
|
+
if _run_osascript(_FRONTMOST_SCRIPT, timeout=timeout) != "true":
|
|
124
125
|
raise WindowError("could not bring iPhone Mirroring to front")
|
|
125
126
|
_wait_capturable()
|
|
126
127
|
|
|
@@ -419,6 +420,46 @@ def get_overlay() -> dict:
|
|
|
419
420
|
return overlay_snapshot()[0]
|
|
420
421
|
|
|
421
422
|
|
|
423
|
+
# F37 (2026-11-05, macOS 27.2): the first launch after the macOS upgrade showed a welcome
|
|
424
|
+
# screen ("iPhone on Your Mac …") in a 640×660 window. Its button made it look like the paused
|
|
425
|
+
# overlay: phone_status reported paused=true, phone_launch told the user to lock the iPhone, and
|
|
426
|
+
# resume() clicks an overlay's lone button as if it were Resume. The mirror window itself — live
|
|
427
|
+
# or paused — is phone-shaped: elongated, portrait (measured 236×524, 329×727, 354×781 — about
|
|
428
|
+
# 2.2:1) or, for an app that rotates, landscape. A setup, welcome or permission screen is close to
|
|
429
|
+
# square, so a click is only ever sent on an elongated window. 1.7 keeps the 16:9 iPhone SE
|
|
430
|
+
# (≈1.78) in and a wide dialog (e.g. 800×520 ≈ 1.54) out. Geometry, not text: language-
|
|
431
|
+
# independent like the rest of the overlay probe.
|
|
432
|
+
PHONE_MIN_ASPECT = 1.7
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def is_phone_shaped(bounds: WindowBounds | None = None) -> bool | None:
|
|
436
|
+
"""True when the mirror window is elongated like a phone, portrait or landscape (F37).
|
|
437
|
+
|
|
438
|
+
The long side must be at least PHONE_MIN_ASPECT times the short side. None when the
|
|
439
|
+
bounds cannot be read.
|
|
440
|
+
"""
|
|
441
|
+
try:
|
|
442
|
+
b = bounds if bounds is not None else get_bounds()
|
|
443
|
+
except Exception:
|
|
444
|
+
return None
|
|
445
|
+
if b.width <= 0 or b.height <= 0:
|
|
446
|
+
return None # degenerate bounds say nothing about the shape
|
|
447
|
+
long_side, short_side = max(b.width, b.height), min(b.width, b.height)
|
|
448
|
+
return long_side >= PHONE_MIN_ASPECT * short_side
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def is_setup_screen(overlay: dict | None = None,
|
|
452
|
+
bounds: WindowBounds | None = None) -> bool:
|
|
453
|
+
"""True when an overlay is up in a window that is not phone-shaped (F37).
|
|
454
|
+
|
|
455
|
+
Such a screen (first-run welcome, setup, permission prompt) is the user's to
|
|
456
|
+
finish: resume() never clicks it and phone_launch says so. Unknown bounds → False.
|
|
457
|
+
"""
|
|
458
|
+
if overlay is None:
|
|
459
|
+
overlay = get_overlay()
|
|
460
|
+
return overlay["live"] is False and is_phone_shaped(bounds) is False
|
|
461
|
+
|
|
462
|
+
|
|
422
463
|
def is_paused(overlay: dict | None = None) -> bool | None:
|
|
423
464
|
"""True when the overlay shows a Resume control; False when live; None when unknown.
|
|
424
465
|
|
|
@@ -493,6 +534,10 @@ def resume() -> bool:
|
|
|
493
534
|
return True
|
|
494
535
|
if overlay["live"] is not False or overlay["buttons"] != 1:
|
|
495
536
|
return False
|
|
537
|
+
if is_phone_shaped() is not True:
|
|
538
|
+
# F37: a button in a window that is not phone-shaped belongs to a setup /
|
|
539
|
+
# welcome / permission screen — never ours to click. Unknown bounds: no click either.
|
|
540
|
+
return False
|
|
496
541
|
try:
|
|
497
542
|
clicked = _run_osascript(_CLICK_RESUME_SCRIPT)
|
|
498
543
|
except (WindowError, subprocess.TimeoutExpired):
|
package/package.json
CHANGED