【问题标题】:Working with Mountain Lion's Notification Center using PyObjC使用 PyObjC 与 Mountain Lion 的通知中心合作
【发布时间】:2012-08-25 13:00:21
【问题描述】:

我正在尝试从我的 python 脚本向 Mountain Lion 发送通知,并对点击通知做出反应。现在可以完美地找到发送通知。但是我无法让 Lion 在单击时回调我的脚本。

这就是我所做的。我实现了一个通知类。该类实例的唯一目的是通过调用notify() 来提供通知。在相同的方法中,我将对象设置为应用程序的委托。

import Foundation
import objc
import AppKit

class MountainLionNotification(Foundation.NSObject, Notification):

    def notify(self, title, subtitle, text, url):
        NSUserNotification = objc.lookUpClass('NSUserNotification')
        NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
        notification = NSUserNotification.alloc().init()
        notification.setTitle_(str(title))
        notification.setSubtitle_(str(subtitle))
        notification.setInformativeText_(str(text))
        notification.setSoundName_("NSUserNotificationDefaultSoundName")
        notification.setUserInfo_({"action":"open_url", "value":url})
        AppKit.NSApplication.sharedApplication().setDelegate_(self)
        NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)

    def applicationDidFinishLaunching_(self, sender):
        userInfo = sender.userInfo()
        if userInfo["action"] == "open_url":
            import subprocess
            subprocess.Popen(['open', userInfo["value"]])

现在我希望在单击通知时调用applicationDidFinishLaunching_()。不幸的是,这永远不会发生。我做错了什么?

【问题讨论】:

  • 我尝试在委托方法中添加一个装饰器@objc.signature("v@:^@"),但没有成功。
  • 现在我还尝试将我的MountainLionNotification 对象设置为默认通知中心的委托并实现协议userNotificationCenter_didActivateNotification_() 方法。还是没有成功!
  • 嘿,您是否能够在不启动事件循环的情况下仅从 python 脚本/解释器获取要显示的通知?我似乎什至无法使用上面的代码显示通知
  • @GP89 ​​- 你肯定需要启动事件循环;没有办法。
  • @koloman 你从哪里得到通知类 MountainLionNotification(Foundation.NSObject, Notification) ?

标签: python osx-mountain-lion pyobjc nsusernotification


【解决方案1】:

好的,找到了。没有运行AppHelper.runEventLoop()。很明显是面相错误。以下代码有效:

class MountainLionNotification(Foundation.NSObject, Notification):

    def notify(self, title, subtitle, text, url):
        NSUserNotification = objc.lookUpClass('NSUserNotification')
        NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
        notification = NSUserNotification.alloc().init()
        notification.setTitle_(str(title))
        notification.setSubtitle_(str(subtitle))
        notification.setInformativeText_(str(text))
        notification.setSoundName_("NSUserNotificationDefaultSoundName")
        notification.setHasActionButton_(True)
        notification.setOtherButtonTitle_("View")
        notification.setUserInfo_({"action":"open_url", "value":url})
        NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
        NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)

    def userNotificationCenter_didActivateNotification_(self, center, notification):
        userInfo = notification.userInfo()
        if userInfo["action"] == "open_url":
            import subprocess
            subprocess.Popen(['open', userInfo["value"]])

【讨论】:

  • 我知道这是旧的,但我得到了错误:objc.BadPrototypeError: Objective-C expects 1 arguments, Python argument has 5 arguments for <unbound selector notify of MountainLionNotification at 0x10bf48650>Notification is not defined。你知道这些能不能解决吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 2012-07-27
  • 2012-07-21
  • 2013-08-15
  • 2012-07-17
  • 1970-01-01
相关资源
最近更新 更多