我遇到了一些困难,不理解如何使用Python使NSUserNotification的操作按钮执行操作。 我认为当用户点击按钮时,“userNotificationCenter_didActivateNotification_”被调用?但我想不出如何接收通知并使用它来调用自定义方法-我只是使用os.system(“say”)进行测试。我尝试将类的代理设置为self,但似乎没有任何区别。任何想法都将不胜感激! Adam。这是我作为一个类拥有的东西,当完成某些事情时,我会从另一个脚本中调用它:
#!/usr/bin/env python
import Foundation, objc
import AppKit
import sys
import os
import logging
from Foundation import NSUserNotification
from Foundation import NSUserNotificationCenter
from optparse import OptionParser
class Notification_osx:
def __init__(self, item,proyecto):
self.item = item
notification = NSUserNotification.alloc().init()
notification.setTitle_("Notification: " + proyecto)
notification.setSubtitle_("Un archivo nuevo esta disponible:")
notification.setInformativeText_(item)
notification.setHasActionButton_(True);
notification.setActionButtonTitle_("Donde?")
home = os.path.expanduser("~")
LOG_FILENAME = os.path.join(home,'Desktop','sync.log')
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug(notification.description)
# notification.setUserInfo_({"action":"open_url", "value":item})
# if options.sound:
# notification.setSoundName_("NSUserNotificationDefaultSoundName")
center = NSUserNotificationCenter.defaultUserNotificationCenter()
center.setDelegate_(self)
center.deliverNotification_(notification)
# center.didActivateNotification_(notification)
def userNotificationCenter_shouldPresentNotification_(self, center, notification):
os.system("say hola")
return True
def userNotificationCenter_didDeliverNotification_(self, center, notification):
os.system("say hola")
def userNotificationCenter_didActivateNotification_(self, center, notification):
# userInfo = notification.userInfo()
os.system("say hola")