如何在Mac上监听iTunes通知(使用NSDistributedNotificationCenter)
2 个回答
4
GrowlTunes的源代码可能会给你一些线索。你需要把它从Objective-C转换成PyObjC,不过没关系,随便啦。 :)
GrowlTurnesController.m (或者你可以下载整个Growl的源代码,然后找到GrowlTunes,这样你就能看到它的实际运行情况了。: 这里有获取源代码的说明链接)
12
如果有人看到这个问题,我找到了如何监听的方法,下面的代码可以正常工作。不过,访问属性的方式似乎和标准的Python属性访问不太一样。
更新:你不能像在Python中那样访问属性,也就是不能用(.x)的方式,下面的代码已经更新,现在生成了一个叫做song_details的字典。
更新3:代码又更新了,现在是子类化NSObject,去掉了从类中添加addObserver的部分。会在github上保持代码更新,这里就不再更新了。
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()