Sandbox entitlement to script iTunes via NSAppleScript

荒凉一梦 提交于 2019-12-11 12:03:30

问题


I'm trying to script iTunes from NSAppleScript in my Cocoa app in order to add a file to the library.

In my entitlements file, I added the following:

<key>com.apple.security.scripting-targets</key>
    <dict>
        <key>com.apple.itunes</key>
        <array>
            <string>com.apple.itunes.library.read-write</string>
        </array>
    </dict>

I then call the AppleScript like this:

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)

but it fails with an error -10004: iTunes got an error: A privilege violation occurred.

I tried capitalizing iTunes both ways (itunes and iTunes) but nothing seems to work. I also tried adding, on top of the read-write entitlement, the read entitlement. Finally, I tried adding read and write access to the user's Music folder (where the iTunes library is stored) and this didn't help either.

Is there another required entitlement that I'm not aware of in order to script iTunes?

I found this link in my search for a solution (link) but it requires the user to select a specific folder in his Library folder to give the application user-selected file access and then it requires the script to be in a separate file which is 2 things too many for what I want to do. I don't trust the user regarding the management of the script files and I don't need a file for 1 line of AppleScript code. Another disadvantage, as I understand NSUserAppleScriptTask is that you can't persist a script's state across multiple calls, which is not a problem in my case but could be for someone else.

Thanks


回答1:


After contacting the Developer Technical Support of Apple, I was able to solve this problem, which is actually 2 problems in one.

First, the entitlements need to have the bundle identifiers properly cased. For iTunes, the entitlements must be like this (note the capital T in 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>

Then, the privilege violation will occur unless the AppleScript code includes of source 1 at the end like this :

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

There you go!



来源:https://stackoverflow.com/questions/34119076/sandbox-entitlement-to-script-itunes-via-nsapplescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!