Avoid permissions popup Mojave

Hello all,

I am developing an MacOS application which needs to check application which is foreground.

My problem is that with the update to Mojave, anytime that application goes foreground popup asking for permissions appears. I find this very annoying to the user.

I have been trying to find a way to fix this. Only thing I have found so far is to generate MDM profile. However since it seems that solution is designed for managed computers within companies or things like that and my application is designed to be used by anyone it won't work.

Is there any other way to solve this?? I am running out of ideas

Thanks in advance and regards

Replies

How are you determining the foreground application? I generally do this sort of thing use

NSWorkspace
and
NSRunningApplication
[1], and AFAIK there’s been no extra authorisation checks added to that API in 10.14.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] Specifically:

  • NSWorkspaceDidActivateApplicationNotification
  • NSWorkspaceDidDeactivateApplicationNotification
  • The

    active
    property on
    NSRunningApplication

Sorry I forgot to add that for that I am using AppleScript.

Application is done in C++ with Qt and AppleScript. Specifically I am using AppleScript to get active window, and also to get browser URL if active window is browser.

Is it possible to achieve that using Objective-C as well? The URL part I mean

Thanks in advance

Is it possible to achieve that using Objective-C as well? The URL part I mean

Not in any way that’ll be useful to you (you can use Objective-C to send the same Apple events that the AppleScript is using, but they’ll hit the same authorisation restrictions).

Having said that, your AppleScript should not trigger an authorisation warning every time you run it. The system remembers the user’s authorisation response and automatically allows (or denies) your request depending on that remembered response without further user interaction.

It’s possible that your app is packaged in a way that’s preventing this. The most common cause of such problems is code signing. You need to make sure that your code is signed consistently for this feature to work (code signing establishes code identity, and code identity is needed for the system to remember this authorisation state).

Consider the following code:

static NSString * kScript = @
    "tell application \"Safari\"\n"
    "    URL of current tab of window 1\n"
    "end tell";

- (IBAction)testAction:(id)sender {
    #pragma unused(sender)
    if (self.script == nil) {
        self.script = [[NSAppleScript alloc] initWithSource:kScript];
        assert(self.script != nil);
    }
    NSAppleEventDescriptor * result = [self.script executeAndReturnError:nil];
    NSLog(@"%@", result);
}

The first time I click the Test button it runs the script and the system asks me whether to authorise my app controlling Safari. If I allow that then my app prints:

2018-12-07 09:53:25.677685+0000 xxom[83945:5748328] <NSAppleEventDescriptor: 'utxt'("https://forums.developer.apple.com/thread/111693")>

Yay!

For subsequent clicks this authorisation doesn’t happen, even if I quit and relaunch the app.

IMPORTANT If you want to try this yourself make sure you have an

NSAppleEventsUsageDescription
key in your
Info.plist
. That’s required for all apps that use Apple events and are linked with the macOS 10.14 SDK or later.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"