NSUserNotificationCenter.defaultUserNotificationCenter() returns None in python
Asked Answered
O

1

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.

Octosyllabic answered 15/4, 2013 at 17:35 Comment(9)
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?Slump
Ah! I suppose I could have!Octosyllabic
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.Hindman
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.Hindman
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.Octosyllabic
@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.Lacrosse
possible duplicate of NSUserNotificationCenter.defaultUserNotificationCenter() returning None using PyInstallerKile
btw, the answer on that other question is what worked for meKile
if you have an answer for Catalina please post on: #62234533Slivovitz
O
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).

Octosyllabic answered 15/4, 2013 at 20:3 Comment(2)
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?Meyeroff
This is working for me using a native install of pyobjc with a custom python installation that's 64 bits.Whole

© 2022 - 2024 — McMap. All rights reserved.