--------- Co-authored-by: intelligence <intelligence@debian.local> Co-authored-by: jourintown <jourintown@proton.me> Reviewed-on: #2
44 lines
1.2 KiB
Swift
44 lines
1.2 KiB
Swift
import AppKit
|
|
import ApplicationServices
|
|
import Carbon.HIToolbox
|
|
|
|
enum OutputResult {
|
|
case typed
|
|
case copied
|
|
case copiedBecauseAccessibilityIsOff
|
|
}
|
|
|
|
enum TranscriptOutput {
|
|
@MainActor
|
|
static func deliver(_ text: String, to destination: TranscriptDestination) -> OutputResult {
|
|
let pasteboard = NSPasteboard.general
|
|
pasteboard.clearContents()
|
|
pasteboard.setString(text, forType: .string)
|
|
guard destination == .cursor else { return .copied }
|
|
|
|
let options = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true]
|
|
guard AXIsProcessTrustedWithOptions(options as CFDictionary) else {
|
|
return .copiedBecauseAccessibilityIsOff
|
|
}
|
|
guard let source = CGEventSource(stateID: .hidSystemState),
|
|
let keyDown = CGEvent(
|
|
keyboardEventSource: source,
|
|
virtualKey: CGKeyCode(kVK_ANSI_V),
|
|
keyDown: true
|
|
),
|
|
let keyUp = CGEvent(
|
|
keyboardEventSource: source,
|
|
virtualKey: CGKeyCode(kVK_ANSI_V),
|
|
keyDown: false
|
|
)
|
|
else {
|
|
return .copiedBecauseAccessibilityIsOff
|
|
}
|
|
keyDown.flags = .maskCommand
|
|
keyUp.flags = .maskCommand
|
|
keyDown.post(tap: .cghidEventTap)
|
|
keyUp.post(tap: .cghidEventTap)
|
|
return .typed
|
|
}
|
|
}
|