Skip to content

Instantly share code, notes, and snippets.

@changkun
Forked from baliw/mlnotifications.py
Created March 21, 2017 16:06
Show Gist options
  • Save changkun/b5e54a99f84cc06f72d4f56da362ffce to your computer and use it in GitHub Desktop.
Save changkun/b5e54a99f84cc06f72d4f56da362ffce 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")
@changkun
Copy link
Author

  1. we have to install pyobjc
  2. notification.setUserInfor_ might cause an error, we probably need remove it.

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