Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291import ApplicationServicesimport Foundationimport OSLog
@MainActorpublic enum MenuShortcutReader { /// For unmanaged apps only. Pass the existing AppThread to read(on:) when the app already has an owner. public static func read(pid: Int32) async throws -> [MenuShortcut] { guard pid > 0 else { throw InputError.invalidProcess(pid) } guard AXIsProcessTrusted() else { throw InputError.accessibilityRequired } try Task.checkCancellation() let appThread = try AppThread(pid: pid) defer { appThread.shutdown() } return try await read(on: appThread) }
public static func read(on appThread: AppThread) async throws -> [MenuShortcut] { guard AXIsProcessTrusted() else { throw InputError.accessibilityRequired } try Task.checkCancellation() let layout = try MenuKeyResolver.currentLayout() let cancellation = AppThreadWriteCancellation() return try await withTaskCancellationHandler { try await retryTransientChildrenFailure { try await appThread.perform { application in guard !cancellation.isCancelled else { throw CancellationError() } var pid: pid_t = 0 try checkAX(AXUIElementGetPid(application.raw, &pid)) return try MenuWalker( pid: pid, layout: layout, timeout: application.timeout, checkCancellation: { guard !cancellation.isCancelled else { throw CancellationError() } } ).read(application.raw) } } } onCancel: { cancellation.cancel() } }
static func retryTransientChildrenFailure( delay: () async throws -> Void = { try await Task.sleep(for: .milliseconds(80)) }, read: () async throws -> [MenuShortcut] ) async throws -> [MenuShortcut] { try Task.checkCancellation() do { let shortcuts = try await read() try Task.checkCancellation() return shortcuts } catch let error as InputError { guard case .accessibility(let pid, kAXChildrenAttribute, let cause) = error, cause != .notPermitted, cause != .elementInvalid, cause != .attributeUnsupported else { throw error } Logger(subsystem: "dev.vinnymac.parcel", category: "menu-shortcuts") .debug("Retrying transient menu children failure for PID \(pid)") try await delay() try Task.checkCancellation() let shortcuts = try await read() try Task.checkCancellation() return shortcuts } }}
enum MenuAXValue { private static let logger = Logger(subsystem: "dev.vinnymac.parcel", category: "menu-shortcuts")
private static func invalid(_ value: CFTypeRef, attribute: String) -> InputError { logger.debug("Rejected menu attribute \(attribute, privacy: .public), CF type \(CFGetTypeID(value))") return .unexpectedAccessibilityValue(attribute) }
static func string(_ value: CFTypeRef, attribute: String) throws -> String { guard CFGetTypeID(value) == CFStringGetTypeID(), let string = value as? String else { throw invalid(value, attribute: attribute) } return string }
static func element(_ value: CFTypeRef, attribute: String) throws -> AXUIElement { guard CFGetTypeID(value) == AXUIElementGetTypeID() else { throw invalid(value, attribute: attribute) } // Core Foundation has no checked Swift downcast; the type ID check is the boundary. return Unmanaged<AXUIElement>.fromOpaque(Unmanaged.passUnretained(value).toOpaque()).takeUnretainedValue() }
static func children(_ value: CFTypeRef) throws -> [AXUIElement] { guard CFGetTypeID(value) == CFArrayGetTypeID(), let values = value as? [CFTypeRef] else { throw invalid(value, attribute: kAXChildrenAttribute) } return try values.map { try element($0, attribute: kAXChildrenAttribute) } }
static func boolean(_ value: CFTypeRef, attribute: String) throws -> Bool { guard CFGetTypeID(value) == CFBooleanGetTypeID(), let number = value as? NSNumber else { throw invalid(value, attribute: attribute) } return number.boolValue }
static func integer(_ value: CFTypeRef, attribute: String) throws -> Int { guard CFGetTypeID(value) == CFNumberGetTypeID(), let number = value as? NSNumber, let integer = Int(exactly: number) else { throw invalid(value, attribute: attribute) } return integer }
static func checkCall(_ status: AXError, elapsed: Double, timeout: Float, pid: Int32, attribute: String) throws { do { try AXElement.checkCall(status, elapsed: elapsed, timeout: timeout) } catch let error as AXPlatformError { throw InputError.accessibility(pid: pid, attribute: attribute, cause: error) } }}
struct MenuWalker { private static let logger = Logger(subsystem: "dev.vinnymac.parcel", category: "menu-shortcuts")
let pid: Int32 let layout: MenuKeyResolver let timeout: Float private let deadline: ContinuousClock.Instant private let checkCancellation: @Sendable () throws -> Void
init( pid: Int32, layout: MenuKeyResolver, timeout: Float, deadline: ContinuousClock.Instant = ContinuousClock.now.advanced(by: .seconds(5)), checkCancellation: @escaping @Sendable () throws -> Void = {} ) { self.pid = pid self.layout = layout self.timeout = timeout self.deadline = deadline self.checkCancellation = checkCancellation }
func read(_ application: AXUIElement) throws -> [MenuShortcut] { precondition(!Thread.isMainThread) guard AXIsProcessTrusted() else { throw InputError.accessibilityRequired } guard let rawMenu = try attribute(application, kAXMenuBarAttribute) else { return [] } let menu = try MenuAXValue.element(rawMenu, attribute: kAXMenuBarAttribute) return try walk(menu, attribute: attribute, childrenCount: childrenCount) }
func walk( _ menu: AXUIElement, attribute: (AXUIElement, String) throws -> CFTypeRef?, childrenCount: (AXUIElement) throws -> Int? = { _ in nil } ) throws -> [MenuShortcut] { var pending: [(AXUIElement, [String], Int)] = [(menu, [], 0)] var visited: [AXUIElement] = [] var result: [MenuShortcut] = [] while let (item, parentPath, depth) = pending.popLast() { try checkCancellation() guard visited.count < 4096, depth < 64, ContinuousClock.now < deadline else { throw InputError.menuTraversalLimit } if visited.contains(where: { CFEqual($0, item) }) { continue } visited.append(item) var path = parentPath var skipShortcut = false do { // Help search fields advertise children that AX cannot copy; they are not menu commands. if try string(item, kAXRoleAttribute, attribute: attribute) == kAXTextFieldRole { continue } if let enabled = try attribute(item, kAXEnabledAttribute), try !MenuAXValue.boolean(enabled, attribute: kAXEnabledAttribute) { continue } } catch let error as InputError { try Self.logSkippedItem(error) skipShortcut = true } do { if let title = try string(item, kAXTitleAttribute, attribute: attribute), !title.isEmpty { path.append(title) } } catch let error as InputError { try Self.logSkippedItem(error) skipShortcut = true } if !skipShortcut { do { let character = try string(item, kAXMenuItemCmdCharAttribute, attribute: attribute) let virtualKey = try number(item, kAXMenuItemCmdVirtualKeyAttribute, attribute: attribute) let rawModifiers = try number(item, kAXMenuItemCmdModifiersAttribute, attribute: attribute) ?? 0 guard let modifiers = UInt32(exactly: rawModifiers) else { throw InputError.unexpectedAccessibilityValue(kAXMenuItemCmdModifiersAttribute) } let keys = try layout.resolve( virtualKey: virtualKey, character: character, modifiers: modifiers, glyph: try number(item, kAXMenuItemCmdGlyphAttribute, attribute: attribute)) result += keys.map { MenuShortcut(pid: pid, path: path, character: character, hotKey: $0) } } catch let error as InputError { try Self.logSkippedItem(error) } } do { // Some menu leaves reject AXChildren instead of returning an empty array. if try childrenCount(item) == 0 { continue } if let children = try attribute(item, kAXChildrenAttribute) { for child in try MenuAXValue.children(children).reversed() { pending.append((child, path, depth + 1)) } } } catch let error as InputError { guard !CFEqual(item, menu), case .accessibility(_, kAXChildrenAttribute, let cause) = error, Self.canSkipBranch(cause) else { throw error } // An unavailable dynamic submenu must not discard readable sibling shortcuts. Self.logger.debug( "Skipped menu branch for PID \(pid), depth \(depth): \(String(reflecting: cause), privacy: .public)" ) } } return result }
private static func canSkipBranch(_ cause: AXPlatformError) -> Bool { switch cause { case .timedOut, .messagingFailure, .elementInvalid, .other: true default: false } }
private static func logSkippedItem(_ error: InputError) throws { switch error { case .unexpectedAccessibilityValue, .unresolvedMenuCharacter, .unsupportedMenuGlyph: break case .accessibility(_, _, let cause) where cause != .notPermitted: break default: throw error } logger.debug("Skipped menu item: \(String(reflecting: error), privacy: .private)") }
private func childrenCount(_ element: AXUIElement) throws -> Int? { try checkCancellation() guard ContinuousClock.now < deadline else { throw InputError.menuTraversalLimit } try MenuAXValue.checkCall( AXUIElementSetMessagingTimeout(element, timeout), elapsed: 0, timeout: timeout, pid: pid, attribute: "messagingTimeout") var count: CFIndex = 0 let start = ContinuousClock.now let status = AXUIElementGetAttributeValueCount(element, kAXChildrenAttribute as CFString, &count) let elapsed = start.duration(to: .now).components if status == .noValue || status == .attributeUnsupported { return nil } try MenuAXValue.checkCall( status, elapsed: Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18, timeout: timeout, pid: pid, attribute: kAXChildrenAttribute) guard count >= 0 else { throw InputError.unexpectedAccessibilityValue(kAXChildrenAttribute) } return count }
private func attribute(_ element: AXUIElement, _ name: String) throws -> CFTypeRef? { try checkCancellation() guard ContinuousClock.now < deadline else { throw InputError.menuTraversalLimit } // AX timeouts belong to individual elements, not the application subtree. try MenuAXValue.checkCall( AXUIElementSetMessagingTimeout(element, timeout), elapsed: 0, timeout: timeout, pid: pid, attribute: "messagingTimeout") var value: CFTypeRef? let start = ContinuousClock.now let status = AXUIElementCopyAttributeValue(element, name as CFString, &value) let elapsed = start.duration(to: .now).components if status == .noValue || status == .attributeUnsupported { return nil } try MenuAXValue.checkCall( status, elapsed: Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18, timeout: timeout, pid: pid, attribute: name) guard let value else { throw InputError.unexpectedAccessibilityValue(name) } return value }
private func string( _ element: AXUIElement, _ name: String, attribute: (AXUIElement, String) throws -> CFTypeRef? ) throws -> String? { guard let value = try attribute(element, name) else { return nil } return try MenuAXValue.string(value, attribute: name) }
private func number( _ element: AXUIElement, _ name: String, attribute: (AXUIElement, String) throws -> CFTypeRef? ) throws -> Int? { guard let value = try attribute(element, name) else { return nil } return try MenuAXValue.integer(value, attribute: name) }
}