Python,Mac OS X NSUserNotification,动作按钮

时间:2014-03-31 13:26:14

标签: python macos nsusernotification

我在理解如何使用Python获取NSUserNotification的操作按钮时遇到一些麻烦。我想我明白当用户点击按钮时会调用“userNotificationCenter_didActivateNotification_”吗?

但我无法弄清楚如何接收通知并使用它来调用自定义方法 - 我正在使用对os.system的调用(“说”)作为测试

我已经尝试将类的委托设置为self,但似乎没有什么区别。

任何想法都将不胜感激!

干杯

亚当

这就是我在完成某些事情时从另一个脚本调用的类:

#!/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")

1 个答案:

答案 0 :(得分:1)

我能够通过此代码激活通知,不确定它是否回答您的问题,因为您似乎有一个操作按钮

class Publisher(models.Model):
    name = models.CharField(max_length = 30)
    address = models.CharField(max_length = 50)
    city = models.CharField(max_length = 60)
    state_province = models.CharField(max_length = 30)
    country = models.CharField(max_length = 50)
    website = models.URLField()

    def __str__(self):
        return self.name