Skip to content

Instantly share code, notes, and snippets.

@TranceCat
Created January 19, 2015 12:39
Show Gist options
  • Save TranceCat/1eae16bc63feda2824b7 to your computer and use it in GitHub Desktop.
Save TranceCat/1eae16bc63feda2824b7 to your computer and use it in GitHub Desktop.
Example of pyobjc implementation for Notification Center on OS X with reply option
#!/usr/bin/env python
from Foundation import NSUserNotification, NSUserNotificationCenter, NSObject
from PyObjCTools import AppHelper
'''
This class is used for NSUserNotificationCenter alert. User needs to select Alerts style
in System Preferences -> Notifications for the python script.
'''
class Notification(NSObject):
def notify(self, title, subtitle, text):
notification = NSUserNotification.alloc().init()
if title : notification.setTitle_(str(title))
notification.setSubtitle_(str(subtitle))
notification.setInformativeText_(str(text))
notification.setHasReplyButton_ (1)
# Use this if sound is needed
#notification.setSoundName_("NSUserNotificationDefaultSoundName")
NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
def userNotificationCenter_didActivateNotification_(self, center, notification):
alertType = 3 # activationType from Apple documentation
if notification.activationType() is alertType:
response = notification.response()
print(response.string())
# Stop the loop
AppHelper.stopEventLoop()
def main():
notify_obj = Notification.alloc().init()
notify_obj.notify('Title','Subtitle','Some nice text')
# Loop for delegate call
AppHelper.runConsoleEventLoop()
# Do other stuff here. Like:
print('Done')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment