Python post osx notification

后端 未结 7 1166
没有蜡笔的小新
没有蜡笔的小新 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:44

    copy from: https://gist.github.com/baliw/4020619

    following works for me.

    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")
    
    0 讨论(0)
提交回复
热议问题