omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Unable to save or add contact

    Pythonista
    contacts
    3
    14
    5808
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • clark
      clark last edited by

      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 1 Reply Last reply Reply Quote 1
      • cvp
        cvp @clark last edited by 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

        1 Reply Last reply Reply Quote 0
        • clark
          clark last edited by 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 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @clark last edited by cvp

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

            1 Reply Last reply Reply Quote 0
            • clark
              clark last edited by

              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() ?

              1 Reply Last reply Reply Quote 0
              • clark
                clark last edited by 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 1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @clark last edited by

                  @clark 👍

                  1 Reply Last reply Reply Quote 0
                  • eddo888
                    eddo888 last edited by 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 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @eddo888 last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • eddo888
                        eddo888 last edited by eddo888

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

                        cvp 1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @eddo888 last edited by

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

                          1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp last edited by

                            Your last print shows that person.phone did not change

                            1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp last edited by

                              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
                              
                              1 Reply Last reply Reply Quote 0
                              • eddo888
                                eddo888 last edited by

                                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 = <updated list>

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post
                                Powered by NodeBB Forums | Contributors