noracu 0.1.3 → 0.2.0
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 +483 -0
- package/README.md +40 -21
- package/README.zh-CN.md +42 -23
- package/SECURITY.md +15 -8
- package/docs/architecture.md +58 -27
- package/docs/tools.md +46 -35
- package/docs/usage.md +195 -66
- package/imctl/README.md +19 -16
- package/imctl/pyproject.toml +1 -1
- package/imctl/src/imctl/__init__.py +1 -1
- package/imctl/src/imctl/cli.py +315 -35
- package/imctl/src/imctl/gestures.py +313 -96
- package/imctl/src/imctl/input.py +300 -60
- package/imctl/src/imctl/verify.py +39 -0
- package/imctl/src/imctl/vision.py +55 -4
- package/imctl/src/imctl/window.py +61 -9
- package/package.json +5 -4
- package/src/mac-ax.js +2397 -0
- package/src/server.mjs +1887 -510
|
@@ -12,6 +12,8 @@ synthesized touch gestures — see the menu-command section below.
|
|
|
12
12
|
from __future__ import annotations
|
|
13
13
|
|
|
14
14
|
import math
|
|
15
|
+
import os
|
|
16
|
+
import re
|
|
15
17
|
import subprocess
|
|
16
18
|
import time
|
|
17
19
|
import unicodedata
|
|
@@ -38,34 +40,30 @@ def home() -> None:
|
|
|
38
40
|
|
|
39
41
|
|
|
40
42
|
def app_switcher() -> None:
|
|
41
|
-
"""Swipe up and hold:
|
|
43
|
+
"""Swipe up and hold: send the app-switcher gesture."""
|
|
44
|
+
ensure_frontmost()
|
|
42
45
|
b = get_bounds()
|
|
43
|
-
from Quartz import (
|
|
44
|
-
CGEventCreateMouseEvent, CGEventPost, CGEventSourceCreate,
|
|
45
|
-
kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGEventLeftMouseDragged,
|
|
46
|
-
kCGEventSourceStateHIDSystemState, kCGHIDEventTap, kCGMouseButtonLeft,
|
|
47
|
-
)
|
|
48
|
-
import time
|
|
49
|
-
|
|
50
46
|
x0, y0 = b.denormalize(0.5, 0.90)
|
|
51
47
|
x1, y1 = b.denormalize(0.5, 0.45)
|
|
52
|
-
src =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
48
|
+
src = inp._source()
|
|
49
|
+
inp._require_focus() # F26 / R13 §3: before mouse-down
|
|
50
|
+
cx, cy = x0, y0
|
|
51
|
+
inp._mouse_event(inp.kCGEventLeftMouseDown, cx, cy, src)
|
|
52
|
+
try:
|
|
53
|
+
time.sleep(0.02)
|
|
54
|
+
# Eased drag to mid-screen.
|
|
55
|
+
steps = 15
|
|
56
|
+
for i in range(1, steps + 1):
|
|
57
|
+
t = i / steps
|
|
58
|
+
e = t * t * (3 - 2 * t) # smoothstep
|
|
59
|
+
nx, ny = x0 + (x1 - x0) * e, y0 + (y1 - y0) * e
|
|
60
|
+
inp._mouse_event(inp.kCGEventLeftMouseDragged, nx, ny, src)
|
|
61
|
+
cx, cy = nx, ny
|
|
62
|
+
time.sleep(0.016)
|
|
63
|
+
inp._require_focus() # R18: before the hold, the mirror must still be in front
|
|
64
|
+
time.sleep(0.55) # hold to trigger the switcher
|
|
65
|
+
finally:
|
|
66
|
+
inp._mouse_event(inp.kCGEventLeftMouseUp, cx, cy, src)
|
|
69
67
|
|
|
70
68
|
|
|
71
69
|
# Wheel pixels per window point of list movement, measured on iPhone Mirroring
|
|
@@ -200,18 +198,18 @@ end run
|
|
|
200
198
|
'''
|
|
201
199
|
|
|
202
200
|
|
|
203
|
-
def _run_osascript(script: str, args: list[str] | None = None) -> str:
|
|
201
|
+
def _run_osascript(script: str, args: list[str] | None = None, timeout: float = 10) -> str:
|
|
204
202
|
"""Run a static AppleScript; runtime values go through argv only."""
|
|
205
203
|
cmd = ["osascript", "-e", script]
|
|
206
204
|
if args:
|
|
207
205
|
cmd += ["--"] + args
|
|
208
|
-
result = subprocess.run(cmd, capture_output=True, text=True, timeout=
|
|
206
|
+
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
|
|
209
207
|
if result.returncode != 0:
|
|
210
208
|
raise GestureError(f"osascript failed: {result.stderr.strip()}")
|
|
211
209
|
return result.stdout.strip()
|
|
212
210
|
|
|
213
211
|
|
|
214
|
-
def _view_menu_shortcut(item: str) -> tuple[str, int] | None:
|
|
212
|
+
def _view_menu_shortcut(item: str, timeout: float = 10) -> tuple[str, int] | None:
|
|
215
213
|
"""Read a View menu item's shortcut — read-only, never clicks.
|
|
216
214
|
|
|
217
215
|
Returns (cmdChar, cmdModifiers), or None when unreadable.
|
|
@@ -220,7 +218,7 @@ def _view_menu_shortcut(item: str) -> tuple[str, int] | None:
|
|
|
220
218
|
8 means no key equivalent at all.
|
|
221
219
|
"""
|
|
222
220
|
try:
|
|
223
|
-
raw = _run_osascript(_MENU_SHORTCUT_SCRIPT, [item])
|
|
221
|
+
raw = _run_osascript(_MENU_SHORTCUT_SCRIPT, [item], timeout=timeout)
|
|
224
222
|
except (GestureError, OSError, subprocess.TimeoutExpired):
|
|
225
223
|
return None
|
|
226
224
|
char, sep, mods = raw.partition(",")
|
|
@@ -232,27 +230,29 @@ def _view_menu_shortcut(item: str) -> tuple[str, int] | None:
|
|
|
232
230
|
return None
|
|
233
231
|
|
|
234
232
|
|
|
235
|
-
def _run_view_menu_command(item: str) -> bool:
|
|
233
|
+
def _run_view_menu_command(item: str, timeout: float = 10) -> bool:
|
|
236
234
|
"""Trigger a View menu command of iPhone Mirroring.
|
|
237
235
|
|
|
238
236
|
Primary: the item's shortcut as a real Command chord (works on any
|
|
239
237
|
UI language). Fallback: clicking the English menu item. Returns
|
|
240
238
|
False when neither path works (app not running, localized menus…).
|
|
239
|
+
``timeout`` bounds each osascript run it makes, including those that
|
|
240
|
+
bring the mirror to the front.
|
|
241
241
|
"""
|
|
242
|
-
shortcut = _view_menu_shortcut(item)
|
|
242
|
+
shortcut = _view_menu_shortcut(item, timeout=timeout)
|
|
243
243
|
if shortcut is not None:
|
|
244
244
|
char, mods = shortcut
|
|
245
245
|
keycode = inp.keycode_for_char(char) if mods == 0 else None
|
|
246
246
|
if keycode is not None:
|
|
247
247
|
try:
|
|
248
|
-
ensure_frontmost()
|
|
248
|
+
ensure_frontmost(timeout=timeout)
|
|
249
249
|
inp._cmd_chord(keycode)
|
|
250
250
|
return True
|
|
251
|
-
except (inp.InputError, WindowError):
|
|
251
|
+
except (inp.InputError, WindowError, OSError, subprocess.TimeoutExpired):
|
|
252
252
|
pass # fall through to clicking the menu item
|
|
253
253
|
try:
|
|
254
|
-
ensure_frontmost()
|
|
255
|
-
_run_osascript(_CLICK_MENU_ITEM_SCRIPT, [item])
|
|
254
|
+
ensure_frontmost(timeout=timeout)
|
|
255
|
+
_run_osascript(_CLICK_MENU_ITEM_SCRIPT, [item], timeout=timeout)
|
|
256
256
|
return True
|
|
257
257
|
except (GestureError, OSError, subprocess.TimeoutExpired, WindowError):
|
|
258
258
|
return False
|
|
@@ -275,7 +275,8 @@ def open_spotlight() -> bool:
|
|
|
275
275
|
SPOTLIGHT_MIN_DIFF = 6.0
|
|
276
276
|
|
|
277
277
|
|
|
278
|
-
def open_spotlight_checked(settle_s=1.0, *, luma=None, sleep=time.sleep, opener=None, home=None,
|
|
278
|
+
def open_spotlight_checked(settle_s=1.0, *, luma=None, sleep=time.sleep, opener=None, home=None,
|
|
279
|
+
home_settle_s=0.8, read_screen=None) -> bool:
|
|
279
280
|
"""Go to the Home Screen, open Spotlight, and confirm it (F17 guard).
|
|
280
281
|
|
|
281
282
|
Two layers of protection, in order:
|
|
@@ -296,19 +297,53 @@ def open_spotlight_checked(settle_s=1.0, *, luma=None, sleep=time.sleep, opener=
|
|
|
296
297
|
SPOTLIGHT_MIN_DIFF (calibrated for the single home-screen →
|
|
297
298
|
Spotlight transition).
|
|
298
299
|
|
|
300
|
+
③ F43 (2026-11-05 real-device, iOS 27): going Home from an app that
|
|
301
|
+
was opened from Spotlight lands back IN Spotlight, with the previous
|
|
302
|
+
query and its Top Hit card. The Spotlight command then closed it, and
|
|
303
|
+
that big change fooled layer ② into "opened" while the paste went to
|
|
304
|
+
the Home Screen (every phone_open right after a successful one
|
|
305
|
+
failed). When Home itself brought up a Top Hit card (none was on
|
|
306
|
+
screen before it), Spotlight is already open: it is reported as open
|
|
307
|
+
and no toggle is sent (the caller clears the old query first). A card
|
|
308
|
+
that was there before Home may be another app's (Safari shows one) with
|
|
309
|
+
Home ignored, so then — and when a read fails — layer ② decides. Known limit: a previous query
|
|
310
|
+
without a Top Hit card (no results) is not recognized, and the toggle
|
|
311
|
+
still closes that Spotlight.
|
|
312
|
+
|
|
299
313
|
``luma`` (default verify.window_luma), ``sleep``, ``opener``
|
|
300
|
-
(default open_spotlight)
|
|
301
|
-
|
|
314
|
+
(default open_spotlight), ``home`` (default: View ▸ Home Screen)
|
|
315
|
+
and ``read_screen`` (default vision.read_screen) are injectable so
|
|
316
|
+
tests can drive this without a phone.
|
|
302
317
|
"""
|
|
303
318
|
if luma is None:
|
|
304
319
|
luma = verify.window_luma
|
|
320
|
+
if read_screen is None:
|
|
321
|
+
read_screen = vision.read_screen
|
|
305
322
|
if opener is None:
|
|
306
323
|
opener = open_spotlight
|
|
307
324
|
if home is None:
|
|
308
325
|
home = lambda: _run_view_menu_command("Home Screen")
|
|
326
|
+
|
|
327
|
+
def card():
|
|
328
|
+
# a Top Hit card header near the top of the window (Spotlight's sits at ny ≈ 0.14; a
|
|
329
|
+
# "Top Hit" line far down is no evidence), "none", or "unreadable"
|
|
330
|
+
try:
|
|
331
|
+
header = _tophit_header(read_screen())
|
|
332
|
+
except Exception as exc:
|
|
333
|
+
if not verify.is_capture_error(exc):
|
|
334
|
+
raise
|
|
335
|
+
return "unreadable"
|
|
336
|
+
return "card" if header is not None and header.ny < SPOTLIGHT_HEADER_MAX_NY else "none"
|
|
337
|
+
|
|
338
|
+
before_home = card()
|
|
309
339
|
if not home():
|
|
310
340
|
return False
|
|
311
341
|
sleep(home_settle_s)
|
|
342
|
+
# F43: Home itself brought up a Top Hit card — Spotlight with the previous query: do not
|
|
343
|
+
# toggle it shut. A card that was already there before Home may be another app's own
|
|
344
|
+
# (Safari shows one too) with Home ignored; then, and when unsure, the gate below decides.
|
|
345
|
+
if before_home == "none" and card() == "card":
|
|
346
|
+
return True
|
|
312
347
|
before = luma()
|
|
313
348
|
if not opener():
|
|
314
349
|
return False
|
|
@@ -328,6 +363,16 @@ _LABEL_LOOKALIKES = {
|
|
|
328
363
|
}
|
|
329
364
|
|
|
330
365
|
|
|
366
|
+
# open_spotlight_checked() takes a Top Hit header only this high up the window as proof that
|
|
367
|
+
# Spotlight is open (the header measured at ny ≈ 0.14).
|
|
368
|
+
SPOTLIGHT_HEADER_MAX_NY = 0.3
|
|
369
|
+
|
|
370
|
+
# Spotlight's search field is at the bottom of the window (ny >= SEARCH_FIELD_NY) and echoes the
|
|
371
|
+
# typed query. choose_app_hit never takes a label there; open_app reads that echo as the proof
|
|
372
|
+
# that a late paste has reached the phone (F42).
|
|
373
|
+
SEARCH_FIELD_NY = 0.88
|
|
374
|
+
|
|
375
|
+
|
|
331
376
|
def _norm_label(s: str) -> str:
|
|
332
377
|
"""Normalize an app label (or query) for exact comparison.
|
|
333
378
|
|
|
@@ -359,20 +404,37 @@ def choose_app_hit(hits: list, name: str) -> vision.OCRHit | None:
|
|
|
359
404
|
forbidden: "X" must never hit "Xcode" or "X Corp". When several
|
|
360
405
|
rows match, the topmost (smallest ny) wins. Returns None when
|
|
361
406
|
nothing qualifies.
|
|
407
|
+
|
|
408
|
+
F35 (2026-11-05 real-device): an exact label is not an app. The
|
|
409
|
+
query "Pinduoduo" matched a Chrome *suggestion* row of that name
|
|
410
|
+
(the app's own Top Hit label reads 拼多多), the tap opened a web
|
|
411
|
+
search, and the open was reported as confirmed. Only a row INSIDE
|
|
412
|
+
the Top Hit card counts now: below the "Top Hit" header and at
|
|
413
|
+
most TOP_HIT_CARD_SPAN under it — the same geometry the icon
|
|
414
|
+
fallback in open_app() already relies on. No header, or no exact
|
|
415
|
+
label inside the card → None: "not found" beats opening the
|
|
416
|
+
wrong thing. Suggestion, website and Settings deep-link rows of
|
|
417
|
+
the same name are never candidates.
|
|
362
418
|
"""
|
|
363
419
|
want = _norm_label(name)
|
|
420
|
+
header = _tophit_header(hits)
|
|
421
|
+
if header is None:
|
|
422
|
+
return None
|
|
364
423
|
best = None
|
|
365
424
|
for hit in hits:
|
|
366
425
|
if _norm_label(hit.text) != want:
|
|
367
426
|
continue
|
|
368
|
-
if hit.ny >=
|
|
427
|
+
if hit.ny >= SEARCH_FIELD_NY:
|
|
369
428
|
continue # the search field echoing the query
|
|
429
|
+
if not header.ny < hit.ny <= header.ny + TOP_HIT_CARD_SPAN:
|
|
430
|
+
continue # outside the Top Hit card (F35)
|
|
370
431
|
if best is None or hit.ny < best.ny:
|
|
371
432
|
best = hit
|
|
372
433
|
return best
|
|
373
434
|
|
|
374
435
|
|
|
375
|
-
def find_app_hit(read_screen, name, attempts=2, delay_s=1.0, sleep=time.sleep
|
|
436
|
+
def find_app_hit(read_screen, name, attempts=2, delay_s=1.0, sleep=time.sleep,
|
|
437
|
+
deadline=None, clock=time.monotonic):
|
|
376
438
|
"""Read the screen up to ``attempts`` times for a Spotlight result row.
|
|
377
439
|
|
|
378
440
|
Each pass calls ``read_screen()`` for OCR hits and hands them to
|
|
@@ -388,26 +450,37 @@ def find_app_hit(read_screen, name, attempts=2, delay_s=1.0, sleep=time.sleep):
|
|
|
388
450
|
single-character labels, and they are recognized more reliably
|
|
389
451
|
without it. F27: Vision's full-frame pass can also skip such a label
|
|
390
452
|
entirely (the app "X" on 2026-11-04), so a short name that both
|
|
391
|
-
reads miss gets a third read
|
|
392
|
-
|
|
453
|
+
reads miss gets a third read that adds the minimumTextHeight sweep
|
|
454
|
+
to the default pass on the same frame —
|
|
455
|
+
``read_screen(language_correction=False, also_sweep=True)`` (F35:
|
|
456
|
+
the Top Hit header must be in the same read as the label). The match
|
|
393
457
|
stays exact either way. Long names keep the exact original
|
|
394
|
-
read/sleep sequence.
|
|
458
|
+
read/sleep sequence. ``deadline`` (a ``clock()`` value) is checked
|
|
459
|
+
before every read; once reached, no further read happens.
|
|
395
460
|
"""
|
|
396
461
|
short = len(_norm_label(name)) <= 2
|
|
462
|
+
|
|
463
|
+
def expired():
|
|
464
|
+
return deadline is not None and clock() >= deadline
|
|
465
|
+
|
|
466
|
+
reads = [{}]
|
|
467
|
+
if short:
|
|
468
|
+
# F35: the sweep finds the one-letter label, but choose_app_hit also
|
|
469
|
+
# needs the "Top Hit" header from the SAME frame — the last read uses
|
|
470
|
+
# both the default pass and the sweep (also_sweep), not the sweep alone.
|
|
471
|
+
reads += [{"language_correction": False},
|
|
472
|
+
{"language_correction": False, "also_sweep": True}]
|
|
397
473
|
for i in range(attempts):
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
if
|
|
404
|
-
return
|
|
405
|
-
hit = choose_app_hit(
|
|
406
|
-
read_screen(language_correction=False, sweep=True), name)
|
|
474
|
+
if i:
|
|
475
|
+
if expired():
|
|
476
|
+
return None
|
|
477
|
+
sleep(delay_s)
|
|
478
|
+
for kwargs in reads:
|
|
479
|
+
if expired():
|
|
480
|
+
return None # out of time before this read (open_app reports it)
|
|
481
|
+
hit = choose_app_hit(read_screen(**kwargs), name)
|
|
407
482
|
if hit is not None:
|
|
408
483
|
return hit
|
|
409
|
-
if i + 1 < attempts:
|
|
410
|
-
sleep(delay_s)
|
|
411
484
|
return None
|
|
412
485
|
|
|
413
486
|
|
|
@@ -416,24 +489,85 @@ def find_app_hit(read_screen, name, attempts=2, delay_s=1.0, sleep=time.sleep):
|
|
|
416
489
|
# 0.218,单位为窗口高度比例)。
|
|
417
490
|
SPOTLIGHT_ICON_ABOVE_LABEL = 0.052
|
|
418
491
|
|
|
492
|
+
# Top Hit 卡片的纵向范围:卡片内的行在 "Top Hit" 标题下方、且距标题不超过这个
|
|
493
|
+
# 窗高比例。2026-11-05(macOS 27.2 / iOS 27)实测:标题 0.136,卡片内标签 0.24,
|
|
494
|
+
# 下一个分区标题 "Suggestions" 0.294,第一条建议行 0.336 —— 0.15 落在卡片与
|
|
495
|
+
# 建议区之间。choose_app_hit()(F35)与 open_app() 的图标兜底共用这一个值。
|
|
496
|
+
TOP_HIT_CARD_SPAN = 0.15
|
|
497
|
+
|
|
419
498
|
# 待真机标定:打开 App 前后整窗 luma 帧差下限。
|
|
420
499
|
OPEN_MIN_DIFF = 12.0
|
|
421
500
|
|
|
501
|
+
# open_app's own time budget (seconds). The server stops `open` at 45 s from outside; a run
|
|
502
|
+
# that is killed there can leave Spotlight open with the name typed in. open_app checks this
|
|
503
|
+
# budget before every read pass and tap and, once it is spent, goes back to the Home Screen
|
|
504
|
+
# and reports "deadline" instead (F39).
|
|
505
|
+
OPEN_DEADLINE_S = 38.0
|
|
506
|
+
|
|
507
|
+
# F42 (2026-11-05 real-device): a paste reached the phone anywhere from 1.5 s to more than 10 s
|
|
508
|
+
# after Cmd+V — the text travels to the iPhone only when it pastes. When type_text did not see
|
|
509
|
+
# the paste arrive, open_app keeps reading Spotlight, one pass a second, for up to this long
|
|
510
|
+
# (within its budget). Once the search field shows the name, the query is in, and one more pass
|
|
511
|
+
# is enough — a Top Hit card alone may still be the previous query's.
|
|
512
|
+
OPEN_LATE_PASTE_S = 15.0 # a later device run under load saw ~15 s from Cmd+V to the echo
|
|
513
|
+
OPEN_LATE_PASTE_PASSES = 30 # a cap for injected clocks; the time limit normally ends it first
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
# The Spotlight "Top Hit" section label phone_open looks for. English by default;
|
|
517
|
+
# NORACU_TOPHIT_LABELS (comma-separated) supplies the label of an iPhone set to another
|
|
518
|
+
# language, exactly as the phone shows it (no unverified translations built in).
|
|
519
|
+
def _tophit_labels(raw):
|
|
520
|
+
# ASCII and full-width commas, and the ideographic enumeration comma
|
|
521
|
+
parts = re.split(r"[,,、]", raw or "")
|
|
522
|
+
labels = tuple(_norm_label(x) for x in parts if x.strip())
|
|
523
|
+
return labels or (_norm_label("Top Hit"),)
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
TOP_HIT_LABELS = _tophit_labels(os.environ.get("NORACU_TOPHIT_LABELS"))
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def _field_echo(hits, name) -> bool:
|
|
530
|
+
"""True when the search field shows exactly ``name`` (F42).
|
|
531
|
+
|
|
532
|
+
The field is the bottom zone (ny >= SEARCH_FIELD_NY); OCR may read the
|
|
533
|
+
magnifier glyph in front of the text as a "Q". Exact after _norm_label,
|
|
534
|
+
like every other label comparison here.
|
|
535
|
+
"""
|
|
536
|
+
want = _norm_label(name)
|
|
537
|
+
for hit in hits:
|
|
538
|
+
if hit.ny < SEARCH_FIELD_NY:
|
|
539
|
+
continue
|
|
540
|
+
got = _norm_label(hit.text)
|
|
541
|
+
# the magnifier glyph may come back as "Q", "Q." or "Q:" in front of the text
|
|
542
|
+
if got == want or re.sub(r"^q[\s.,:;·•]*", "", got, count=1) == want:
|
|
543
|
+
return True
|
|
544
|
+
return False
|
|
545
|
+
|
|
422
546
|
|
|
423
547
|
def _tophit_header(hits):
|
|
424
548
|
"""The OCR hit for the Spotlight "Top Hit" card header, or None.
|
|
425
549
|
|
|
426
550
|
Matched exactly after _norm_label (case-folding, trailing
|
|
427
|
-
punctuation), so a list row that merely
|
|
428
|
-
never pose as the card header.
|
|
551
|
+
punctuation) against TOP_HIT_LABELS, so a list row that merely
|
|
552
|
+
contains the words can never pose as the card header. Several
|
|
553
|
+
matches → the first in reading order (the topmost).
|
|
429
554
|
"""
|
|
430
|
-
want = _norm_label("Top Hit")
|
|
431
555
|
for hit in hits:
|
|
432
|
-
if _norm_label(hit.text)
|
|
556
|
+
if _norm_label(hit.text) in TOP_HIT_LABELS:
|
|
433
557
|
return hit
|
|
434
558
|
return None
|
|
435
559
|
|
|
436
560
|
|
|
561
|
+
# open_app's tidy-up trip to the Home Screen must fit inside the time the server leaves after
|
|
562
|
+
# its deadline (two 10 s osascript runs did not), so it uses shorter ones.
|
|
563
|
+
TIDY_OSASCRIPT_TIMEOUT_S = 4.0
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
def home_via_menu(timeout: float = TIDY_OSASCRIPT_TIMEOUT_S) -> bool:
|
|
567
|
+
"""View ▸ Home Screen in iPhone Mirroring (what open_app uses to tidy up)."""
|
|
568
|
+
return _run_view_menu_command("Home Screen", timeout=timeout)
|
|
569
|
+
|
|
570
|
+
|
|
437
571
|
def _tap_and_confirm_open(nx, ny, *, luma, sleep, tap, read_screen):
|
|
438
572
|
"""Tap (nx, ny) and confirm the app actually opened (F24 ②).
|
|
439
573
|
|
|
@@ -459,7 +593,9 @@ def _tap_and_confirm_open(nx, ny, *, luma, sleep, tap, read_screen):
|
|
|
459
593
|
|
|
460
594
|
def open_app(name: str, *, spotlight_open: bool = False,
|
|
461
595
|
read_screen=None, luma=None, sleep=time.sleep, tap=None,
|
|
462
|
-
home=None, clear_text=None, type_text=None
|
|
596
|
+
home=None, clear_text=None, type_text=None,
|
|
597
|
+
clock=time.monotonic, deadline_s: float | None = OPEN_DEADLINE_S,
|
|
598
|
+
report: dict | None = None) -> str:
|
|
463
599
|
"""Open an app by name: Spotlight → clear → type → OCR → tap → CONFIRM.
|
|
464
600
|
|
|
465
601
|
``spotlight_open`` lets a caller that already ran
|
|
@@ -480,7 +616,9 @@ def open_app(name: str, *, spotlight_open: bool = False,
|
|
|
480
616
|
keeps the previous Spotlight query, so the new name would be
|
|
481
617
|
appended to it and no result row would match (F17). Results are
|
|
482
618
|
then read up to twice (find_app_hit) because they can lag the
|
|
483
|
-
keystrokes
|
|
619
|
+
keystrokes — or, when type_text did not see the paste arrive, once a
|
|
620
|
+
second for up to OPEN_LATE_PASTE_S (F42): once the search field shows
|
|
621
|
+
the name, one more pass.
|
|
484
622
|
|
|
485
623
|
The top hit is never opened with Return — it may belong to a
|
|
486
624
|
different app than the requested one.
|
|
@@ -497,28 +635,37 @@ def open_app(name: str, *, spotlight_open: bool = False,
|
|
|
497
635
|
(still moving → the last read is authoritative).
|
|
498
636
|
② tap the row, then confirm via _tap_and_confirm_open (frame
|
|
499
637
|
diff ≥ OPEN_MIN_DIFF and the "Top Hit" header gone).
|
|
500
|
-
③ when that tap did nothing and the
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
638
|
+
③ when that tap did nothing and the post-tap read still shows the
|
|
639
|
+
exact label inside the Top Hit card (choose_app_hit, F35), tap the
|
|
640
|
+
icon SPOTLIGHT_ICON_ABOVE_LABEL above THAT label and confirm the
|
|
641
|
+
same way. Otherwise NO second tap: an upward offset from a stale
|
|
642
|
+
position could land on a different app's row.
|
|
505
643
|
④ still nothing confirmed → back to the Home Screen (View ▸
|
|
506
644
|
Home Screen), reported as not_confirmed.
|
|
507
645
|
|
|
508
646
|
A capture failure after the hit is found (WindowError /
|
|
509
647
|
CaptureError, F25) counts as unconfirmed — no further taps.
|
|
510
648
|
|
|
511
|
-
Returns "opened"; "not_found" (
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
Screen);
|
|
649
|
+
Returns "opened"; "not_found" (a Top Hit card was seen, but never
|
|
650
|
+
with exactly this name; back on the Home Screen); "no_top_hit" (no
|
|
651
|
+
read showed Spotlight's "Top Hit" header at all — no such app, or a
|
|
652
|
+
non-English iPhone; back on the Home Screen); "not_confirmed" (row
|
|
653
|
+
— and possibly the Top Hit icon — tapped but the app never
|
|
654
|
+
verifiably opened; back on the Home Screen); "not_typed" (type_text
|
|
655
|
+
did not see the paste arrive and the search field never showed the
|
|
656
|
+
name during the late-paste reads either — the name may never have
|
|
657
|
+
reached the phone, so this is no evidence the app is missing; back on
|
|
658
|
+
the Home Screen);
|
|
659
|
+
"deadline" (the OPEN_DEADLINE_S budget ran out before a tap; back on
|
|
660
|
+
the Home Screen); or "spotlight_unavailable" (Spotlight could not be
|
|
515
661
|
opened; nothing was typed or tapped).
|
|
516
662
|
|
|
517
663
|
``read_screen`` (default vision.read_screen), ``luma`` (default
|
|
518
664
|
verify.window_luma), ``sleep``, ``tap`` (default inp.tap),
|
|
519
665
|
``home`` (default View ▸ Home Screen), ``clear_text`` and
|
|
520
666
|
``type_text`` are injectable so tests can drive this without a
|
|
521
|
-
phone.
|
|
667
|
+
phone. ``report`` (a dict), when given, receives ``home``: whether
|
|
668
|
+
the last trip back to the Home Screen worked.
|
|
522
669
|
"""
|
|
523
670
|
if read_screen is None:
|
|
524
671
|
read_screen = vision.read_screen
|
|
@@ -527,23 +674,44 @@ def open_app(name: str, *, spotlight_open: bool = False,
|
|
|
527
674
|
if tap is None:
|
|
528
675
|
tap = inp.tap
|
|
529
676
|
if home is None:
|
|
530
|
-
home =
|
|
677
|
+
home = home_via_menu
|
|
678
|
+
|
|
679
|
+
def tidy():
|
|
680
|
+
# back to the Home Screen; the caller learns whether that worked
|
|
681
|
+
ok = bool(home())
|
|
682
|
+
if report is not None:
|
|
683
|
+
report["home"] = ok
|
|
684
|
+
return ok
|
|
685
|
+
|
|
531
686
|
if clear_text is None:
|
|
532
687
|
clear_text = inp.clear_text
|
|
533
688
|
pending: list = []
|
|
534
689
|
if type_text is None:
|
|
535
690
|
def type_text(text):
|
|
536
|
-
|
|
691
|
+
# F42: never give the clipboard back here — the name can reach the phone more
|
|
692
|
+
# than 10 s after Cmd+V, and Spotlight can show it in its own suggestions before
|
|
693
|
+
# that; a restore in between would paste the user's clipboard into the search
|
|
694
|
+
return inp.type_text(text, pending=pending, restore=False)
|
|
537
695
|
raw_read = read_screen
|
|
696
|
+
deadline = None if deadline_s is None else clock() + deadline_s
|
|
697
|
+
saw_top_hit = [False]
|
|
698
|
+
saw_echo = [False]
|
|
538
699
|
|
|
539
700
|
def read_screen(**kwargs):
|
|
540
|
-
# F33: every later read is also evidence for a clipboard restore
|
|
541
|
-
# type_text
|
|
701
|
+
# F33: every later read is also evidence for a clipboard restore an
|
|
702
|
+
# injected type_text may have handed over (the default never does: F42).
|
|
542
703
|
hits = raw_read(**kwargs)
|
|
543
704
|
for restore in pending:
|
|
544
705
|
restore.offer(hits)
|
|
706
|
+
if _tophit_header(hits) is not None:
|
|
707
|
+
saw_top_hit[0] = True # tells "not_found" apart from "no_top_hit"
|
|
708
|
+
if _field_echo(hits, name):
|
|
709
|
+
saw_echo[0] = True # F42: the name is in the search field — the paste arrived
|
|
545
710
|
return hits
|
|
546
711
|
|
|
712
|
+
def out_of_time():
|
|
713
|
+
return deadline is not None and clock() >= deadline
|
|
714
|
+
|
|
547
715
|
short = len(_norm_label(name)) <= 2
|
|
548
716
|
|
|
549
717
|
def reread():
|
|
@@ -554,26 +722,64 @@ def open_app(name: str, *, spotlight_open: bool = False,
|
|
|
554
722
|
return read_screen(also_sweep=True) if short else read_screen()
|
|
555
723
|
|
|
556
724
|
# F34: one retry — on the device the Spotlight command was once ignored
|
|
557
|
-
# right after an app closed. Each try
|
|
558
|
-
#
|
|
725
|
+
# right after an app closed. Each try goes Home first; when Home lands back in
|
|
726
|
+
# Spotlight (iOS 27, F43) the gate reports it open instead of toggling it shut.
|
|
559
727
|
if not spotlight_open and not (open_spotlight_checked()
|
|
560
728
|
or open_spotlight_checked()):
|
|
561
729
|
return "spotlight_unavailable"
|
|
730
|
+
if out_of_time():
|
|
731
|
+
# the budget was spent before anything was typed (a tiny
|
|
732
|
+
# NORACU_PHONE_TIMEOUT_MS, or a slow start) — do not clear or type at all
|
|
733
|
+
tidy()
|
|
734
|
+
return "deadline"
|
|
562
735
|
clear_text() # iOS kept the last query; new input would append (F17)
|
|
563
736
|
sleep(0.3)
|
|
564
737
|
typed = type_text(name)
|
|
565
738
|
sleep(1.5) # wait for Spotlight results
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
739
|
+
hit = None
|
|
740
|
+
if typed is not False:
|
|
741
|
+
hit = find_app_hit(read_screen, name, attempts=2, sleep=sleep,
|
|
742
|
+
deadline=deadline, clock=clock)
|
|
743
|
+
if hit is None and not saw_echo[0]:
|
|
744
|
+
# F33/F42: the name is not in the search field yet — the paste may still be on its
|
|
745
|
+
# way, whether type_text missed it or took another change for it (a clear that
|
|
746
|
+
# landed late redraws the results: seen on the device after a back-to-back open)
|
|
747
|
+
limit = clock() + OPEN_LATE_PASTE_S
|
|
748
|
+
if deadline is not None:
|
|
749
|
+
limit = min(limit, deadline)
|
|
750
|
+
echo_passes = 0
|
|
751
|
+
for i in range(OPEN_LATE_PASTE_PASSES):
|
|
752
|
+
# the pass right after the first echo still runs past the limit (the results
|
|
753
|
+
# trail the query by a moment), but never past the open budget
|
|
754
|
+
confirming = echo_passes == 1
|
|
755
|
+
if i:
|
|
756
|
+
stop_at = deadline if confirming else limit
|
|
757
|
+
if stop_at is not None and clock() >= stop_at:
|
|
758
|
+
break
|
|
759
|
+
sleep(1.0)
|
|
760
|
+
hit = find_app_hit(read_screen, name, attempts=1, sleep=sleep,
|
|
761
|
+
deadline=deadline if confirming else limit, clock=clock)
|
|
762
|
+
if hit is not None:
|
|
763
|
+
break
|
|
764
|
+
# only the search field showing the name proves the paste arrived — a Top Hit
|
|
765
|
+
# card may still be the previous query's; once it is in, one more pass
|
|
766
|
+
if saw_echo[0]:
|
|
767
|
+
echo_passes += 1
|
|
768
|
+
if echo_passes >= 2:
|
|
769
|
+
break
|
|
571
770
|
if hit is None:
|
|
572
771
|
# Leave the phone tidy: back to the home screen (View ▸ Home Screen).
|
|
573
|
-
|
|
574
|
-
|
|
772
|
+
tidy()
|
|
773
|
+
if out_of_time():
|
|
774
|
+
return "deadline"
|
|
775
|
+
if not saw_echo[0]:
|
|
776
|
+
return "not_typed" # F42: the name never showed up in the search field
|
|
777
|
+
return "not_found" if saw_top_hit[0] else "no_top_hit"
|
|
575
778
|
|
|
576
779
|
try:
|
|
780
|
+
if out_of_time():
|
|
781
|
+
tidy()
|
|
782
|
+
return "deadline"
|
|
577
783
|
# ① the Spotlight results can still be reflowing when
|
|
578
784
|
# find_app_hit returns; re-read after 0.5s and re-pick.
|
|
579
785
|
sleep(0.5)
|
|
@@ -590,22 +796,27 @@ def open_app(name: str, *, spotlight_open: bool = False,
|
|
|
590
796
|
hit = once_more
|
|
591
797
|
# else: the two reads agree — the first hit stands
|
|
592
798
|
|
|
593
|
-
|
|
799
|
+
if out_of_time():
|
|
800
|
+
tidy()
|
|
801
|
+
return "deadline"
|
|
802
|
+
# ② tap the row (the label inside the Top Hit card) and confirm.
|
|
803
|
+
# The post-tap read uses reread(): for a short name it also sweeps,
|
|
804
|
+
# so ③ can find the label again (F32/F35).
|
|
594
805
|
opened, hits = _tap_and_confirm_open(
|
|
595
806
|
hit.nx, hit.ny, luma=luma, sleep=sleep, tap=tap,
|
|
596
|
-
read_screen=
|
|
807
|
+
read_screen=reread)
|
|
597
808
|
if opened:
|
|
598
809
|
return "opened"
|
|
599
810
|
|
|
600
|
-
# ③ the label tap did nothing
|
|
601
|
-
#
|
|
602
|
-
#
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
811
|
+
# ③ the label tap did nothing → tap the icon above the label, but
|
|
812
|
+
# only where the post-tap read shows that label inside the Top Hit
|
|
813
|
+
# card NOW (choose_app_hit, F35) — never the first read's position:
|
|
814
|
+
# the results may have changed. No such label → no second tap.
|
|
815
|
+
again = choose_app_hit(hits, name)
|
|
816
|
+
if again is not None and not out_of_time():
|
|
606
817
|
opened, _ = _tap_and_confirm_open(
|
|
607
|
-
|
|
608
|
-
luma=luma, sleep=sleep, tap=tap, read_screen=
|
|
818
|
+
again.nx, again.ny - SPOTLIGHT_ICON_ABOVE_LABEL,
|
|
819
|
+
luma=luma, sleep=sleep, tap=tap, read_screen=reread)
|
|
609
820
|
if opened:
|
|
610
821
|
return "opened"
|
|
611
822
|
except Exception as exc:
|
|
@@ -615,7 +826,7 @@ def open_app(name: str, *, spotlight_open: bool = False,
|
|
|
615
826
|
# no further taps; wrap up unconfirmed below.
|
|
616
827
|
|
|
617
828
|
# ④ leave the phone tidy and say the taps were not confirmed
|
|
618
|
-
|
|
829
|
+
tidy()
|
|
619
830
|
return "not_confirmed"
|
|
620
831
|
|
|
621
832
|
|
|
@@ -629,15 +840,19 @@ def status() -> dict:
|
|
|
629
840
|
|
|
630
841
|
Keys: running; and when running: window {x,y,w,h} (AX bounds), window_id, onscreen, frontmost,
|
|
631
842
|
live (True/False/null — is the phone stream up), overlay (text shown over the stream when not
|
|
632
|
-
live, e.g. a paused/connecting notice), paused (True/False, or null when the state is unknown)
|
|
843
|
+
live, e.g. a paused/connecting notice), paused (True/False, or null when the state is unknown),
|
|
844
|
+
setup (True when the overlay is a setup / welcome screen the user must finish — F37; then
|
|
845
|
+
paused is False: there is no Resume control to press).
|
|
633
846
|
"""
|
|
634
847
|
from .window import (
|
|
635
848
|
get_overlay, get_window_info, is_frontmost, is_paused, is_running,
|
|
849
|
+
is_setup_screen,
|
|
636
850
|
)
|
|
637
851
|
|
|
638
852
|
running = is_running()
|
|
639
853
|
info = {"running": running}
|
|
640
854
|
if running:
|
|
855
|
+
b = None
|
|
641
856
|
try:
|
|
642
857
|
b = get_bounds()
|
|
643
858
|
info["window"] = {"x": b.x, "y": b.y, "w": b.width, "h": b.height}
|
|
@@ -651,7 +866,9 @@ def status() -> dict:
|
|
|
651
866
|
overlay = get_overlay()
|
|
652
867
|
info["live"] = overlay["live"]
|
|
653
868
|
info["overlay"] = overlay["overlay"]
|
|
654
|
-
|
|
869
|
+
setup = b is not None and is_setup_screen(overlay, b)
|
|
870
|
+
info["paused"] = False if setup else is_paused(overlay)
|
|
871
|
+
info["setup"] = setup
|
|
655
872
|
return info
|
|
656
873
|
|
|
657
874
|
|