How do you listen to notifications from iTunes on a Mac (Using the NSDistributedNotificationCenter)(您如何在 Mac 上收听来自 iTunes 的通知(使用 NSDistributedNotificationCenter))
问题描述
寻找使用python听的帮助/教程/示例代码分布式通知 来自 Mac 上的应用程序.我知道 py-objc 库是 python 和 mac/cocoa 类之间的桥梁,并且 Foundation 库可以用于添加观察者,但正在寻找有关如何使用它来监视 iTunes 的示例或教程.
Looking for help/tutorials/sample code of using python to listen to distributed notifications from applications on a mac. I know the py-objc lib is the bridge between python and mac/cocoa classes, and the Foundation library can be used to add observers, but looking for examples or tutorials on how to use this to monitor iTunes.
推荐答案
如果有人问这个问题,我想出了如何倾听,下面的代码有效.然而,访问属性似乎不像标准的 python 属性访问那样工作.
If anyone comes by to this question, i figured out how to listen, the code below works. However accessing attributes do not seem to work like standard python attribute access.
更新:您不像在 python 中那样访问属性,即 (.x),下面的代码已更新,它现在生成一个名为 song_details 的字典.
Update: you do not access attributes as you would in python i.e (.x), the code has been updated below, it now generates a dict called song_details.
Update3:更新代码,现在继承 NSObject,删除从类中添加 addObserver.将在 github 上更新代码,这里不再更新.
Update3: Update to the code, now subclassing NSObject, removed adding the addObserver from the class. Will keep the code updated on github, no more updates here.
import Foundation
from AppKit import *
from PyObjCTools import AppHelper
class GetSongs(NSObject):
def getMySongs_(self, song):
song_details = {}
ui = song.userInfo()
for x in ui:
song_details[x] = ui.objectForKey_(x)
print song_details
nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
GetSongs = GetSongs.new()
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.iTunes.playerInfo',None)
NSLog("Listening for new tunes....")
AppHelper.runConsoleEventLoop()
这篇关于您如何在 Mac 上收听来自 iTunes 的通知(使用 NSDistributedNotificationCenter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:您如何在 Mac 上收听来自 iTunes 的通知(使用 NSDi


- YouTube API v3 返回截断的观看记录 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01