6

I am trying to connect to the Mountain Lion notification center via python. I've installed pyobjc and am following the instructions here and here. Also see: Working with Mountain Lion's Notification Center using PyObjC

Here's my code:

import Foundation, objc
import AppKit
import sys

NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')

def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
    """ Python method to show a desktop notification on Mountain Lion. Where:
        title: Title of notification
        subtitle: Subtitle of notification
        info_text: Informative text of notification
        delay: Delay (in seconds) before showing the notification
        sound: Play the default notification sound
        userInfo: a dictionary that can be used to handle clicks in your
                  app's applicationDidFinishLaunching:aNotification method
    """
    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)

When I call the notify function with arguments I get an attribute error:

AttributeError: 'NoneType' object has no attribute 'scheduleNotification_'

I don't understand why NSUserNotificationCenter.defaultUserNotificationCenter() is returning a NoneType object. I've not been able to query anything on this matter on the internet or SO.

Community
  • 1
  • 1
deepak
  • 2,045
  • 2
  • 21
  • 36
  • 2
    Couldn't you reproduce this with just three lines of code (`import objc; NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter'); print NSUserNotificationCenter.defaultUserNotificationCenter()`) instead of the entire function? – David Robinson Apr 15 '13 at 17:45
  • Ah! I suppose I could have! – deepak Apr 15 '13 at 17:46
  • FWIW, the three-line test works for me using the current MacPorts `python27` (@2.7.3_1) and `py27-pyobjc` (@2.5.1_0): it returns an instance of `_NSConcreteUserNotificationCenter`. – Ned Deily Apr 15 '13 at 18:41
  • For that matter, the three-line test also works for me using the current Apple-supplied system Python 2.7.2 (`/usr/bin/python2.7`) in OS X 10.8.3. Apple ships a version of PyObjC with its system Pythons. – Ned Deily Apr 15 '13 at 18:45
  • Thanks Ned! I noticed it works for me too using default python 2.7.2. However, the issue I presented above occurs when I installed PyObjC using easy_install on a 64 bit installation of Enthought Python. For the moment I guess I'll just continue using the system default python in this script. – deepak Apr 15 '13 at 19:22
  • @NedDeily MyNSUserNotificationCenterDelegate.alloc().init() to show notification, whether the app is front end or back end. Can you suggest some way to resolve this issue. – imp Nov 18 '13 at 04:36
  • possible duplicate of [NSUserNotificationCenter.defaultUserNotificationCenter() returning None using PyInstaller](http://stackoverflow.com/questions/25204797/nsusernotificationcenter-defaultusernotificationcenter-returning-none-using-py) – g3rv4 Aug 31 '15 at 13:51
  • btw, the answer on that other question is what worked for me – g3rv4 Aug 31 '15 at 13:51
  • 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:52

1 Answers1

2

So, this works fine using Ned's advice of using default python. It also worked when I installed pyobjc on the 32-bit enthought distribution. It seems to me that pyobjc works only on 32 bit python distributions (I cannot confirm this, but so far this seems to be the case).

(Note: When I posted the question I had installed pyobjc on the 64 bit enthought distribution).

deepak
  • 2,045
  • 2
  • 21
  • 36
  • However, a number of Apple frameworks won't work properly unless the binary is in a bundle (application or plugin). Does Enthought install a framework or just a plain unix distribution? – Ronald Oussoren Apr 17 '13 at 07:02
  • 1
    This is working for me using a native install of pyobjc with a custom python installation that's 64 bits. – El Developer Jul 21 '13 at 05:06