3

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)
}
RK69
  • 41
  • 1
  • Unfortunately not as I have no problem with the prompt popping up on the first run. – RK69 Sep 16 '20 at 14:37

1 Answers1

1

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.

numist
  • 488
  • 4
  • 7
  • Thanks, that also works but it doesn't seem to be a good user experience. Would it change it's behaviour if I sign it with a developer acccount? I personally use apps, that don't require to me to check and uncheck it every time they get an update. – RK69 Sep 30 '20 at 06:05
  • 1
    Yes; in my experience, signing an app with an Apple developer account lets it keeps its permissions as long as the code signature’s „Team Identifier“ stays the same. – MrMage Dec 17 '21 at 22:17