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

查看:25
本文介绍了您如何在 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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