2012-08-30 12 views
8

Estoy tratando de enviar notificaciones a Mountain Lion desde mi script python y reacciono a los clics en las notificaciones. El envío de notificaciones funciona perfectamente ahora. Sin embargo, no pude lograr que Lion llamara mi script con solo un clic.Trabajando con Mountain Lion's Notification Center usando PyObjC

Esto es lo que hago. Implementé una clase de notificación. El único propósito de una instancia de esa clase es proporcionar notificaciones invocando notify(). En el mismo método, configuro el objeto delegado de la aplicación.

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"]]) 

Ahora me espera applicationDidFinishLaunching_() a ser llamados a un clic en la notificación. Desafortunadamente eso nunca sucede. ¿Qué estoy haciendo mal?

+0

He tratado de agregar un decorador '@ objc.signature (" v @:^@ ")' al método de delegado, sin éxito. – koloman

+0

Ahora también intenté configurar mi objeto 'MountainLionNotification' como el delegado del centro de notificación predeterminado e implementar el método' userNotificationCenter_didActivateNotification _() 'de los protocolos. ¡Sin éxito! – koloman

+0

Oye, ¿fue posible que las notificaciones se mostraran solo con un script/intérprete de Python sin iniciar el ciclo de evento? Parece que ni siquiera puedo obtener notificaciones mostrando usando el código de arriba – GP89

Respuesta

8

Ok, lo encontramos. No ejecutó AppHelper.runEventLoop(). Obviamente un error de facepalm. El siguiente código funciona:

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"]])