如何在Mac上从iTunes收听通知(使用NSDistributedNotificationCenter) [英] How do you listen to notifications from iTunes on a Mac (Using the NSDistributedNotificationCenter)

查看:116
本文介绍了如何在Mac上从iTunes收听通知(使用NSDistributedNotificationCenter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python收听 py-objc lib是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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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