Skip to content

Instantly share code, notes, and snippets.

@baliw
Created November 5, 2012 21:53
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save baliw/4020619 to your computer and use it in GitHub Desktop.
Save baliw/4020619 to your computer and use it in GitHub Desktop.
Mountain Lion Notification Center via Python
import Foundation
import objc
import AppKit
import sys
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
notification = NSUserNotification.alloc().init()
notification.setTitle_(title)
notification.setSubtitle_(subtitle)
notification.setInformativeText_(info_text)
notification.setUserInfo_(userInfo)
if sound:
notification.setSoundName_("NSUserNotificationDefaultSoundName")
notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
notify("Test message", "Subtitle", "This message should appear instantly, with a sound", sound=True)
sys.stdout.write("Notification sent...\n")
@MrBjorn
Copy link

MrBjorn commented Aug 26, 2014

Hi,

I tried this, but it didn't show any notification. I am on 10.9.4, anything that have changed maybe?

Cheers

@karlbunch
Copy link

@MrBjorn I was able to make it work on 10.9.5 by making it a proper python script:

$ cat > ~/tmp/n
#!/usr/bin/python
{cut-paste above here}
{Control-D}
$ chmod a+x ~/tmp/n
$ ~/tmp/n

Worked right out of the gate!

@baliw Thanks this saved me a lot of time digging into OSX Docs to figure out the objects and call structure.

@meoow
Copy link

meoow commented Oct 12, 2014

Is there a way to custom the icon showing in notification?
The message does not wrap but cut off if longer than the width, any workaround?

@WardsParadox
Copy link

Icon is dependent on the script icon. Just found that out myself.

@muammar
Copy link

muammar commented Sep 25, 2016

I am getting this error:

  % python3 mlnotifications.py                                                                                                                         !10244
Traceback (most recent call last):
  File "mlnotifications.py", line 1, in <module>
    import Foundation
ImportError: No module named 'Foundation'

Should I install pyobjc?.

@lakshmi-subramanya
Copy link

Getting following error on MAC OS 10.13 onwards: error: NSInvalidUnarchiveOperationException - Class 'OC_PythonDictionary' has a superclass that supports secure coding, but 'OC_PythonDictionary' overrides -initWithCoder: and does not override +supportsSecureCoding. The class must implement +supportsSecureCoding and return YES to verify that its implementation of -initWithCoder: is secure coding compliant.

@ninest
Copy link

ninest commented Jun 6, 2020

Is it possible to set a callback? For example, when a button is pressed, open a URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment