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"