无法在Xcode中运行AppleScript,报错Not authorized to send Apple events to Finder.(-1743)
解决Monterey下Xcode中AppleScript访问Finder的权限错误(-1743)
问题场景
在Xcode中使用NSAppleScript执行脚本访问Finder时,出现权限错误:
let appleScript = NSAppleScript(source: script) var error: NSDictionary? if let outputString = appleScript?.executeAndReturnError(&error).stringValue { print(outputString) } else if (error != nil) { print("error: \(error!)") }
错误信息:
error : { NSAppleScriptErrorAppName = Finder; NSAppleScriptErrorBriefMessage = "Not authorized to send Apple events to Finder."; NSAppleScriptErrorMessage = "Not authorized to send Apple events to Finder."; NSAppleScriptErrorNumber = "-1743" NSAppleScriptErrorRange = "NSRange: {63,1O}" }
已尝试设置Entitlements中Apple Events为YES,但问题仍存在(Monterey系统)。
解决建议
检查系统隐私自动化权限
打开「系统偏好设置 > 安全性与隐私 > 隐私 > 自动化」,找到你的应用,确保Finder对应的复选框已勾选。如果应用未出现在列表中,先运行一次触发权限请求,或右键点击列表空白处选择「添加」手动导入应用。完善Entitlements配置
仅设置com.apple.security.apple-events为YES在Monterey及后续系统中可能不足,需明确指定允许访问的目标应用ID。修改Entitlements文件:<key>com.apple.security.apple-events</key> <true/> <key>com.apple.security.temporary-exception.apple-events</key> <array> <string>com.apple.finder</string> </array>注意:
com.apple.finder是Finder的Bundle ID,若脚本访问其他应用,需添加对应ID。重置应用权限数据库
若权限状态混乱,执行终端命令重置应用的Apple Events权限:tccutil reset AppleEvents com.your-app-bundle-id替换
com.your-app-bundle-id为你应用的Bundle ID(可在Xcode项目设置「标识」中查看)。清理Xcode缓存并重建
- 关闭Xcode,删除Derived Data:Xcode > 偏好设置 > 位置 > 点击Derived Data旁的箭头,删除对应文件夹
- 重新打开项目,按
Cmd+Shift+K清理构建文件夹,再重新构建运行。
确保权限请求触发
若首次运行未弹出权限请求弹窗,大概率是之前手动拒绝过权限。此时需通过上述「重置权限数据库」或在隐私设置中手动开启权限后,重新运行应用。
内容的提问来源于stack exchange,提问作者fantasticproposal
相关产品推荐
相关产品推荐

