Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,029
168
Norway
For a long time I've been struggling with iTunes not being able to sync my contacts with my iPad, something I've in detail gone into in this thread.
Long story short I've narrowed down the problem source to be some of the contact entries themselves. Bottom line: I can now sync Contacts with my iPad (via iTunes through a USB connection) as long as I don't have any of those problematic entries. The problem of course is that I have no idea which ones are OK or not!

A small test where I imported a few (i.e. 10 or so) different entries into an empty Contacts app showed that with one or more "problem" entries I wouldn't be able to sync at all, and that locating the "problem" entries isn't obvious either as they all appear fine in Contacts on the Mac. No corrupt characters, missing fields or whatever to give it away.
It takes forever to import one contact entry (previously exported) at a time into an empty Contacts, then sync iTunes to see if it works or not, and then move on to importing the next entry.

So.... to make this easier I'm wondering:
1) is there a way to check/analyze and fix a corrupt Contacts file, determining where exactly the problem lies?
2) is there a way to export all my Contacts card entries as individual Vcards? (I have yet not found a way of selecting a number of entries, then being able to export them as one vCard file for each entry. In this case Contacts saves a single Vcard file as i.e. "John Doe and 598 others". Not very useful for debugging purposes as what I need.
 
Looks like this might be a long existing bug. For example:


Guessing that this was not and never will be fixed (ie. iTunes is dead to Apple; most recent update appears to be in 2019).

You can try AppleScript. #2 would be the easy one, as it appears that writing out one vcard per entry is possible (just need to dynamically build up the file name to use a counter, eg. entry001.vcard, entry002.vcard).

Starting point:


(this can also be done with Shortcuts fairly easily, but probably not applicable for this scenario, unless you want to make the iPad the master and Mac the client for Contacts)

Looping through a Contact's fields is a bigger task.
 
I just did a little test on exporting to vcards, and it looks to me like the composite file is nothing but the concatenation of the individual vcards. That is, the end of one vcard is followed by the start of the next one.

For example, exporting 3 Contacts entries results in output like this:
Code:
BEGIN:VCARD
VERSION:3.0
...data..
END:VCARD
BEGIN:VCARD
VERSION:3.0
...data..
END:VCARD
BEGIN:VCARD
VERSION:3.0
...data..
END:VCARD

To see a concrete example, export multiple contacts to a VCF file, then drag-and-drop the resulting file onto TextEdit.app. It should open it as a plain text file, where you can look at the data. One might notice something awry on visual inspection.

Since the end of each card is clearly demarcated, as is the start of the next one, it seems like a fairly simple program could split this into multiple files.

The simplest would be to name the resulting files sequentially (1.vcf, 2.vcf, etc.). More complex would be to look at a particular VCARD field such as the N: or FN: fields, and convert that into a filename.

I suspect something like awk or perl could do this.

 
Thanks for the comments and suggestions!
So mine is apparently not an isolated case, but it's a widespread bug. But without a proper solution as far as I could see from that other thread linked above.

I took a look at the Applescript and even tried it out, but am unfortunately not knowledgeable enough to edit the script so it can "spit out" single VCF files (one for each entry). I think that could be helpful in determining which entries can be synced, then (in worst case) manually re-enter the remaining ones. If I'm lucky the problematic ones won't be the majority of all my contacts.
According to the Apple forum thread linked above the problem (at least partly) is with entries containing "son" and "daughter". I was looking into a way of creating a Smart Group in the Contacts app to group only those entries with "son" or "daughter" fields but didn't succeed. Is it possible to do this?
That would help a lot as I could narrow down the problematic entries.

Ruggy: interesting to hear about BeauContact, but I see it's for iPad/iPhone and not for the Mac platform, so unfortunately that won't be of much help (as I can't export the contacts from my Mac over to my iPad). But once that's done, perhaps its a helpful app to remove repeated entries or such things?
 
Not really an answer to your question, but if you can't figure out a way to automate the process, I'd suggest trying to batch them, so you send over maybe half of your contacts at a time & see if those error out. If it does, then cut that batch in half and do it over again, etc. Assuming you only have a small number of corrupted contacts, the process should go pretty quickly. I think you could probably separate them into groups in the contacts app on the Mac to help with the process.
 
Yes, that's a good suggestion (though time consuming).
Keeping in mind what the Apple discussion thread said about son, daughter etc. fields being the culprit I've now done the following within the Contacts app:

1) I created a number of empty groups, naming them with a description of various "anomalies" I noticed among my contacts, such as:
- contains birthday-field
- contains son, daughter or spouse-field
- contains non-standard fields (custom fields)
- contains repeated fields
- doesn't contain any name

2) I then exported the contact archive to keep a backup

3) next (I haven't gotten this far yet) I'm going to connect my iPad (via USB), open up iTunes and try to sync my contacts. Naturally it won't sync the first time, but now I can try it again after I've deleted the entries of a whole group (i.e. those that contain son, daughter or spouse fields). Hopefully I'll be able to sync after deleting one of those groups so I can narrow down the problem. This will be interesting to try out.
 
  • Like
Reactions: mk313
is there a way to export all my Contacts card entries as individual Vcards?

AppleScript:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set folderPath to (path to documents folder as string)

tell application "Contacts"
    -- your Contacts code goes here
    repeat with pers in people
        set theFile to folderPath & name of pers & ".vcf"
        my writeTextToFile(vcard of pers, theFile, true)
    end repeat
end tell

on writeTextToFile(theText, theFile, overwriteExistingContent)
    try
        
        -- Convert the file to a string
        set theFile to theFile as string
        
        -- Open the file for writing
        set theOpenedFile to open for access file theFile with write permission
        
        -- Clear the file if content should be overwritten
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        
        -- Write the new content to the file
        write theText to theOpenedFile starting at eof
        
        -- Close the file
        close access theOpenedFile
        
        -- Return a boolean indicating that writing was successful
        return true
        
        -- Handle a write error
    on error
        
        -- Close the file
        try
            close access file theFile
        end try
        
        -- Return a boolean indicating that writing failed
        return false
    end try
end writeTextToFile
 
  • Like
Reactions: NoBoMac
Wow! That's totally awesome. Thank you :) ?
It appears to work great except it exported 540 vcf card files while the Contact app only has 522 entries. Any idea why and what those additional 18 cards are?
Looks like I have some ways of testing further now, hopefully narrowing down the problem source! Thanks again.
 
Don't really know why it did that but no harm done in looking at one of those additional files in the Finder or TextEdit. Do they have names? Is there any data in them?
Glad it helped you further.
 
but now I can try it again after I've deleted the entries of a whole group

Maybe I'm mistaken here (been a while), but, should not need to "delete" entries, at least on "first" pass with iTunes sync. Should just need to exclude Groups from the sync, then dive into specific cards within a group.
 
Last edited:
That doesn't work here. I need to actually delete the offending entries in the Contacts app before syncing. It doesn't help just to exclude their group.

A little update:
1) I've now gone through a tedious process of first cleaning up my contacts (removing duplicates etc).

2) Then ensuring every entry was part of at least one group. I did this by creating a Smart Group like this:
Screen Shot 2022-04-30 at 13.40.51.png


Seeing that there were no entries in this Smart Group I moved on to the next step (if the Smart Group had some entries in it I would have put them in the existing groups (or some new ones) just so that no entries were "orphaned).

3) Now I went into the first group, selected all of its entries and right-clicked to "Export vCard". To make things easier I didn't use the default filename (i.e. "John Doe and 24 others") but the same group name as they belonged to (i.e. "Local businesses"). I repeated this for every group in Contacts so that I ended up with a number of vCard groups (i.e. "Local businesses", "Friends", "Family", "Work contacts", "Church", "School" etc.)

4) To further organize things I clicked on "All contacts" in Contacts, which of course displays absolutely all my entries alphabetically.
I then selected everything under "A", right-clicked to "Export vCard" and saved it as "A".
I repeated this by selecting all the "B" entries and exported this as "B" and so on, until I had everything in the alphabetical list. Phew!!!

5) With all those vCard files I should be able to narrow down the problematic entries much easier, and if this doesn't help I'll have to narrow it down further with the script I just received (so as to get one vCard for each entry), but I'm putting this off for now as I'm not sure why I'm not ending up with the same amount of vCards as the number of entries. I'll consider looking deeper into this if the abive vCards aren't enough to help me located the problem.


OK, now on to the process of actually locating the problematic entries....

6) I connect the iPad (via USB) to the Mac and start up iTunes on the Mac

7) In Contacts I back up my entire setup (File-Export-Contacts archive). This way I can always go back and re-import my previous setup. Very important!

8) Now, in Contacts I delete every single entry (go to "All contacts", select all of the entries, press backspace and choose to "Delete contacts").
Also delete every group.
I don't know if it makes a difference, but once this is done I also quit Contatcs, then reopen it (in case there are some caches that need to be flushed).


Contacts should be completely empty now, so we're ready to test our first vCard file to see if it contains any problematic entries (preventing iTunes from syncing) or not.....

9) Create a new group, giving it the same name as of the vCard you're going to import (i.e. "Family"). This is just to keep things tidy and organized.
Now import the "Family" vCard and ensure its entries are all shown within the "Family" group (you can also drag & drop the "Family" vCard file over the empty "Family" group.

10) now, in iTunes, ensure that on the right hand side, go to "Info", and ensure that "Sync contacts" in turned on (enabled)
Screen Shot 2022-04-30 at 14.16.43.png


If the iPad already contains some contacts you might also want to scroll down to the "Advanced" section of "Info" and turn on "Replace information on this iPad: Contacts" so it'll be easier to see what's going on.
Screen Shot 2022-04-30 at 14.19.04.png




Now we're ready to actually do the testing!

11) Press the "Sync" (sometimes changed to "Apply") button in iTunes to perform a sync.
If the sync is successful (i.e. the "Sync contacts" section in iTunes suddenly shows the same group as you have in Contacts, and you can also see it on your iPad (go to the Contacts app there as well, then go to see an overview of the groups. It should show the same group as you see in both iTunes and in your Mac's Contacts app.
Congratulations!!!
So now you've found out that the vCard you imported actually works!
Make this easy to see by going to the vCard file in the Finder window, right-click on it and select a green tag just to show that it's fine.
If it didn't work out (i.e. the group in Contacts on the Mac didn't show up in iTunes after syncing), tag the vCard file red so you know this one contains at least one problematic entry.

12) Now we're going to test the next vCard file, so clean up Contacts on the Mac by deleting all of its entries (not just removing them from the group! Ensure this has been done by going to the "All contacts" group). Also remove the group you created from last time.
Next create a new group, giving it the same name as the vCard file you're about to import (i.e. "Church").
Then drag & drop the "Church" vCard file over to the "Church" group. It should now contain all of the entries from church. Double-check to see (in the "All contacts" group) that there are no entries there other than those from church.

13) Repeat the process of syncing iTunes from step 11.
If it works you should see iTunes updating its "Sync contacts" section to reflect the new group. Again, if it syncs properly you can give the vCard file a green tag, if it doesn't give it a red tag in the Finder and move repeat with the next vCard file and so on.


I'm in the midst of this now (luckily some of the vCard files worked!), but I have no idea how many vCard files won't sync.
So what I'm doing now is the first round of testing. Once done I can eliminate the "green" vCard files and concentrate on the "red" ones.
That's all for now :)
 
  • Like
Reactions: mk313
I have a preliminary result which is interesting:

Testing with a vCard that contained just 3 entries and failed (i.e. I tagged it as a "red" vCard file) I could quickly determine which of the 3 entries was the offending one by using this method:

1) After a failed sync, delete (not remove!) the first entry
2) sync iTunes again
3) if it now syncs properly I know that the entry I just deleted was the offending one (and I can take a closer look at what makes it different from the other entries)
4) if it still doesn't sync I need to delete (not remove!) all of the entries, drag & drop the vCard to import them all again, then delete (not remove!) the second entry
5) sync iTunes again
6) if it now syncs properly I know it's the second entry (the one I just deleted) which causes the problems, if not I need to check the last entry.

This was a very small vCard file, making things very simple, but I assume that some problematic vCard files may have multiple offending entries, making it much more complicated. That's why it was nice to test with a small file and actually locate the source of the problem!
And the problem source here was that the offending entry contained a "daughter" field. Once I removed the daughter's name and once again synced it synced just fine!
This is an important finding, and one that confirms the findings written about in that Apple discussion thread linked earlier here. At least now I know of one field which can cause problems, so I know what to look for in the other failed vCard group files. Encouraging! ?
 
  • Like
Reactions: mk313
Interesting finding. Thanks for keeping us up to date
No problem. I've struggled with this problem for several years so it's a huge relief to finally find the source and a solution! I suspect others might be struggling with the same thing as well.

Kryten2: Wow! So that script actually fixes those entries even without having to export vCards first. Would it be possible to make it replace any instances of "son" or "daughter" with "child"?
I've now gone through all of my vCard groups and all of the sync problems were because of "son" or "daughter" fields being used. Fortunately "child" works (and is a usable replacement), and now I've re-assembled a working set of contacts ???

Having your script modified to replace "son" or "daughter" with "child" would be great if I ever run into this problem again, and also for anyone else who are having similar issues. Such a timesaver compared to manually finding and editing the problem-entries.

NOTE: I haven't tested all the other fields for sync problems, but I did notice something interesting worth mentioning.... editing an entry on my iPad (within its Contacts app) by finding a field named "Add related name" reveals a number of choices (mother, father, parent, brother, sister, child, friend, spouse, partner, assistant, manager, other), but not son or daughter!!!! So it appears Apple have forgotten to include those fields at the receiving end! No wonder it won't sync with the Mac!
This was on an iPad running iOS 9.3.5 so it might have been improved on with newer iOS versions, but since my newer iPad (iOS 14.8) also won't sync in these circumstances I suspect it hasn't been fixed there either (I'll take a look when I have that iPad handy, which I unfortunately don't have right now).
 
  • Like
Reactions: kryten2
Kryten2: Wow! So that script actually fixes those entries even without having to export vCards first. Would it be possible to make it replace any instances of "son" or "daughter" with "child"?
I've now gone through all of my vCard groups and all of the sync problems were because of "son" or "daughter" fields being used. Fortunately "child" works (and is a usable replacement), and now I've re-assembled a working set of contacts ???

Having your script modified to replace "son" or "daughter" with "child" would be great if I ever run into this problem again, and also for anyone else who are having similar issues. Such a timesaver compared to manually finding and editing the problem-entries.
My apologies, I should have read the links sooner. Glad I could help.

 
No need to apologize -my findings were preliminary and I don't think I had fully understood/written the scope of it ;)

I've now entered the script from your video into AppleScript editor (see below), added some comments and a quick test shows it to appear working fine!


Code:
-- This script will fix sync issues when syncing the contacts between a Mac and an iOS device (iPad, iPhone etc.).

-- Any contact entries containing "Son" or "Daughter" fields won't sync (and for some reason Apple haven't fixed this).
-- It's simply solved by replacing the "Son" or "Daughter" fields with "Child", which is what this script does automatically by looking through and fixing all such entries in your Mac Contacts app

-- The source of this script and a discussion relevant to this can be found here: https://forums.macrumors.com/threads/contacts-how-to-locate-fix-corrupt-entries.2342937


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Contacts"
    repeat with pers in people
        -- replace (every related name of pers whose label is "son" or label is "daughter") to "child"
        set label of (every related name of pers whose label is "son" or label is "daughter") to "child"
    end repeat
    save
end tell

To make it even more user-friendly it would be helpful to have a window pop up at the beginning, explaining what it'll do (and an "OK" (or "I understand, please proceed") as well as a "Cancel" button so as not to cause irreversable changes to the ignorant user) as well as a window that pops up at the end with a "Done! All contacts checked and fixed" along with an "OK" button.
How (and where in the script) do I add those two alert windows?
Oh.... and if possible (in the "all done" alert window) a summary of what's been done (i.e. "xx number of cards updated", "xx fields changed"). I personally always like some feedback of what has actually been done (or if the process has somehow failed).
 
Last edited:
Looking good! ?
I've made some minor changes to the dialogs while testing and it's a lot more informative now.

The only minor issue left is that if the Contacts app doesn't have any entries containing "son" or "daughter" the script ends with an alert saying "something went wrong". Is it possible to make this alert something like "No changes were made"?
 
You can change "something went wrong" into "anything you like" so certainly into "No changes were made".
 
Oops! :rolleyes:
I should have read through the script properly before thinking this was a default error message. Sorry about that.

Here's the final script, which (after several tests) appears to do the job perfectly:
AppleScript:
-- This script will fix sync issues when trying to sync Contacts between a Mac and an iOS device
-- It replaces any instances of "son" or "saughter" with "child" in the Mac "Contacts" app
-- WARNING: be sure to back up your contacts (File-Export-Contacts archive) in the Contacts app before proceeding, in case you need to revert
-- script source: https://forums.macrumors.com/threads/contacts-how-to-locate-fix-corrupt-entries.2342937

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

display dialog "Replace all SON or DAUGHTER fields with CHILD?" with title "Contacts sync repair" buttons {"Cancel", "OK"} default button 1 with icon caution

if the button returned of the result is "OK" then
    set numberOfCards to 0
    set numberOfFields to 0
    tell application "Contacts"
        set startList to label of (every related name of people whose label is "son" or label is "daughter")
        repeat with pers in people
            set relatedNames to (every related name of pers whose label is "son" or label is "daughter")
            if relatedNames is not {} then
                set numberOfFields to numberOfFields + (count of relatedNames)
                set numberOfCards to numberOfCards + 1
                set label of (every related name of pers whose label is "son" or label is "daughter") to "child"
                save
            end if
        end repeat
        set endList to label of (every related name of people whose label is "son" or label is "daughter")
    end tell
end if

if startList is equal to endList then
    display dialog "Nothing to repair!" with title "Title" buttons {"OK"} default button 1 with icon stop --giving up after 5
else
    display dialog ((numberOfCards as text) & space & "cards updated." & return & numberOfFields as text) & space & "fields changed." with title "Repair done!" buttons {"OK"} default button 1 with icon note --giving up after 5
end if

I've also attached it as a ZIP file. After decompressing, those interested can run it through the Applescript Editor app. Thanks again Kryten2 for your invaluable help in this! It really has made my day, and hopefully whoever else reading and having the same problems as well!


SUMMARY
For those who haven't followed through the entire thread, here's a short summary of what this is all about:
I've long since had a problem where iTunes wouldn't synchronize my contacts on my Mac with any iOS device (iPad, iPhone etc.). I've only attempted to sync via a USB wired connection, and don't know if syncing via Wifi would work better.
The problem (at least in my case) is that if any card (contact entry) in the Mac Contacts app contains either the "son" or "daughter" field then it won't sync! So the solution is to either remove those fields or replace them with something else. Luckily another field ("child") works and is indeed a perfect replacement, so the script above simply looks through all your contacts, and if it finds any instances with "son" or "daughter" it replaces them with "child". From then on you should be able to sync your contacts with an iOS device.


PS: I noticed something very interesting.....
As I said in an earlier post I realized than on an old iPad I have which runs iOS 9.3.5, when going to its Contacts app and editing any address I looked through all the options available when clicking on a field named "Add related name" and "son" or "daughter" just wasn't to be found.

But.... on my newer iPad (iOS 14.8) those fields ("son" and "daughter") are both available! This means that I can (on my iPad) create a new contact with "son" and "daughter" fields and interestingly enough it allows synchronizing with my Mac so my Mac-Contacts app gets updated with that o_O
But from that point on I can no longer sync (if I make a change or addition on either the Mac or iPad it won't be reflected on the other device).
The solution (once again) is to run the script above, then sync again. And of course keep in mind that you should refrain from ever using "son" or "daughter" again ;)
 

Attachments

  • Repair Contacts SON and DAUGHTER fields.zip
    1.7 KB · Views: 143
Last edited:
  • Like
Reactions: kryten2
AppleScript:
    tell application "Contacts"
        set label of (every related name of people whose label is "son" or label is "daughter") to "child"
        save
end tell
 
Huh?
This appears to be a simplified version of the original script, essentially doing the same thing but without the user-interaction alert windows, correct?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.