Contacts API - Group Modification
-
Hello!
I'm currently trying to implement a small script to explicitly set/change a single contact's group. I'm not sure how to use the API.
Reading through http://omz-software.com/pythonista/docs/ios/contacts.html, I find ways to get all groups or those specific to a user, but I do not find information on how to set or change a user's group.
Did anyone already successfully implement this functionality?
Kind regards,
Hendrik
-
Python
introspection
to the rescue...import contacts print(dir(contacts.get_all_groups()[-1])) # dir(the last Group in Contacts)
Group has three methods
add_member()
,get_members()
, andremove_member()
.
-
Thank you, this is exactly the pointer I need..
-
A quick follow-up question on groups: I try to move a locally created contact to a group that is supposed to reside on a CardDAV-server.
Is there any way to distinguish between the individual accounts the groups belong to? I ask, because I can iterate over my iCloud-stored groups, but the CardDAV-stored group seems to be missing.
I will try and find my way in reverse (find a user, that is stored in the CardDAV-group and inspect the attributes), but maybe there is already some advice on how to speed up my learning here...
-
My Pythonista 3 does not have these methods😥
import contacts
p = contacts.find('foo')[0]
g = contacts.Group()
g.name = 'foobar '
g = contacts.add_group(g)
contacts.add_member(p,g)
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: module 'contacts' has no attribute 'add_member'print(dir(contacts.get_all_groups())[-1])
sortWhere have they gone?
-
@osamu small errors
import contacts p = contacts.find('foo')[0] g = contacts.Group() g.name = 'foobar ' contacts.add_group(g) g.add_member(p) contacts.save()
If after multiple tests, you get multiple groups named "foobar", you can remove them via
import contacts for g in contacts.get_all_groups(): print(g.name) if 'foobar' in g.name: contacts.remove_group(g) contacts.save()
-
I got the point. add_member() is a method of object Group().
Thanks!
-
@cvp one more question.
What is the attribute of People() that stores 'date' field?
-
@osamu you need to use ObjectiveC to get dates infos, like
from objc_util import * # ObjectiveC contacts CNContactStore = ObjCClass('CNContactStore').alloc().init() CNContact = ObjCClass('CNContact') Containers = CNContactStore.containersMatchingPredicate_error_(None,None) for Container in Containers: id = Container.identifier() predicate = CNContact.predicateForContactsInContainerWithIdentifier_(id) # keys not exactly like in Apple doc predicate_contacts = CNContactStore.unifiedContactsMatchingPredicate_keysToFetch_error_(predicate, ['familyName', 'givenName', 'middleName', 'dates'], None) for contact in predicate_contacts: if len(contact.dates()) == 0: continue name = str(contact.givenName()) + '|' + str(contact.middleName()) + '|' + str(contact.familyName()) print(name) for date in contact.dates(): print(date) # CNLabeledValue d = date.value() print(d) # NSDateComponents print(date.label(),':',d.year(),d.month(),d.day())
-
@cvp Thanks! You know everything!
Oh, that's very challenging...
-
@osamu said:
You know everything!
Sincerely, not at all but I'm retired thus I have a lot of time to spend by searching, trying, bugging 😂