Python,Mac OS X NSUserNotification,操作按钮

3 投票
1 回答
1446 浏览
提问于 2025-04-18 00:40

我在理解如何让NSUserNotification的操作按钮在Python中执行某些操作时遇到了一些困难。我觉得我明白“userNotificationCenter_didActivateNotification_”是在用户点击按钮时被调用的,对吧?

但是我不知道怎么接收这个通知并用它来调用一个自定义的方法——我只是用os.system("say")来做测试。

我尝试把类的委托设置为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 个回答

1

我用这段代码成功实现了通知的激活,不过不太确定这是否能回答你的问题,因为你似乎有一个操作按钮。

import Foundation, objc

NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
NSObject = objc.lookUpClass('NSObject')


class NotificationDelegator(NSObject):

    def userNotificationCenter_didActivateNotification_(self, center, notification):
        print "user notification center"

    def userNotificationCenter_shouldPresentNotification_(self, center, notification):
        return True

delegator = NotificationDelegator.alloc().init()


def notify(title, subtitle, info_text, delay=1, 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")
    center = NSUserNotificationCenter.defaultUserNotificationCenter()
    center.setDelegate_(delegator)
    center.deliverNotification_(notification)

撰写回答