I have a simple Swift-based macOS app that creates and executes an Apple Script:
let unreadEmailsScript = """
tell application "Mail"
get unread count of inbox
end tell
"""
if let scriptObject = NSAppleScript(source: unreadEmailsScript) {
var errorDict: NSDictionary? = nil
scriptObject.executeAndReturnError(&errorDict)
if let error = errorDict {
print(error)
}
}
I've added the following to my project's entitlements:
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
<string>com.apple.Mail</string>
</array>
When I execute this on the latest version (18A389) of Mojave the code prints:
{
NSAppleScriptErrorAppName = Mail;
NSAppleScriptErrorBriefMessage = "Not authorized to send Apple events to Mail.";
NSAppleScriptErrorMessage = "Not authorized to send Apple events to Mail.";
NSAppleScriptErrorNumber = "-1743";
NSAppleScriptErrorRange = "NSRange: {33, 12}";
}
The system doesn't prompt me for authorization at app launch.
What is the correct way to send Apple Events to Mail via a Swift app under Mojave?
TIA