Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

swift - Reading currently playing track in macOS using ScriptingBridge not working

I am trying to read the currently playing track of iTunes using ScriptingBridge. I found a couple of other threads here but no one seems to be working for me (macOS Mojave and iTunes 12.9.4).

I have created the iTunes.h file using the following command:

sdef /Applications/iTunes.app | sdp -fh --basename "iTunes" 

I am creating an instance of the Scripting Bridge as follows:

if let itunes: AnyObject = SBApplication(bundleIdentifier: "com.apple.iTunes")
{
  if itunes.isRunning
  {
    Swift.print("iTunes is running...")
  }

  Swift.print(itunes.currentTrack?.name)
  Swift.print(itunes.currentTrack?.album)
}

The code compiles fine. When I run the code, I get the following output:

iTunes is running...
nil
nil

The currentTrack is nil, but iTunes is playing a song. What am I missing here? I already tried disabling the sandbox, but to no avail. I am using macOS Mojave, Xcode 10.2 and iTunes 12.9.4. Also, when iTunes is not running, it will be started? Why?

Would there be another way to get the currently playing song of iTunes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...