NSUserNotificationCenter.defaultUserNotificationCenter() returns None in python

2024/9/25 2:24:28

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 sysNSUserNotification = 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 notificationsubtitle: Subtitle of notificationinfo_text: Informative text of notificationdelay: Delay (in seconds) before showing the notificationsound: Play the default notification sounduserInfo: a dictionary that can be used to handle clicks in yourapp'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.

Answer

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://en.xdnf.cn/q/71632.html

Related Q&A

Flask app hangs while processing the request

I have a simple flask app, single page, upload html and then do some processing on it on the POST; at POST request; i am using beautifulsoup, pandas and usually it takes 5-10 sec to complete the task. …

How do I make a server listen on multiple ports

I would like to listen on 100 different TCP port with the same server. Heres what Im currently doing:-import socket import selectdef main():server_socket = socket.socket(socket.AF_INET, socket.SOCK_STR…

Django REST Framework: return 404 (not 400) on POST if related field does not exist?

Im developing a REST API which takes POST requests from some really brain-dead software which cant PATCH or anything else. The POSTs are to update Model objects which already exist in the database.Spec…

How can i login in instagram with python requests?

Hello i am trying to login instagram with python requests library but when i try, instagram turns me "bad requests". İs anyone know how can i solve this problem?i searched to find a solve f…

How to abstract away command code in custom django commands

Im writing custom django commands under my apps management/commands directory. At the moment I have 6 different files in that directory. Each file has a different command that solves a unique need. How…

Python one-liner (converting perl to pyp)

I was wondering if its possible to make a one-liner with pyp that has the same functionality as this.perl -l -a -F, -p -eif ($. > 1) { $F[6] %= 12; $F[7] %= 12;$_ = join(q{,}, @F[6,7]) }This takes i…

Getting symbols with Lark parsing

Im trying to parse a little pseudo-code Im writing and having some trouble getting values for symbols. It parses successfully, but it wont return a value the same as it would with "regular" …

Why cant I connect to my localhost django dev server?

Im creating a django app and in the process of setting up my local test environment. I can successfully get manage.py runserver working but pointing my browser to any variation of http://127.0.0.1:8000…

How to use L2 pooling in Tensorflow?

I am trying to implement one CNN architecture that uses L2 pooling. The reference paper particularly argues that L2 pooling was better than max pooling, so I would like to try L2 pooling after the acti…

Abstract base class is not enforcing function implementation

from abc import abstractmethod, ABCMetaclass AbstractBase(object):__metaclass__ = ABCMeta@abstractmethoddef must_implement_this_method(self):raise NotImplementedError()class ConcreteClass(AbstractBase)…