python - 使用 PyObjC 处理 Mountain Lion 的通知中心

标签 python osx-mountain-lion pyobjc nsusernotification

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

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

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_()。不幸的是,这永远不会发生。我做错了什么?

最佳答案

好的,找到了。没有运行 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"]])

关于python - 使用 PyObjC 处理 Mountain Lion 的通知中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12202983/

相关文章:

python-3.x - 在 Mac OS 10.8 上的 Anaconda 中更改默认 Python 环境(从 2.7 到 3.3)

ruby-on-rails-3 - Mountain Lion - Rails - 未找到符号 : _rb_Digest_MD5_Finish (LoadError)

python - 如何创建 Cocoa 库并在 python 中使用它

python - python.org 版本能否与 Apple 框架构建兼容?

iphone - 计算本周的第一天和最后一天

python - 如何在 Django 中管理 Apache Spark 上下文?

python - 我通过 open cv2 进行人脸检测时不断收到此错误

python - 子进程运行 scp 身份文件不可访问

python - 使用 NumPy 将一个数组与另一个数组建立索引

svn - Mac SVN E175002 : Could not resolve hostname `'