Tuesday, December 11, 2012

Dealing with a weird problem with Mail.app and Contacts.app using different AddressBook files on OSX Mountain Lion

Recently I had to switch my Apple MacBook. I used the Apple Migration Utility to transfer my profile from one machine to another. This worked fine and everything seemed to be okay. However After a week or so of using the new machine there was one strange problem: Mail.app would only find email addresses from the recent list. I found that I couldn't lookup email addresses from my AddressBook in the Contacts app. So how to fix this? This is the story. It was quite a challenge.

Whenever I get a problem like this I resort to "The Google" and see if others have had a similar problem. There were plenty of reports of problems with syncing AddressBook with iCloud, However, my iCloud syncing and Facebook integration was all working fine. Just to be certain I first did a backup of my AddressBook from the Contacts app. I recommend doing this before any major changes to your Contacts App since your contact database is often one of your most valuable resources.

To do the backup I went to Contacts and did File...Export...Contacts Archive to create the backup.

I followed some of the advice online and tried to reset my Contacts database. I quit the Contacts.app and Mail.app. I then went to System Preferences...iCloud and switched off iCloud Contacts syncing.  

I opened Contacts.app and checked my contacts were still there. All good so far. But opening Mail.App there was no change. I still couldn't get mail to lookup email addresses for any of my contacts.

I closed both Contacts and Mail.app and went back to System Preferences...iCloud and switched contact syncing back on. After waiting for iCloud to re-sync with Contacts I opened Mail.app and Contacts. No change. Mail.app would still not find any of my contacts email addresses.

I then went to Mail.app and checked out the Address Panel. There were only two AddressBook entries. The first was for Apple and the second was My AddressBook card. 

I tried adding an email address from Mail.app in to the AddressBook. This allowed the email address to found in Mail.app but the entry did not show up in the Contacts.app. At this point it was obvious that the two applications were not talking to each other. They were working off different copies of the AddressBook database.

At this point it was time to take a look under the covers. Time to  open up Finder and take a look through my user profile Library folder (~/Library).

My first visit was to ~/Library/Application Support/AddressBook. 

The AddressBook-v22.abcddb file was large and looked to be too big to have just 2 records in it.

I went back to Google to search for some more ideas.  One article mentioned the Containers folder in ~/Library. This is apparently part of the sandboxing that has been implemented in OS X Mountain Lion.

I took a look in ~/Library/Containers and found a com.apple.mail folder. 

This folder had a Container.plist file and a Data folder.

The Data Folder held two Folders:

- Documents
- Library

The Library folder was really interesting it had a number of folders that mimicked the folders in the ~/Library folder:

Application Scripts
Application Support
Caches
Cookies
Images
LaunchAgents
Logs
Mail
Mail Downloads
Notes
Preferences
Saved Application State
SyncedPreferences

I looked in the Application Support folder and found:

AddressBook
CrashReporter
Mail
SyncServices 

I looked in the AddressBook folder and found the following files and folders:

AddressBook-v22.abcddb
Configuration.plist
Images
MailRecents-v4.abcdmr
Metadata

The contents of the Images and Metadata folders matched the AddressBook entries that were being used by Mail.app.

Finally I had conclusive proof that Mail.app was using a different AddressBook file and I had found the database it was using. The next step was to figure out a way to fix it. The challenge is that there is nothing in Mail.app's preferences to force it to use a different contacts database and the only way to re-install Mail.app is to re-install OS X. I wasn't ready to do that. So there must be another way.

After taking copies of both AddressBook folders I moved the AddressBook folder in ~/Library/Containers/com.apple.mail/Data/Library/Application Support/
to AddressBook_broken inside the Application Support folder.

I then went to ~/Library/Application Support in Finder and created an Alias of the AddressBook Folder. I then moved it to the ~/Library/Containers/com.apple.mail/Data/Library/Application Support/ folder and renamed it to remove the " alias" in the folder name.

I then tried Mail.app but no luck. As I suspected Aliases don't work very well. It was time to resort to Terminal. First I deleted the Alias that wasn't working

I decided to try a symbolic link. A quick Google to get the syntax right and the following steps worked:

cd ~/Library/Containers/com.apple.mail/Data/Library/Application\ Support/

ln ln -s ~/Library/Application\ Support/AddressBook AddressBook

I then fired up Contacts.app and Mail.app. It worked! Contacts was using the AddressBook database and Mail.app was too.  To make sure everything was okay I added an email address using mail.app. I switched to Contacts.app and the entry appeared there too.  I then edited the entry in Contacts.app and added a picture to the entry.

While I am at this point I have to point out the retarded change in Contacts.app in the handling of pictures. in the Lion version of AddressBook you could simply drop a picture on to the Picture Frame in an AddressBook Contact Card and the card would be updated. In Mountain Lion it is a major step backward in usability. You now have to switch in to edit mode for the contact record. Then you have to go and edit the picture. Then you have to select the "Recents" tab. Finally you can drag and drop a picture in to the edit window and select Done. 

After doing this and saving the Contact Card I went back to Mail.app and made sure the preference...Viewing tab had the option set to "show contact photos in the message list."

Now the updated photo displayed in Mail.app against an email from the addressee. This is a really useful feature because when you are scanning through a list of emails it is easy to spot the emails from people you are interested if you have added a picture to their contact card because you will see that picture next to the email message instead of the grey head and shoulders profile.

While I was troubleshooting this problem I did call Apple. They were incredibly efficient. They called back exactly when I scheduled them to. Within minutes after discussing the issue with the Support Rep (I had solved the problem at this point - but I was concerned that an update to OS X might break it)  I was connected within two minutes to the next level expert. It seemed I had solved the problem. However, there seems to be a problem lurking here. It shouldn't be this hard. The fix is definitely beyond the scope of many users. 

The support rep gave me their contact details and when I got home I found an email address asking if I could document the steps I went through. So Glen, this is for you.... Rather than keep the solution to myself I have written this up and posted the information to my blog where  others may find it and hopefully give a clue to solve the problem if others experience this.