我正在尝试在我的 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>
然后我这样调用 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)
但失败并出现错误 -10004
:iTunes 出现错误:发生权限冲突。
我尝试了两种方式(itunes
和 iTunes
)大写 iTunes,但似乎没有效果。我还尝试在读写权限之上添加读取权限。最后,我尝试添加对用户音乐文件夹(存储 iTunes 库的位置)的读写访问权限,但这也没有帮助。
为了编写 iTunes 脚本,是否还需要我不知道的其他权利?
我在搜索解决方案时找到了此链接 ( link ),但它要求用户在其 Library
文件夹中选择特定文件夹以授予应用程序用户选择的文件访问权限,然后它要求脚本位于一个单独的文件中,这对于我想做的事情来说太多了。我不信任用户对脚本文件的管理,并且我不需要 1 行 AppleScript 代码的文件。据我了解,另一个缺点是 NSUserAppleScriptTask
是您无法在多个调用中保留脚本的状态,这对我来说不是问题,但对其他人来说可能是问题。
谢谢
最佳答案
联系苹果开发者技术支持后,我解决了这个问题,实际上是两个问题合二为一。
首先,权利需要正确设置 bundle 标识符的大小写。对于 iTunes,权利必须如下所示(注意 iTunes 中的大写 T
):
<key>com.apple.security.scripting-targets</key>
<dict>
<key>com.apple.iTunes</key>
<array>
<string>com.apple.iTunes.library.read-write</string>
</array>
</dict>
然后,除非 AppleScript 代码在末尾包含 of source 1
,如下所示,否则就会发生权限违规:
tell application "iTunes" to add (POSIX file "your/path/to/mp3.mp3") to library playlist 1 of source 1
给你!
关于swift - 通过 NSAppleScript 编写 iTunes 脚本的沙盒权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34119076/