【问题标题】:How do you listen to notifications from iTunes on a Mac (Using the NSDistributedNotificationCenter)你如何在 Mac 上收听来自 iTunes 的通知(使用 NSDistributedNotificationCenter)
【发布时间】:2009-12-19 14:59:38
【问题描述】:

寻找帮助/教程/示例代码使用 python 收听来自 Mac 上的应用程序的distributed notifications。我知道py-objc lib 是 python 和 mac/cocoa 类之间的桥梁,Foundation 库可用于添加观察者,但正在寻找有关如何使用它来监控 iTunes 的示例或教程。

【问题讨论】:

  • 这些天 iTunes 真的会发布分布式通知吗?
  • 是的……在 com.apple.iTunes.player 下,使用通知手表我可以看到它们。

标签: python macos notifications pyobjc


【解决方案1】:

如果有人提出这个问题,我想出了如何听,下面的代码有效。然而,访问属性似乎不像标准的 python 属性访问那样工作。

更新:您不像在 python 中那样访问属性,即 (.x),代码已在下面更新,它现在生成一个名为 song_details 的字典。

Update3:更新代码,现在是 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()

【讨论】:

    【解决方案2】:

    GrowlTunes 的源代码可能会在这里为您提供一些线索。您必须从 Objective-C 转换为 PyObjC,但是,无论如何。 :)

    GrowlTurnesController.m (或者抓取整个咆哮源树并导航到 GrowlTunes,这样您就可以看到它的全部运行情况。:here's a link to the directions on how to get the source

    【讨论】:

    • 感谢您的帮助,设法通过pyobjc网站和苹果网站解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多