Add contact to a group?
My first question here and just getting started so please be kind.
The task I’ve set myself is to automatically extract contact information from a stack of pdf registration forms, and to create new IOS contacts with that extracted information.
I’ve used (apple) workflow to get the text elements from the PDFs, and have cobbled together the code below from snippets found here that use the contacts module provided in pythonista to create a new contact in IOS.
All good so far.... however now I’m stuck and looking for some clues on how to add the contact to a particular group in IOS contacts, namely “Customers”
The documentation doesn’t seem to cover this in a way I’m able to understand and there are no examples I can find either here or elsewhere on the web showing how to use the contact.group element to add a new member to a group.
What I’ve got so far is here:
Note: I wasn’t able to post the code directly here without being flagged as spam.
Any help much appreciated.
@my4paws I think (but not sure) there is no Pythonista nor Objective-C IOS function do add a group to a contact. The only way should be via iCloud.
Thanks for your reply, I’m kanda hoping that’s not the case though.
The documents covers the following:
A Group object represents a group in the address book, e.g. friends, family, or co-workers. After initializing a Group object and setting its name attribute, it can be added to the address book database by calling add_group(). When a group’s name is modified, the save() function has to be called in order to make the change persistent.
The group’s name, e.g. ‘Friends’ or ‘Family’.
The persistent identifier of the group record in the address book (int, readonly), or -1 if the group has not been saved yet. The id can be used with the get_group() function.
Somewhere else I’ve seen mention of an “add_member” function too, which perhaps implies it might be possible?
introspectionto the rescue...
import contacts print(dir(contacts.get_all_groups()[-1])) # dir(the last Group in Contacts)
Group has three methods
@my4paws You're right.
I've created an empty group in iCloud and this script prints its name and lists the methods of the group...
import contacts for grp in contacts.get_all_groups(): print(grp.name) print(dir(grp))
Groupe sans titre 1 ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'add_member', 'get_members', 'id', 'name', 'remove_member']
import contacts pers = contacts.get_all_people() print(pers.last_name) grp = contacts.get_all_groups()[-1] print(grp.name) grp.add_member(pers) contacts.save()
I've checked in iCloud some seconds after and the group contains the contact
@cvp Brilliant! Thank you so much. My first script is complete and is working beautifully. :) Even better than that is I learned something!