MacOS Accessibility API not working after change in code and new build (Xcode/Swift simulating keyboard input)

Viewed 313

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)
}
1 Answers

It should work if you just uncheck and re-check the permissions box, and then re-run your app.

Accessibility permissions only apply to the app that was approved, but you've built a new version that the system has identified as different (read: untrusted), but that old version is still approved, so the box stays checked.

Related