You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS Catalina下Swift调用AppleScript遇Finder错误-600及通知问题

解决AppleScriptObjC中Finder对话框不显示及通知失效问题

针对你在MacOS Catalina 10.15.1 + Swift + Xcode环境下遇到的问题,我来拆解原因和对应的解决方案:

一、Finder对话框报错(error -600)的解决

错误Finder got an error: Application isn’t running. (error -600)看似矛盾——毕竟Finder默认后台运行,但本质是App Sandbox权限限制导致你的应用无法访问系统级的Finder进程。

快速测试方案:关闭App Sandbox

  1. 打开Xcode项目,进入Signing & Capabilities标签页
  2. 找到App Sandbox选项,取消勾选
  3. 重新运行应用,调用testDialog()后Finder对话框应该能正常弹出

适配上架的方案:配置Sandbox权限

如果你的应用需要上架App Store,不能关闭Sandbox,可以尝试:

  • 在App Sandbox的File Access区域,添加System Files的Read/Write权限(注意:苹果审核可能要求你说明权限用途)
  • 在AppleScript代码里先显式激活Finder,确保进程能被应用识别:
to testDialog()
    log "This is log msg."
    -- 显式激活Finder,解决Sandbox下的进程访问问题
    activate application "Finder"
    tell application "Finder"
        display dialog "Hello World"
    end tell
    -- 后续通知代码...
end testDialog

二、通知无法工作的解决

AppleScript原生的display notification命令默认绑定到脚本编辑器的上下文,而非你的macOS应用,所以在AppleScriptObjC环境下会失效。我们需要改用Cocoa的通知API来发送通知:

修改AppleScript代码,使用NSUserNotificationCenter

script test
    property parent : class "NSObject"
    to testDialog()
        log "This is log msg."
        activate application "Finder"
        tell application "Finder"
            display dialog "Hello World"
        end tell
        
        -- 改用Cocoa通知API,适配当前应用上下文
        set notification to current application's NSUserNotification's alloc()'s init()
        notification's setTitle:"This is notification Title"
        notification's setInformativeText:"This is notification Content"
        current application's NSUserNotificationCenter's defaultUserNotificationCenter()'s deliverNotification:notification
    end testDialog
end script

确保应用请求了通知权限

在Swift代码中,需要主动向系统请求通知权限,否则通知会被拦截:

import UserNotifications

// 在AppDelegate的applicationDidFinishLaunching方法中添加:
func applicationDidFinishLaunching(_ aNotification: Notification) {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
        if let error = error {
            print("通知权限请求失败:\(error.localizedDescription)")
        } else if granted {
            print("通知权限已授权")
        }
    }
}

额外检查:开启Sandbox下的通知权限

如果你的应用开启了App Sandbox,需要在Signing & Capabilities的App Sandbox里,勾选Notifications选项,确保通知能正常发送。

内容的提问来源于stack exchange,提问作者Artis24106

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:34:09