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

Using pyobjc via launchd does not behave as expected. #488

Open
mikez opened this issue Aug 2, 2022 · 3 comments
Open

Using pyobjc via launchd does not behave as expected. #488

mikez opened this issue Aug 2, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@mikez
Copy link

mikez commented Aug 2, 2022

Describe the bug

Using pyobjc via launchd does not behave as expected.

For example, normally

$ python3 -c 'import AddressBook as ab; print(len(ab.ABAddressBook.sharedAddressBook().people()))'
100

However, if the command is launched via launchd, then ab.ABAddressBook.sharedAddressBook() returns None.

Platform information

  • Python 3.9.13
  • homebrew
  • macOS 11.6.5

To Reproduce

  1. Create a shell script ab-example.sh with the content here.
  2. chmod +x ab-example.sh to make it executable.
  3. Create a simple launchd configuration file ab-example.plist with the example content here. Replace YOUR_USER_HERE by your username.
  4. launchctl load ~/ab-example.plist
  5. launchctl start ab-example

In ab-example.errors.log you can see

Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'people'

Expected behavior
ab.ABAddressBook.sharedAddressBook() does not return None.

Note, that this runs fine in the Terminal

$ /usr/local/bin/python3 -c 'import AddressBook as ab; print(len(ab.ABAddressBook.sharedAddressBook().people()))'
100

Additional context

  • This worked fine prior to upgrading to the newer macOS.
  • I tried various workaround to SIP as outlined here and here. These workarounds resolve other cases, but not pyobjc.
@mikez mikez added the bug Something isn't working label Aug 2, 2022
@ronaldoussoren
Copy link
Owner

I expect that this is a rights problem, with code running in a launch item not having access to the AddressBook.

Have you tried bundling the script in an app bundle and run it that way. First time running should ask for permission (system dialog), after that running the main binary in the app bundle in a launch bundle will likely work.

@Alexandro1112
Copy link

python3 -c 'import AddressBook as ab; print(len(ab.ABAddressBook.sharedAddressBook().people()))' It doesn't show anything at all, just print me 2023-07-04 10:52:42.743 Python[79271:7603071] -[__NSCFString count]: unrecognized selector sent to instance 0x600003d25170 zsh: trace trap python3 -c

@mikez
Copy link
Author

mikez commented Jul 4, 2023

@ronaldoussoren Thank you for the helpful reply and sorry for the late response.

Your proposed method resolved it for me. ✅

Here are the steps for others:

  1. Create an app ab-example.app (e.g. with the Script Editor or osacompile) and launch ab-example.sh from there.
  2. In the launchd configuration plist, put: "open -W ab-example.app"
  3. run launchd start ab-example and grant permission to access contacts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants