通过 NSAppleScript 编写 iTunes 脚本的沙盒权限

bee*_*eeb 2 cocoa applescript appstore-sandbox nsapplescript swift

我正在尝试在 Cocoa 应用程序中从 NSAppleScript 编写 iTunes 脚本,以便将文件添加到库中。

在我的权利文件中,我添加了以下内容:

<key>com.apple.security.scripting-targets</key>
    <dict>
        <key>com.apple.itunes</key>
        <array>
            <string>com.apple.itunes.library.read-write</string>
        </array>
    </dict>
Run Code Online (Sandbox Code Playgroud)

然后我这样调用 AppleScript:

var error: NSDictionary? = nil
let appleScript = NSAppleScript(source: "tell application \"iTunes\" to add (POSIX file \"\(path)\") to library playlist 1")
let result = appleScript?.executeAndReturnError(&error)
Run Code Online (Sandbox Code Playgroud)

但它失败并出现错误-10004:iTunes got an error: A privilege violation occurred.

itunes我尝试以两种方式(和)大写 iTunes iTunes,但似乎没有任何效果。我还尝试在读写权利之上添加read权利。最后,我尝试添加对用户音乐文件夹(存储 iTunes 库的位置)的读写访问权限,但这也没有帮助。

为了编写 iTunes 脚本,是否还需要我不知道的其他权利?

我在搜索解决方案时找到了此链接(链接),但它要求用户在其文件夹中选择一个特定文件夹,Library以授予应用程序用户选择的文件访问权限,然后要求脚本位于一个单独的文件中,该文件为 2对于我想做的事情来说太多了。我不信任用户对脚本文件的管理,并且我不需要 1 行 AppleScript 代码的文件。据我了解,另一个缺点NSUserAppleScriptTask是您无法在多个调用中保留脚本的状态,这对我来说不是问题,但对其他人来说可能是问题。

谢谢

bee*_*eeb 5

在联系Apple的开发者技术支持后,我能够解决这个问题,这实际上是两个问题合二为一。

首先,权利需要正确设置捆绑包标识符的大小写。对于 iTunes,权限必须如下所示(注意TiTunes 中的大写字母):

<key>com.apple.security.scripting-targets</key>
<dict>
    <key>com.apple.iTunes</key>
    <array>
        <string>com.apple.iTunes.library.read-write</string>
    </array>
</dict>
Run Code Online (Sandbox Code Playgroud)

然后,除非 AppleScript 代码of source 1在末尾包含如下内容,否则就会发生特权冲突:

tell application "iTunes" to add (POSIX file "your/path/to/mp3.mp3") to library playlist 1 of source 1
Run Code Online (Sandbox Code Playgroud)

就这样吧!