【问题标题】:Python, Mac OS X NSUserNotification, action buttonPython、Mac OS X NSUserNotification、操作按钮
【发布时间】:2014-03-31 13:26:13
【问题描述】:

我在理解如何让 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")

【问题讨论】:

    标签: python macos nsusernotification


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2012-07-25
      • 1970-01-01
      • 2015-04-07
      • 2010-09-11
      • 1970-01-01
      • 2014-03-14
      • 2016-12-22
      • 2012-09-05
      • 1970-01-01
      相关资源
      最近更新 更多