Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Python script to send notification to the OSX Notifications Center (requires OS X 10.8+). Tested with pyobjc 2.5.1
#!/usr/bin/env python
from Foundation import NSUserNotification
from Foundation import NSUserNotificationCenter
from Foundation import NSUserNotificationDefaultSoundName
from optparse import OptionParser
def main():
parser = OptionParser(usage='%prog -t TITLE -m MESSAGE')
parser.add_option('-t', '--title', action='store', default='A title')
parser.add_option('-m', '--message', action='store', default='...')
parser.add_option('--no-sound', action='store_false', default=True,
dest='sound')
options, args = parser.parse_args()
notification = NSUserNotification.alloc().init()
notification.setTitle_(options.title)
notification.setInformativeText_(options.message)
if options.sound:
notification.setSoundName_(NSUserNotificationDefaultSoundName)
center = NSUserNotificationCenter.defaultUserNotificationCenter()
center.deliverNotification_(notification)
if __name__ == '__main__':
main()
@meoow

This comment has been minimized.

Copy link

commented Oct 12, 2014

How can I custom the icon showing in notification?
And the message doesn't wrap.

@JokerQyou

This comment has been minimized.

Copy link

commented Dec 23, 2015

This does not work for python script, but it's okay if you use it inside a PyInstaller bundled app. Now it does not work on OS X 11.11 for both situations.

@the-c0d3r

This comment has been minimized.

Copy link

commented Apr 12, 2016

Working for python script on OS X El Capitan. Icon is the default python launcher icon

@garfieldnate

This comment has been minimized.

Copy link

commented May 12, 2017

Works great on El Capitan. Make sure that notification center is turned on, though:
https://discussions.apple.com/thread/7679080?start=0&tstart=0

mathiasbynens's very popular dotfiles for Mac turn off the notification center.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.