NSUserNotificationCenter.defaultUserNotificationCenter() returns None in python

纵饮孤独 提交于 2019-12-10 15:10:44

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/16021330/nsusernotificationcenter-defaultusernotificationcenter-returns-none-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!