Python post osx notification

后端 未结 7 1176
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 07:08

Using python I am wanting to post a message to the OSX Notification Center. What library do I need to use? should i write a program in objective-c and then call that progra

7条回答
  •  时光说笑
    2020-12-04 07:19

    Here is a way (You need the Foundation module):

    from Foundation import NSUserNotification
    from Foundation import NSUserNotificationCenter
    from Foundation import NSUserNotificationDefaultSoundName
    
    
    class Notification():
        def notify(self, _title, _message, _sound = False):
            self._title = _title
            self._message = _message
            self._sound = _sound
    
            self.notification = NSUserNotification.alloc().init()
            self.notification.setTitle_(self._title)
            self.notification.setInformativeText_(self._message)
            if self._sound == True:
                self.notification.setSoundName_(NSUserNotificationDefaultSoundName)
    
            center = NSUserNotificationCenter.defaultUserNotificationCenter()
            center.deliverNotification_(self.notification)
    
    N = Notification()
    N.notify(_title="SOME", _message="Something", _sound=True)
    

    This works only for MAC. Hope you enjoy!

提交回复
热议问题