|
| 1 | +import AppKit |
| 2 | +import ApplicationServices |
| 3 | +import CoreGraphics |
| 4 | +import Foundation |
| 5 | +import ImageIO |
| 6 | +import Vision |
| 7 | + |
| 8 | +struct VisualAlertMatch { |
| 9 | + let buttonLabel: String |
| 10 | +} |
| 11 | + |
| 12 | +func dismissLocalNetworkPermissionAlertVisually( |
| 13 | + screenshotPath: String, |
| 14 | + app: NSRunningApplication |
| 15 | +) throws -> VisualAlertMatch? { |
| 16 | + guard FileManager.default.fileExists(atPath: screenshotPath) else { |
| 17 | + return nil |
| 18 | + } |
| 19 | + let screenshotUrl = URL(fileURLWithPath: screenshotPath) |
| 20 | + guard let source = CGImageSourceCreateWithURL(screenshotUrl as CFURL, nil), |
| 21 | + let image = CGImageSourceCreateImageAtIndex(source, 0, nil) |
| 22 | + else { |
| 23 | + return nil |
| 24 | + } |
| 25 | + |
| 26 | + let request = VNRecognizeTextRequest() |
| 27 | + request.recognitionLevel = .accurate |
| 28 | + request.recognitionLanguages = ["en-US"] |
| 29 | + request.usesLanguageCorrection = false |
| 30 | + do { |
| 31 | + try VNImageRequestHandler(cgImage: image).perform([request]) |
| 32 | + } catch { |
| 33 | + throw HelperError.commandFailed( |
| 34 | + "alert screenshot recognition failed", |
| 35 | + details: ["error": error.localizedDescription] |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + let recognized = (request.results ?? []).compactMap { observation -> (String, CGRect)? in |
| 40 | + guard let candidate = observation.topCandidates(1).first else { |
| 41 | + return nil |
| 42 | + } |
| 43 | + return (candidate.string, observation.boundingBox) |
| 44 | + } |
| 45 | + let recognizedText = recognized.map(\.0).joined(separator: " ") |
| 46 | + guard recognizedText.localizedCaseInsensitiveContains("local network"), |
| 47 | + let denial = recognized.first(where: { text, _ in |
| 48 | + normalizeVisualAlertText(text).contains("don't allow") |
| 49 | + }), |
| 50 | + let windowRect = bestMatchingWindowRect( |
| 51 | + app: app, |
| 52 | + screenshotWidth: image.width, |
| 53 | + screenshotHeight: image.height |
| 54 | + ) |
| 55 | + else { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + let point = CGPoint( |
| 60 | + x: windowRect.x + Double(denial.1.midX) * windowRect.width, |
| 61 | + y: windowRect.y + (1 - Double(denial.1.midY)) * windowRect.height |
| 62 | + ) |
| 63 | + guard let move = CGEvent( |
| 64 | + mouseEventSource: nil, |
| 65 | + mouseType: .mouseMoved, |
| 66 | + mouseCursorPosition: point, |
| 67 | + mouseButton: .left |
| 68 | + ), |
| 69 | + let down = CGEvent( |
| 70 | + mouseEventSource: nil, |
| 71 | + mouseType: .leftMouseDown, |
| 72 | + mouseCursorPosition: point, |
| 73 | + mouseButton: .left |
| 74 | + ), |
| 75 | + let up = CGEvent( |
| 76 | + mouseEventSource: nil, |
| 77 | + mouseType: .leftMouseUp, |
| 78 | + mouseCursorPosition: point, |
| 79 | + mouseButton: .left |
| 80 | + ) |
| 81 | + else { |
| 82 | + throw HelperError.commandFailed( |
| 83 | + "alert action failed", |
| 84 | + details: ["reason": "event_creation_failed"] |
| 85 | + ) |
| 86 | + } |
| 87 | + move.post(tap: .cghidEventTap) |
| 88 | + down.post(tap: .cghidEventTap) |
| 89 | + up.post(tap: .cghidEventTap) |
| 90 | + return VisualAlertMatch(buttonLabel: denial.0) |
| 91 | +} |
| 92 | + |
| 93 | +private func normalizeVisualAlertText(_ value: String) -> String { |
| 94 | + value |
| 95 | + .replacingOccurrences(of: "’", with: "'") |
| 96 | + .trimmingCharacters(in: .whitespacesAndNewlines) |
| 97 | + .lowercased() |
| 98 | +} |
| 99 | + |
| 100 | +private func bestMatchingWindowRect( |
| 101 | + app: NSRunningApplication, |
| 102 | + screenshotWidth: Int, |
| 103 | + screenshotHeight: Int |
| 104 | +) -> RectResponse? { |
| 105 | + guard screenshotWidth > 0, screenshotHeight > 0 else { |
| 106 | + return nil |
| 107 | + } |
| 108 | + let screenshotAspectRatio = Double(screenshotWidth) / Double(screenshotHeight) |
| 109 | + return windows(of: AXUIElementCreateApplication(app.processIdentifier)) |
| 110 | + .compactMap(rectAttribute) |
| 111 | + .filter { $0.width > 0 && $0.height > 0 } |
| 112 | + .min { left, right in |
| 113 | + abs(left.width / left.height - screenshotAspectRatio) |
| 114 | + < abs(right.width / right.height - screenshotAspectRatio) |
| 115 | + } |
| 116 | +} |
0 commit comments