Forum Archive

Unable to save or add contact

clark

All of the examples I can find online make it look easy, but I can’t get it to work. ‘save()’ always returns false.

What am I doing wrong?

```
import contacts

print(("Authorized:", contacts.is_authorized()))
p = contacts.Person()
p.first_name = "Johnny"
p.last_name = "Ray"
print(("add_person:", contacts.add_person(p)))
print(("save:", contacts.save()))```

cvp

@clark test gives

('Authorized:', True)
('add_person:', True)
('save:', True)

And John Ray is added

Be careful, several runs create several contacts with the same name

Wait perhaps some time before to check If exists

clark

Thanks for the response.

I’m baffled.

I waited a while. I waited overnight. I went to iCloud on my laptop and checked there.

Also, I’m unable to do a simple edit on an existing Person.

There’s something fundamentally wrong on my end.

cvp

@clark you are sure that in iOS settings, Pythonista is authorized to access to contacts ?

clark

I dbl-checked that. Pythonista has access to Contacts & iCloud.

Is there a way to get the error code, other than just a False return from the Contacts.save() ?

clark

*** SOLVED ***

I uninstalled / reinstalled Pythonista.
This time I got the prompt to authorize access to Contacts.

Sometimes I wonder about myself :-/

Thanks for the help.

cvp

@clark 👍

eddo888

I am having difficulties as well, I am changing a phone number and it doesnt save even though save sais true and is_authorised is true.

    print(("Authorized:", contacts.is_authorized()))

    pattern = re.compile('^(0([2346789])|\(0([2346789])\))')
    for person in contacts.get_all_people():
        if person.full_name != 'David Edson': continue
        #print person.full_name

        for p in range(len(person.phone)):
            t_phone = person.phone[p]
            l_phone = list(t_phone)
            number = str(l_phone[1])
            match = pattern.match(number)
            if match:
                changed = pattern.sub('+61 %s'%match.group(2),number)
                print(number, changed)
                l_phone[1] = changed
                print l_phone
                t_phone = tuple(l_phone)
                print t_phone
                person.phone[p] = t_phone

        print person.phone
    print('save:',contacts.save())

with the output like this

('Authorized:', True)
('0408 680 808', '+61 408 680 808')
[u'_$!<Mobile>!$_', '+61 408 680 808']
(u'_$!<Mobile>!$_', '+61 408 680 808')
[(u'_$!<Mobile>!$_', u'0408 680 808')]
('save:', True)
cvp

@eddo888 did you wait some time before checking it has been saved?

eddo888

yup, 1 hour now, the johhny ray example saved instantly, can we edit a contact and expect save to work ?

cvp

@eddo888 Sorry, I didn't read correctly your post 😢. Thus the problem is not the save but your code

cvp

Your last print shows that person.phone did not change

cvp

This seems to work, if your contact has only one phone

                print 'before', person.phone
                #person.phone[p] = t_phone
                person.phone = [t_phone]
                print 'after', person.phone
eddo888

cool, thanks, there must be a setter property on person.phones that does the work,
changes to bits of the phone list are not seen unless you do person.phone =