我正在尝试通过 python 连接到 Mountain Lion 通知中心。我已经安装了 pyobjc 并按照此处和此处的说明进行操作。另请参阅:使用 PyObjC 处理 Mountain Lion 的通知中心
这是我的代码:
import Foundation, objc
import AppKit
import sys
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
""" Python method to show a desktop notification on Mountain Lion. Where:
title: Title of notification
subtitle: Subtitle of notification
info_text: Informative text of notification
delay: Delay (in seconds) before showing the notification
sound: Play the default notification sound
userInfo: a dictionary that can be used to handle clicks in your
app's applicationDidFinishLaunching:aNotification method
"""
notification = NSUserNotification.alloc().init()
notification.setTitle_(title)
notification.setSubtitle_(subtitle)
notification.setInformativeText_(info_text)
notification.setUserInfo_(userInfo)
if sound:
notification.setSoundName_("NSUserNotificationDefaultSoundName")
notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
当我用参数调用通知函数时,我得到一个属性错误:
AttributeError: 'NoneType' object has no attribute 'scheduleNotification_'
我不明白为什么NSUserNotificationCenter.defaultUserNotificationCenter()
要返回 NoneType 对象。我无法在互联网或 SO 上查询有关此事的任何内容。