使用ScriptingBridge无法读取macOS中当前播放的曲目 [英] Reading currently playing track in macOS using ScriptingBridge not working

查看:153
本文介绍了使用ScriptingBridge无法读取macOS中当前播放的曲目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ScriptingBridge阅读iTunes当前正在播放的曲目. 我在这里找到了其他两个线程,但似乎没有一个对我有用(macOS Mojave和iTunes 12.9.4).

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).

我使用以下命令创建了iTunes.h文件:

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

currentTrack为零,但iTunes正在播放歌曲.我在这里想念什么?我已经尝试禁用沙箱,但无济于事.我正在使用macOS Mojave,Xcode 10.2和iTunes 12.9.4. 另外,当iTunes未运行时,它将启动吗?为什么?

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?

是否会有另一种方法来获取iTunes当前正在播放的歌曲?

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

推荐答案

关键是您必须在某个时候看到显示以下内容的对话框

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

MyApp希望访问以控制"iTunes".允许控制将提供对"iTunes"中文档和数据的访问,并在该应用程序内执行操作.

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:

  • 在权利"中,关闭沙箱 .

Info.plist 中,添加一个隐私-AppleEvents发送使用情况说明"条目,并使用任意字符串作为其值.

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.

这篇关于使用ScriptingBridge无法读取macOS中当前播放的曲目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆