Files
vectorcraft/.github/macos-undo-input.swift

17 lines
750 B
Swift

import Cocoa
import CoreGraphics
let pid = Int32(CommandLine.arguments[1])!
let app = NSRunningApplication(processIdentifier: pid)!
app.activate(options: [.activateAllWindows, .activateIgnoringOtherApps])
Thread.sleep(forTimeInterval: 0.3)
print("frontmost matches: \(NSWorkspace.shared.frontmostApplication?.processIdentifier == pid); CG event access: \(CGPreflightPostEventAccess())")
let mode = CommandLine.arguments[2]
let code: CGKeyCode = mode == "tool" ? 46 : 6
for down in [true, false] {
let event = CGEvent(keyboardEventSource: nil, virtualKey: code, keyDown: down)!
if mode != "tool" { event.flags = mode == "redo" ? [.maskCommand, .maskShift] : [.maskCommand] }
event.postToPid(pid)
Thread.sleep(forTimeInterval: 0.12)
}