Reading Currently Playing Track in MACos Using Scriptingbridge Not Working

Reading currently playing track in macOS using ScriptingBridge not working

The key thing is that you must at some point see the dialog that says

MyApp wants access to control “iTunes“. Allowing control will provide access to documents and data in “iTunes“, and to perform actions within that app.

If you have not seen that dialog:

  • In the Entitlements, turn sandboxing off.

  • In the Info.plist, add a Privacy - AppleEvents Sending Usage Description entry with some arbitrary string as its value.

Run the app. If it still doesn't work, then say this in the Terminal:

tccutil reset AppleEvents

and run the app again.

SBApplication no communication

Thanks to the @matt I found the solution - Info.plist was missing "Privacy - AppleEvents Sending Usage Description"

Scripting iTunes: playing single tracks

In Mac OS X 10.6 Snow Leopard Apple introduced the AppleScriptObjC framework which provides a much simpler bridging between AppleScript and Cocoa. No more ugly header files.

In the script classes you can mix AppleScript with an Objective-C like syntax.
There is an AppleScriptObjC template in Xcode, but you can add ASOC to an existing Cocoa project by adding the AppleScriptObjC framework and call in main.m

[[NSBundle mainBundle] loadAppleScriptObjectiveCScripts];

The Apple documentation is quite poor, there are some resources on macosxautomation.com, a comprehensive book written by Shane Stanley is available there, too.

iTunes scripting with Scripting Bridge & Sandboxing

Never mind. It turns out I was calling filteredArrayUsingPredicate: on an SBElementArray to find out the track I wanted to play and that somehow was messing things up. Now I use the method objectWithName: and it works.

AppleScript used in my cocoa mac app, stopped working in osx 10.14

You say "it worked perfectly" in previous systems. I find that difficult to believe, since almost everything about your code is wrong. I corrected your code and got your script to work, with very little difficulty.

I'll try to describe what I did.

To prepare the ground, I ran a version of your script in Script Editor (removing the backslashes and string interpolation of course):

tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari.app"
keystroke "c" using {command down}
delay 0.1
set myData to (the clipboard) as text
return myData
end tell
end tell

The script didn't run at first, but a dialog sent me to System Preferences -> Security & Privacy -> Accessibility, where I checked Script Editor and System Events.

Sample Image

Now I was ready to create the app. My app is called AnotherAppleScriptExample. In its entitlements, sandboxing is NO.

Sample Image

In its Info.plist is this entry:

Sample Image

My version of your code (fixing the various Swift mistakes) is this:

        let app = "Safari.app"
let script = """
tell application "\(app)"
activate
end tell
tell application "System Events"
tell process "\(app)"
keystroke "c" using {command down}
delay 0.1
set myData to (the clipboard) as text
return myData
end tell
end tell
"""
if let scriptObject = NSAppleScript(source: script) {
var error: NSDictionary? = nil
let result = scriptObject.executeAndReturnError(&error)
if( kAENullEvent != result.descriptorType ){
print(result.stringValue as Any)
}
}

I ran the app. I got two dialogs. First this:

Sample Image

I clicked OK. Then this:

Sample Image

I clicked Open System Preferences. In System Preferences, I checked my app (now both System Events and my app are checked):

Sample Image

Now the ground is fully prepared. I quit the app and ran it again. The script worked correctly, printing the selection in Safari.



Related Topics



Leave a reply



Submit