Jad El Jerdy
Jad El Jerdy

Reputation: 46

NSRunningApplication terminate() returning false

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!

Upvotes: 2

Views: 604

Answers (1)

marcprux
marcprux

Reputation: 10355

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.

Upvotes: 1

Related Questions