2

Building a macOS app that gets the currently active NSRunningApplication.

public func findActiveApp() -> NSRunningApplication? {
    for app in NSWorkspace.shared.runningApplications {
        if app.isActive {
            return app
        }
    }
    return nil
}

After that, I'm just trying to terminate the application using

let app = findActiveApp()
app.terminate()

It's not terminating the application and returning false (this is for any app not just a specific one).

I have also tried app.forceTerminate().

Please note that I have already added my application to the Accessibility list in System Preferences.

Do you have any clue to what might be the cause of this?

Thanks!

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • I'd appreciate if somebody can let me know if code-signing, App Sandboxing, and Run Schemes have anything to do with allowing me to terminate the NSRunningApplication. – Jad El Jerdy Oct 24 '18 at 16:38

1 Answers1

1

For a sandboxed app, you'll need to add the "com.apple.security.temporary-exception.apple-events" entitlement, like:

<key>com.apple.security.temporary-exception.apple-events</key>
<array>
    <string>com.my.company.AppId</string>
</array>

Note, though, that it may be rejected from an app store submission unless you have a good explanation for the need for the entitlement.

marcprux
  • 9,845
  • 3
  • 55
  • 72