8

I'm trying to send notifications to Mountain Lion from my python script and react to clicks on the notifications. Sending the notifications works perfectly find by now. But yet I was not able to get Lion to call back my script upon a click.

Here is what I do. I implemented a Notification class. The only purpose of an instance of that class is to provide notifications by invoking notify(). In the same method I set the object the app's delegate.

import Foundation
import objc
import AppKit

class MountainLionNotification(Foundation.NSObject, Notification):

    def notify(self, title, subtitle, text, url):
        NSUserNotification = objc.lookUpClass('NSUserNotification')
        NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
        notification = NSUserNotification.alloc().init()
        notification.setTitle_(str(title))
        notification.setSubtitle_(str(subtitle))
        notification.setInformativeText_(str(text))
        notification.setSoundName_("NSUserNotificationDefaultSoundName")
        notification.setUserInfo_({"action":"open_url", "value":url})
        AppKit.NSApplication.sharedApplication().setDelegate_(self)
        NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)

    def applicationDidFinishLaunching_(self, sender):
        userInfo = sender.userInfo()
        if userInfo["action"] == "open_url":
            import subprocess
            subprocess.Popen(['open', userInfo["value"]])

Now I expected applicationDidFinishLaunching_() to be called upon a click on the notification. Unfortunately that never happens. What am I doing wrong?

koloman
  • 711
  • 7
  • 19
  • I've tried to add a decorator `@objc.signature("v@:^@")` to the delegate method, without success. – koloman Aug 31 '12 at 07:15
  • Now I also tried to set my `MountainLionNotification` object the default notification center's delegate and implement the protocols `userNotificationCenter_didActivateNotification_()` method. Stil no success! – koloman Aug 31 '12 at 07:51
  • Hey, were you able to get notifications to show from just a python script/interpreter without starting the event loop? I can't seem to even get notifications showing using the code above – GP89 Oct 24 '12 at 12:54
  • @GP89 - You definitely need to start the event loop; there's no way around it. – Glyph Dec 16 '12 at 07:10
  • 1
    @koloman Where did you get Notification from in class MountainLionNotification(Foundation.NSObject, Notification) ? – Pylinux May 20 '16 at 09:18
  • if you have an answer for Catalina please post on: https://stackoverflow.com/questions/62234033/how-create-local-notification-on-macos-catalina-pyobjc – Chromazmoves Jun 09 '20 at 23:50

1 Answers1

8

Ok, found it. Didn't run AppHelper.runEventLoop(). Obviously a facepalm mistake. The following code works:

class MountainLionNotification(Foundation.NSObject, Notification):

    def notify(self, title, subtitle, text, url):
        NSUserNotification = objc.lookUpClass('NSUserNotification')
        NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
        notification = NSUserNotification.alloc().init()
        notification.setTitle_(str(title))
        notification.setSubtitle_(str(subtitle))
        notification.setInformativeText_(str(text))
        notification.setSoundName_("NSUserNotificationDefaultSoundName")
        notification.setHasActionButton_(True)
        notification.setOtherButtonTitle_("View")
        notification.setUserInfo_({"action":"open_url", "value":url})
        NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
        NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)

    def userNotificationCenter_didActivateNotification_(self, center, notification):
        userInfo = notification.userInfo()
        if userInfo["action"] == "open_url":
            import subprocess
            subprocess.Popen(['open', userInfo["value"]])
koloman
  • 711
  • 7
  • 19
  • 1
    I know this is old, but I'm getting the errors: `objc.BadPrototypeError: Objective-C expects 1 arguments, Python argument has 5 arguments for `, and `Notification is not defined`. Do you know if these can be fixed? – themthem Jun 06 '20 at 04:49