iTunes AppObjC Swift镜像状态

问题描述 投票:-4回答:1

我有一个AppleScript到Swift桥,我的应用程序通过Swift AppDelegate调用。该项目是iTunes的控制器。按钮(例如播放/暂停,快进,快退)工作,但我不知道如何让播放/暂停按钮改变状态取决于iTunes的播放器状态(即,如果iTunes正在播放我的应用程序的播放/暂停按钮应该显示暂停符号)。这应该改变,不仅是当我点击应用程序中的按钮,而是当我与iTunes交互时。

swift itunes bridge applescript-objc
1个回答
0
投票

设置按钮或其他的初始状态,并注册iTunes分布式通知的观察者:

let center = DistributedNotificationCenter.default()
center.addObserver(self,
                   selector:#selector(updatePlayerInfo),
                   name:NSNotification.Name("com.apple.iTunes.playerInfo"),
                   object:nil)

然后使用通知的userInfo中的相应密钥进行更新:

@objc func updatePlayerInfo(_ notification: Notification) {
  print(notification.userInfo?["Player State"] as! String) // example
}

您还可以添加NSWorkspace观察器,以便在iTunes退出您或其他任何情况时收到通知。

© www.soinside.com 2019 - 2024. All rights reserved.