我需要做两件事来解决我的问题:
1) 将以下内容添加到我的应用程序的授权文件中:
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
<string>com.apple.Mail</string>
</array>
2) 将以下内容添加到我的应用程序的plist中:
<key>NSAppleEventsUsageDescription</key>
<string>Please allow "your message here"</string>
这适用于Mojave 10.14(18A389)
当我在我的应用程序中运行相同的脚本时,虽然它失败了,但声明邮件应用程序没有运行(即使它正在运行):
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)
}
}
输出:
{
NSAppleScriptErrorAppName = Mail;
NSAppleScriptErrorBriefMessage = "Application isn't running.";
NSAppleScriptErrorMessage = "Mail got an error: Application isn't running.";
NSAppleScriptErrorNumber = "-600";
NSAppleScriptErrorRange = "NSRange: {33, 12}";
}
我以前从未在应用程序中执行过苹果脚本。是因为权限问题吗?