2012-08-30 13 views
8

Ich versuche, Benachrichtigungen von meinem Python-Skript an Mountain Lion zu senden und auf Klicks auf die Benachrichtigungen zu reagieren. Das Senden der Benachrichtigungen funktioniert jetzt einwandfrei. Aber ich konnte Lion nicht dazu bringen, mein Skript mit einem Klick zurückzurufen.Mit Mountain Lions Benachrichtigungszentrale mit PyObjC arbeiten

Hier ist was ich mache. Ich habe eine Benachrichtigungsklasse implementiert. Der einzige Zweck einer Instanz dieser Klasse ist das Bereitstellen von Benachrichtigungen durch Aufrufen von notify(). In derselben Methode setze ich das Objekt zum Delegaten der App.

import Foundation 
import objc 
import AppKit 

class MountainLionNotification(Foundation.NSObject, Notification): 

    def notify(self, title, subtitle, text, url): 
     NSUserNotification = objc.lookUpClass('NSUserNotification') 
     NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter') 
     notification = NSUserNotification.alloc().init() 
     notification.setTitle_(str(title)) 
     notification.setSubtitle_(str(subtitle)) 
     notification.setInformativeText_(str(text)) 
     notification.setSoundName_("NSUserNotificationDefaultSoundName") 
     notification.setUserInfo_({"action":"open_url", "value":url}) 
     AppKit.NSApplication.sharedApplication().setDelegate_(self) 
     NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification) 

    def applicationDidFinishLaunching_(self, sender): 
     userInfo = sender.userInfo() 
     if userInfo["action"] == "open_url": 
      import subprocess 
      subprocess.Popen(['open', userInfo["value"]]) 

Nun erwartete ich applicationDidFinishLaunching_() auf einen Klick auf die Meldung aufgerufen werden. Leider passiert das nie. Was mache ich falsch?

+0

Ich habe versucht, einen Decorator '@ objc.signature (" v @:^@ ")' zu der Delegate-Methode ohne Erfolg hinzuzufügen. – koloman

+0

Nun habe ich auch versucht, mein 'MountainLionNotification' -Objekt zum Delegierten des Standardbenachrichtigungszentrums zu machen und die Methode' userNotificationCenter_didActivateNotification _() 'zu implementieren. Immer noch kein Erfolg! – koloman

+0

Hey, konnten Sie Benachrichtigungen erhalten, die nur von einem Python-Skript/Interpreter angezeigt werden, ohne die Ereignisschleife zu starten? Ich kann nicht scheinen, sogar Mitteilungen zu erhalten, die den Code über – GP89

Antwort

8

Ok, gefunden. Ich habe nicht ausgeführt. Offensichtlich ein Facepalm-Fehler. Der folgende Code funktioniert:

class MountainLionNotification(Foundation.NSObject, Notification): 

    def notify(self, title, subtitle, text, url): 
     NSUserNotification = objc.lookUpClass('NSUserNotification') 
     NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter') 
     notification = NSUserNotification.alloc().init() 
     notification.setTitle_(str(title)) 
     notification.setSubtitle_(str(subtitle)) 
     notification.setInformativeText_(str(text)) 
     notification.setSoundName_("NSUserNotificationDefaultSoundName") 
     notification.setHasActionButton_(True) 
     notification.setOtherButtonTitle_("View") 
     notification.setUserInfo_({"action":"open_url", "value":url}) 
     NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self) 
     NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification) 

    def userNotificationCenter_didActivateNotification_(self, center, notification): 
     userInfo = notification.userInfo() 
     if userInfo["action"] == "open_url": 
      import subprocess 
      subprocess.Popen(['open', userInfo["value"]])