Python post osx notification

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

    Here is a way (You need the Foundation module):

    from Foundation import NSUserNotification
    from Foundation import NSUserNotificationCenter
    from Foundation import NSUserNotificationDefaultSoundName
    
    
    class Notification():
        def notify(self, _title, _message, _sound = False):
            self._title = _title
            self._message = _message
            self._sound = _sound
    
            self.notification = NSUserNotification.alloc().init()
            self.notification.setTitle_(self._title)
            self.notification.setInformativeText_(self._message)
            if self._sound == True:
                self.notification.setSoundName_(NSUserNotificationDefaultSoundName)
    
            center = NSUserNotificationCenter.defaultUserNotificationCenter()
            center.deliverNotification_(self.notification)
    
    N = Notification()
    N.notify(_title="SOME", _message="Something", _sound=True)
    

    This works only for MAC. Hope you enjoy!

    0 讨论(0)
  • 2020-12-04 07:22

    You should install terminal-notifier first with Ruby for example:

    $ [sudo] gem install terminal-notifier
    

    And then you can use this code:

    import os
    
    # The notifier function
    def notify(title, subtitle, message):
        t = '-title {!r}'.format(title)
        s = '-subtitle {!r}'.format(subtitle)
        m = '-message {!r}'.format(message)
        os.system('terminal-notifier {}'.format(' '.join([m, t, s])))
    
    # Calling the function
    notify(title    = 'A Real Notification',
           subtitle = 'with python',
           message  = 'Hello, this is me, notifying you!')
    

    And there you go:

    enter image description here

    0 讨论(0)
  • 2020-12-04 07:23

    Try ntfy if you also want the script to be able to communicate with you over other devices.

    Installation

    [sudo] pip install ntfy 
    

    where pip refers to the Package Installer of the target Python version

    For Python3 installation:

    [sudo] pip3 install ntfy    
    

    Usage

    I use this simple function for notifications regarding command executions and download completions:

    def notification(title, message):
        """Notifies the logged in user about the download completion."""
    
        import os
        cmd = 'ntfy -t {0} send {1}'.format(title, message)
        os.system(cmd)
    
    notification("Download Complete", "Mr.RobotS01E05.mkv saved at /path")
    

    Advantages of ntfy

    1. This tool is quite handy as it logs all the notifications directly to the Notification Center rather than referring to other third-party application.

    2. Multiple Backend supports: This tool can connect to you over any device through services such as PushBullet, SimplePush, Slack, Telegram etc. Check the entire list of supported backend services here.

    0 讨论(0)
  • 2020-12-04 07:30

    Another choice is a python lib named pync, maybe it's a better choice. pync is a simple Python wrapper around the terminal-notifier command-line tool, which allows you to send User Notifications to the Notification Center on Mac OS X 10.10, or higher.

    Installation:

    pip install pync

    Examples:

    from pync import Notifier
    
    Notifier.notify('Hello World')
    Notifier.notify('Hello World', title='Python')
    Notifier.notify('Hello World', group=os.getpid())
    Notifier.notify('Hello World', activate='com.apple.Safari')
    Notifier.notify('Hello World', open='http://github.com/')
    Notifier.notify('Hello World', execute='say "OMG"')
    
    Notifier.remove(os.getpid())
    
    Notifier.list(os.getpid())
    
    0 讨论(0)
  • 2020-12-04 07:31

    All the other answers here require third party libraries; this one doesn't require anything. It just uses an apple script to create the notification:

    import os
    
    def notify(title, text):
        os.system("""
                  osascript -e 'display notification "{}" with title "{}"'
                  """.format(text, title))
    
    notify("Title", "Heres an alert")
    

    Note that this example does not escape quotes, double quotes, or other special characters, so these characters will not work correctly in the text or title of the notification.

    0 讨论(0)
  • 2020-12-04 07:38

    For a Python only implementation, I've modified the code that someone posted as part of another related question, and is working well for me:

    import mmap, os, re, sys
    from PyObjCTools import AppHelper
    import Foundation
    import objc
    import AppKit
    import time
    from threading import Timer
    
    from datetime import datetime, date
    
    # objc.setVerbose(1)
    
    class MountainLionNotification(Foundation.NSObject):
        # Based on http://stackoverflow.com/questions/12202983/working-with-mountain-lions-notification-center-using-pyobjc
    
        def init(self):
            self = super(MountainLionNotification, self).init()
            if self is None: return None
    
            # Get objc references to the classes we need.
            self.NSUserNotification = objc.lookUpClass('NSUserNotification')
            self.NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
    
            return self
    
        def clearNotifications(self):
            """Clear any displayed alerts we have posted. Requires Mavericks."""
    
            NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
            NSUserNotificationCenter.defaultUserNotificationCenter().removeAllDeliveredNotifications()
    
        def notify(self, title, subtitle, text, url):
            """Create a user notification and display it."""
    
            notification = self.NSUserNotification.alloc().init()
            notification.setTitle_(str(title))
            notification.setSubtitle_(str(subtitle))
            notification.setInformativeText_(str(text))
            notification.setSoundName_("NSUserNotificationDefaultSoundName")
            notification.setHasActionButton_(True)
            notification.setActionButtonTitle_("View")
            notification.setUserInfo_({"action":"open_url", "value":url})
    
            self.NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
            self.NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
    
            # Note that the notification center saves a *copy* of our object.
            return notification
    
        # We'll get this if the user clicked on the notification.
        def userNotificationCenter_didActivateNotification_(self, center, notification):
            """Handler a user clicking on one of our posted notifications."""
    
            userInfo = notification.userInfo()
            if userInfo["action"] == "open_url":
                import subprocess
                # Open the log file with TextEdit.
                subprocess.Popen(['open', "-e", userInfo["value"]])
    

    You could likely clean up the import statements to remove some unneeded imports.

    0 讨论(0)
提交回复
热议问题