|
1 | 1 | import AppKit |
2 | 2 | import ApplicationServices |
| 3 | +import Carbon.HIToolbox |
3 | 4 |
|
4 | 5 | struct PasteService { |
5 | 6 | static func paste(text: String) { |
| 7 | + // Try AX insertion first — works in native AppKit fields without touching clipboard |
| 8 | + if pasteViaAX(text: text) { return } |
| 9 | + // Fall back to clipboard + Cmd+V — works everywhere (Terminal, web, Electron, etc.) |
| 10 | + pasteViaKeyboard(text: text) |
| 11 | + } |
| 12 | + |
| 13 | + @discardableResult |
| 14 | + private static func pasteViaAX(_ text: String) -> Bool { |
6 | 15 | let systemWide = AXUIElementCreateSystemWide() |
7 | 16 | var focusedElement: CFTypeRef? |
8 | 17 | guard AXUIElementCopyAttributeValue(systemWide, kAXFocusedUIElementAttribute as CFString, &focusedElement) == .success, |
9 | | - let element = focusedElement else { return } |
10 | | - AXUIElementSetAttributeValue(element as! AXUIElement, kAXSelectedTextAttribute as CFString, text as CFString) |
| 18 | + let element = focusedElement else { return false } |
| 19 | + let result = AXUIElementSetAttributeValue(element as! AXUIElement, kAXSelectedTextAttribute as CFString, text as CFString) |
| 20 | + return result == .success |
| 21 | + } |
| 22 | + |
| 23 | + private static func pasteViaKeyboard(_ text: String) { |
| 24 | + let pasteboard = NSPasteboard.general |
| 25 | + |
| 26 | + // Preserve whatever was on the clipboard |
| 27 | + let previousItems = pasteboard.pasteboardItems?.compactMap { item -> [NSPasteboard.PasteboardType: Data]? in |
| 28 | + var dataMap: [NSPasteboard.PasteboardType: Data] = [:] |
| 29 | + for type in item.types { |
| 30 | + if let data = item.data(forType: type) { dataMap[type] = data } |
| 31 | + } |
| 32 | + return dataMap.isEmpty ? nil : dataMap |
| 33 | + } |
| 34 | + |
| 35 | + pasteboard.clearContents() |
| 36 | + pasteboard.setString(text, forType: .string) |
| 37 | + |
| 38 | + // Simulate Cmd+V |
| 39 | + let src = CGEventSource(stateID: .hidSystemState) |
| 40 | + let vKey = CGKeyCode(kVK_ANSI_V) |
| 41 | + let down = CGEvent(keyboardEventSource: src, virtualKey: vKey, keyDown: true) |
| 42 | + let up = CGEvent(keyboardEventSource: src, virtualKey: vKey, keyDown: false) |
| 43 | + down?.flags = .maskCommand |
| 44 | + up?.flags = .maskCommand |
| 45 | + down?.post(tap: .cghidEventTap) |
| 46 | + up?.post(tap: .cghidEventTap) |
| 47 | + |
| 48 | + // Restore clipboard after the keystroke has been processed |
| 49 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { |
| 50 | + pasteboard.clearContents() |
| 51 | + if let items = previousItems, !items.isEmpty { |
| 52 | + for dataMap in items { |
| 53 | + let newItem = NSPasteboardItem() |
| 54 | + for (type, data) in dataMap { newItem.setData(data, forType: type) } |
| 55 | + pasteboard.writeObjects([newItem]) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
11 | 59 | } |
12 | 60 |
|
13 | 61 | static func getSelectedText() -> String? { |
|
0 commit comments