8
import ScriptingBridge

class iTunesAccess {
    static func requestAccess() -> Bool {
        guard #available(OSX 10.14, *) else {
            return true
        }
        if var addressDesc = NSAppleEventDescriptor(bundleIdentifier: "com.apple.iTunes").aeDesc?.pointee {
            let appleScriptPermission = AEDeterminePermissionToAutomateTarget(&addressDesc, typeWildCard, typeWildCard, true)
            AEDisposeDesc(&addressDesc)
            return appleScriptPermission == noErr
        }
        return false
    }
}

info.plist:

<key>NSAppleEventsUsageDescription</key>
<string>somedescriprtion</string>

iTunes running but i always get -600 osstatus. How i can fix it? iTunes bundle id is fine.

/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/iTunes.app/Contents/Info.plist
com.apple.iTunes

P.S but if i using "com.apple.dt.Xcode" bundle id it works!

P.P.S i found repo https://github.com/melchor629/iTunes-Scrobbler and build it. It works too.

Horoko
  • 134
  • 10
  • 1
    Is your app sandboxed? If so, you'll also need an entitlement to send Apple events to iTunes. – Chris N Oct 22 '18 at 19:40
  • Omg @ChrisN. I completely forgot to do it! Do this as an answer, please, so that I can mark the issue resolved. – Horoko Oct 23 '18 at 13:54

1 Answers1

5

Good call on the NSAppleEventsUsageDescription key — that’s required if you link against the 10.14 SDK — but if your app is sandboxed, you’ll also need an appropriate Apple event entitlement: com.apple.security.scripting-targets if you can, or com.apple.security.temporary-exception.apple-events if you must. See https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/AppSandboxTemporaryExceptionEntitlements.html for more details, including a way to specify both entitlements, but only one applies depending on the current OS version.

Chris N
  • 905
  • 5
  • 10