I found this code and have been using for an app that pastes measurements from a bluetooth laser distance measurement device to the focused application.
When I run it for the first time MacOS prompts me to allow it to use the accessibility features and I allow it. It then works as suspected. But if I change something in the code and recompile it, it doesn't work any longer. I then first have to delete my app from Preferences/Security/Privacy/Accessebility and run it again. Then it asked again for permission and works.
Any idea what I'm doing wrong here? I'm quite a beginner and only started to code again because I needed a Mac app for that device.
func pasteMatchStyle() {
/*Source: https://stackoverflow.com/questions/40096457/swift-macos-how-to-paste-into-another-application?noredirect=1&lq=1
*/
let event1 = CGEvent(keyboardEventSource: nil, virtualKey: 0x09, keyDown: true) // opt-shft-cmd-v down
event1?.flags = [CGEventFlags.maskCommand, CGEventFlags.maskShift, CGEventFlags.maskAlternate]
event1?.post(tap: CGEventTapLocation.cghidEventTap)
let event2 = CGEvent(keyboardEventSource: nil, virtualKey: 0x09, keyDown: false) // opt-shf-cmd-v up
event2?.post(tap: CGEventTapLocation.cghidEventTap)
}