Working with Mountain Lion's Notification Center using PyObjC

后端 未结 1 2080
温柔的废话
温柔的废话 2020-12-29 14:09

I\'m trying to send notifications to Mountain Lion from my python script and react to clicks on the notifications. Sending the notifications works perfectly find by now. But

相关标签:
1条回答
  • 2020-12-29 14:21

    Ok, found it. Didn't run AppHelper.runEventLoop(). Obviously a facepalm mistake. The following code works:

    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"]])
    
    0 讨论(0)
提交回复
热议问题