import AppKit import ApplicationServices import OSLog /// Performs the agent's actions with Accessibility actions where possible, synthetic /// CGEvents otherwise. Mouse actions put the pointer back where the user left it. enum ActionExecutor { /// Stamped on every synthetic event so Seer's own Escape monitor can ignore them. static let syntheticEventMarker: Int64 = 0x5EE5 struct Key: Sendable { let name: String let description: String let keyCode: CGKeyCode var flags: CGEventFlags = [] } /// The keys and shortcuts the agent may press. Destructive shortcuts (⌘Q, ⌘⌫) are left out. static let keys: [Key] = [ Key(name: "return", description: "Return: submit or go to the text just typed into the focused field", keyCode: 36), Key(name: "tab", description: "Tab: move to the next field", keyCode: 48), Key(name: "escape", description: "Escape: close a menu, popup or dialog", keyCode: 53), Key(name: "space", description: "Space bar", keyCode: 49), Key(name: "delete", description: "Delete: erase one character", keyCode: 51), Key(name: "up", description: "Up arrow", keyCode: 126), Key(name: "down", description: "Down arrow", keyCode: 125), Key(name: "left", description: "Left arrow", keyCode: 123), Key(name: "right", description: "Right arrow", keyCode: 124), Key(name: "cmd+a", description: "⌘A: select all", keyCode: 0, flags: .maskCommand), Key(name: "cmd+c", description: "⌘C: copy", keyCode: 8, flags: .maskCommand), Key(name: "cmd+v", description: "⌘V: paste", keyCode: 9, flags: .maskCommand), Key(name: "cmd+z", description: "⌘Z: undo", keyCode: 6, flags: .maskCommand), Key(name: "cmd+f", description: "⌘F: find text in the page or document", keyCode: 3, flags: .maskCommand), Key(name: "cmd+l", description: "⌘L: put the cursor in the browser address bar when it isn't focused yet", keyCode: 37, flags: .maskCommand), Key(name: "cmd+t", description: "⌘T: open a new browser tab", keyCode: 17, flags: .maskCommand), Key(name: "cmd+n", description: "⌘N: new window, document or message", keyCode: 45, flags: .maskCommand), Key(name: "cmd+w", description: "⌘W: close the current tab or window", keyCode: 13, flags: .maskCommand), Key(name: "cmd+s", description: "⌘S: save", keyCode: 1, flags: .maskCommand), Key(name: "cmd+comma", description: "⌘,: open the app's settings", keyCode: 43, flags: .maskCommand), ] // MARK: - Mouse static func click(_ element: ScreenElement) { if element.isEditable { // Pressing doesn't focus a text field; a real click puts the caret in it. AXUIElementSetAttributeValue(element.element, kAXFocusedAttribute as CFString, kCFBooleanTrue) mouseClick(at: element.center) } else if element.canPress, AXUIElementPerformAction(element.element, kAXPressAction as CFString) == .success { return } else { mouseClick(at: element.center) } } static func mouseClick(at point: CGPoint) { withPointer(at: point) { source in for type in [CGEventType.leftMouseDown, .leftMouseUp] { let event = CGEvent(mouseEventSource: source, mouseType: type, mouseCursorPosition: point, mouseButton: .left) post(event) usleep(40_000) } } } static func scroll(down: Bool, in frame: CGRect) { let point = frame.isEmpty ? (CGEvent(source: nil)?.location ?? .zero) : CGPoint(x: frame.midX, y: frame.midY) withPointer(at: point) { source in let event = CGEvent(scrollWheelEvent2Source: source, units: .line, wheelCount: 1, wheel1: down ? -8 : 8, wheel2: 0, wheel3: 0) post(event) } } /// Moves the pointer to `point` for `body`, then puts it back. private static func withPointer(at point: CGPoint, _ body: (CGEventSource?) -> Void) { let saved = CGEvent(source: nil)?.location let source = CGEventSource(stateID: .hidSystemState) post(CGEvent(mouseEventSource: source, mouseType: .mouseMoved, mouseCursorPosition: point, mouseButton: .left)) usleep(30_000) body(source) usleep(30_000) if let saved { CGWarpMouseCursorPosition(saved) } } // MARK: - Keyboard /// Keyboard events go straight to `pid`, so Seer's own panel never swallows them. static func type(_ text: String, pid: pid_t) { let source = CGEventSource(stateID: .hidSystemState) let units = Array(text.utf16) for start in stride(from: 0, to: units.count, by: 16) { let chunk = Array(units[start.. [(name: String, url: URL)] { let fm = FileManager.default let folders = ["/Applications", "/Applications/Utilities", "/System/Applications", "/System/Applications/Utilities", "\(NSHomeDirectory())/Applications"] var byName: [String: URL] = [:] for folder in folders { for item in (try? fm.contentsOfDirectory(atPath: folder)) ?? [] where item.hasSuffix(".app") { let name = String(item.dropLast(4)) if byName[name] == nil { byName[name] = URL(fileURLWithPath: folder).appendingPathComponent(item) } } } let running = Set(NSWorkspace.shared.runningApplications .filter { $0.activationPolicy == .regular } .compactMap(\.localizedName)) let lowerGoal = goal.lowercased() func rank(_ name: String) -> Int { if lowerGoal.contains(name.lowercased()) { return 0 } if running.contains(name) { return 1 } return 2 } return byName .sorted { (rank($0.key), $0.key) < (rank($1.key), $1.key) } .prefix(limit) .map { (name: $0.key, url: $0.value) } } }