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

如何通过Scripting Bridge获取macOS Finder当前选中项并解决权限问题

获取Finder选中文件/文件夹的Scripting Bridge权限配置问题

要获取macOS Finder中当前选中的文件/文件夹,目前只能通过Scripting Bridge实现,使用SwiftScripting预生成的扩展编写了如下示例代码:

public func getSelected() -> [NSURL] {
    guard let app = SBApplication(bundleIdentifier: "com.apple.finder") else {
        print("no app")
        return []
    }
    
    let finder = app as FinderApplication
    guard let result = finder.selection else {
            print("No items selected")
            return []
    }

    print("result", result)

    guard let selection = result.get() else {
        print("get failed: ", result.lastError() ?? "")
        return []
    }

    print("selection", selection)

    return []
}

调用selection.get()方法时失败,报错信息如下:

result <SBObject @0x600000d26310: selection of application "Finder" (6039)>
get failed:  Error Domain=NSOSStatusErrorDomain Code=-1743 "(null)" UserInfo={ErrorNumber=-1743}

该错误对应errAEEventNotPermitted,说明应用需要正确配置权限。临时例外权限无法通过App Store审核,理论上应使用scripting-targets配置,但相关文档难以查找。

已配置的entitlements如下:

<dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.files.user-selected.read-only</key>
    <true/>
    <key>com.apple.security.temporary-exception.apple-events</key>
    <array>
        <string>com.apple.finder</string>
    </array>
    <key>com.apple.security.scripting-targets</key>
    <dict>
        <key>com.apple.finder</key>
        <array>
            <string>???</string>
        </array>
    </dict> 
</dict>

此前遗漏了Info.plist中的Privacy - AppleEvents Sending Usage Description权限,添加后get()调用成功,但仍需正确配置com.apple.security.scripting-targets以通过App Store审核,现寻求Finder可用的target配置信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 19:30:39