From 03b4e008742dbb247c29dd39b4ee129ceec68107 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:13:46 +0100 Subject: [PATCH] Observe the visible Safari print prompt through macOS Vision --- .github/print-probe/safari-confirm.swift | 49 +++++++++++++++++++++++ .github/print-probe/safari.py | 12 +++++- .github/workflows/verify-safari-print.yml | 2 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .github/print-probe/safari-confirm.swift diff --git a/.github/print-probe/safari-confirm.swift b/.github/print-probe/safari-confirm.swift new file mode 100644 index 000000000..46ae1be19 --- /dev/null +++ b/.github/print-probe/safari-confirm.swift @@ -0,0 +1,49 @@ +import AppKit +import CoreGraphics +import Vision + +let request = VNRecognizeTextRequest() +request.recognitionLevel = .accurate +request.recognitionLanguages = ["en-US"] +try VNImageRequestHandler(url: URL(fileURLWithPath: CommandLine.arguments[1])).perform([request]) +let observations = request.results ?? [] + +guard let prompt = observations.first(where: { observation in + observation.topCandidates(1).first?.string.lowercased().hasPrefix("this webpage is trying to print.") == true +}) else { + print("no-webpage-confirmation") + exit(0) +} + +let buttons = try observations.compactMap { observation -> CGRect? in + guard let candidate = observation.topCandidates(1).first, + let range = candidate.string.range(of: #"\bPrint$"#, options: .regularExpression), + let box = try candidate.boundingBox(for: range)?.boundingBox else { return nil } + return box +}.filter { box in + box.midX > prompt.boundingBox.midX && box.midY < prompt.boundingBox.minY + && prompt.boundingBox.minY - box.midY < 0.16 +} +guard buttons.count == 1, let button = buttons.first else { + print("found-webpage-confirmation-without-one-print-button") + exit(0) +} +guard NSWorkspace.shared.frontmostApplication?.bundleIdentifier == "com.apple.Safari" else { + print("webpage-confirmation-safari-not-frontmost") + exit(0) +} +guard CGPreflightPostEventAccess() else { + print("webpage-confirmation-event-access-denied") + exit(0) +} + +let screen = CGDisplayBounds(CGMainDisplayID()) +let click = CGPoint(x: screen.minX + button.midX * screen.width, y: screen.minY + (1 - button.midY) * screen.height) +guard let down = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: click, mouseButton: .left), + let up = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: click, mouseButton: .left) else { + print("webpage-confirmation-event-creation-failed") + exit(0) +} +down.post(tap: .cghidEventTap) +up.post(tap: .cghidEventTap) +print("confirmed-webpage") diff --git a/.github/print-probe/safari.py b/.github/print-probe/safari.py index cf93ab141..d096845b3 100644 --- a/.github/print-probe/safari.py +++ b/.github/print-probe/safari.py @@ -142,13 +142,23 @@ def native_sheet(action): def wait_sheet(name, visible): deadline = time.monotonic() + 35 confirmed = False + last_confirmation = None while time.monotonic() < deadline: state = native_sheet("inspect") if (state == "print-sheet") == visible: print(f"{name}: {state}", flush=True) return if visible and not confirmed and name in COLORS: - confirmed = native_sheet("confirm-webpage") == "confirmed-webpage" + confirmation = native_sheet("confirm-webpage") + if confirmation != "confirmed-webpage" and os.environ.get("SAFARI_CONFIRM"): + path = RESULTS / f"{name}-webpage-confirmation.png" + if snapshot(path): + result = subprocess.run([os.environ["SAFARI_CONFIRM"], path], capture_output=True, text=True, timeout=10, check=True) + confirmation = result.stdout.strip() + if confirmation != last_confirmation: + print(f"{name}: {confirmation}", flush=True) + last_confirmation = confirmation + confirmed = confirmation == "confirmed-webpage" if confirmed: print(f"{name}: accepted Safari's per-print webpage confirmation", flush=True) time.sleep(0.35) diff --git a/.github/workflows/verify-safari-print.yml b/.github/workflows/verify-safari-print.yml index e5c5734d8..ebeb07226 100644 --- a/.github/workflows/verify-safari-print.yml +++ b/.github/workflows/verify-safari-print.yml @@ -62,6 +62,8 @@ jobs: python3 -m venv "$RUNNER_TEMP/safari-probe-venv" python="$RUNNER_TEMP/safari-probe-venv/bin/python" "$python" -m pip install Pillow==12.0.0 reportlab==4.4.4 selenium==4.38.0 + export SAFARI_CONFIRM="$RUNNER_TEMP/safari-confirm" + /usr/bin/swiftc .github/print-probe/safari-confirm.swift -o "$SAFARI_CONFIRM" bash .github/print-probe/mac-safari-capture.sh "$python" - name: Preserve output or the native-access diagnostic if: always()