4

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

2 Answers2

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
  • 517
  • 5
  • 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
  • 2
    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
0

I have been there wrapping my head around spending a lot of times.
Eventually, I sorted it out and hopefully it could the same problem you encountered.


You may have not set up App Sandbox on a macOS app.

Go to project setting -> Signing & Capabilities -> + Capability -> Add App Sandbox.

To look like this on Signing & Capability tab in Xcode project :
enter image description here

(It shouldn't matter which options are checked on/off)

too late to the party, but It works for me now at least in 2023.

I guess it might be the slightly different problem you were running into,
but I hope it may help other devs deal with the problem.

boraseoksoon
  • 2,164
  • 1
  • 20
  • 25