Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'NoneType' object has no attribute 'setDelegate_' #8

Open
rutgerrrrr opened this issue May 26, 2023 · 7 comments
Open

Comments

@rutgerrrrr
Copy link

Python 3.11.3, "client.create_notification" returns the following error:

Process NotificationProcess-1: Traceback (most recent call last): File "/Users/rutger/.pyenv/versions/3.11.3/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap self.run() File "/Users/rutger/.pyenv/versions/3.11.3/lib/python3.11/site-packages/mac_notifications/listener_process.py", line 28, in run notification_sender.create_notification(self.notification_config, self.queue).send() File "/Users/rutger/.pyenv/versions/3.11.3/lib/python3.11/site-packages/mac_notifications/notification_sender.py", line 58, in send NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'setDelegate_'

@grhkm21
Copy link

grhkm21 commented May 29, 2023

Exact same error, Python 3.10.10 (conda), macOS 13.4.

@grhkm21
Copy link

grhkm21 commented May 29, 2023

I googled quite briefly and as the README suggests, the current method of using NSUserNotificationCenter is deprecated and only supported up to MacOS 11.0, see here. The new way to do it is via UNUserNotificationCenter, but it seems that it is only available when run from a signed binary (?) I just tried myself to get UNUserNotificationCenter to work but it's a mess and hard to get working lol

@grhkm21
Copy link

grhkm21 commented May 29, 2023

I have figured out a way to send notifications, but it's not pretty at all and goes against the point of this library, which is "mac-notification is a Python library to make it as easy as possible to create interactable notifications." Here's the code though:

from Cocoa import NSApplication, NSClassFromString


def main():
    center = NSClassFromString("UNUserNotificationCenter")
    print(center)

    current = center.currentNotificationCenter()
    print(current)


if __name__ == "__main__":
    main()

However, if you just run this from a script, it will crash with the exception bundleProxyForCurrentProcess is nil: mainBundle.bundleURL .... As I mentioned, the method is only available inside a signed application. Instead, you have to do something like this. Assuming that main.py is the file:

pip3 install py2app
echo -e "[tools.py2app.bundle.main]\nscript = "main.py"\nbuild-type = "semi-standalone" > pyproject.toml
py2applet --make-setup main.py
./dist/notif.app/Contents/MacOS/notif

This code should give you something like <UNUserNotificationCenter: 0x600000c23cf0>.

I wonder if Jorricks can find a way to send notifications simper than this, but it seems it's a fundamental problem put on by Apple themselves :(

@Jorricks
Copy link
Owner

Jorricks commented Sep 6, 2023

Sorry for dropping the ball on this one. Have any of you been able to figure this issue out?

@grhkm21
Copy link

grhkm21 commented Sep 6, 2023

I have posted all my findings above. As I said, I think it is fundamentally impossible to post notifications from an unsigned app alone.

However, if you can launch a signed service in the background once, then you can make it send the notifications for you, kind of like a proxy. Hope my terminology is ok

@MacHu-GWU
Copy link

Same issure here.

@Jorricks
Copy link
Owner

Jorricks commented Feb 4, 2024

Sorry for replying this late and thanks for reporting this.

I have the feeling this is because of not using the official python binary. If you use a self-compiled Python binary that is not officially signed by an Apple certificate, it will throw this error. Could you try with an official Python binary and report back here?

Would you mind giving this a shot @MacHu-GWU and @grhkm21 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants