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?
He tratado de agregar un decorador '@ objc.signature (" v @:^@ ")' al método de delegado, sin éxito. – koloman
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
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