字数
472 字
阅读时间
3 分钟
应用程序未在运行 错误:
txt
{
NSAppleScriptErrorAppName = "Google Chrome";
NSAppleScriptErrorBriefMessage = "Application isn\U2019t running.";
NSAppleScriptErrorMessage = "Google Chrome got an error: Application isn\U2019t running.";
NSAppleScriptErrorNumber = "-600";
NSAppleScriptErrorRange = "NSRange: {40, 3}";
}
出现这个错误,是由两个系统级限制导致的:
此处相关需要注意的属性和字段是:NSAppleEventsUsageDescription
(Apple 开发者官方文档)
- 可以通过临时添加权限许可来处理[4]:
xml
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
<string>com.apple.Mail</string>
</array>
在 <项目名称>.entitlements
文件中添加字段 com.apple.security.temporary-exception.apple-events
,Type(类型)设定为 Array
(数组):
创建好之后在点一下字段名左手边箭头以展开编辑,然后在每一行的 Value
(值)中填写需要获取的 App 权限的 appID
(比如 Safari 是 com.apple.Safari
)
添加许可以后重新运行,发现新的错误:
txt
{
NSAppleScriptErrorAppName = "Google Chrome";
NSAppleScriptErrorBriefMessage = "Not authorized to send Apple events to Google Chrome.";
NSAppleScriptErrorMessage = "Not authorized to send Apple events to Google Chrome.";
NSAppleScriptErrorNumber = "-1743";
NSAppleScriptErrorRange = "NSRange: {43, 3}";
}
c# - ScriptError: Visual Studio 2017 macOS 版本运行代码时提示:发送 Apple Events 到 Terminal 未授权 - Stack Overflow
xcode - 尝试在 Swift mac os App 中集成 Apple Script 以获取 macOS 上应用程序的 gui 元素 - Stack Overflow
具体可以参考一下这篇文章: Executing AppleScript in a Mac app on macOS Mojave and dealing with AppleEvent sandboxing