Addressbook Sync problems in MacOSX

I have got ghost records in my addressbook. Records that have only an email address in them. The problem is that there are now and then thousands of those records. I have also, like many others, got problems with the updated format of images in the records in MacOSX 10.5.x. Many findings on the net suggest doing complete reset of the sync database on all computers (iPhone etc), but I do not want that. Why should I? Then rumors said the sync problem would go away in 10.5.3, but it did not. Or rather, the image syncing is better (but not resolved), but I get these bogus records.

To remove all of those records, I could either select those (one can find them in the Address Book by searching), do select-all, and then delete. The problem is that the “select-all” operation was never succeeding with the 16k bogus records. I had to kill Addressbook application after 4-5 hours with 100% CPU load. I could mark about 50 records at a time, and do manual delete, confirm etc, but each one of those took 20-30 seconds. Not a good solution either.

So I wrote an AppleScript. When running it, I could delete about 200 records every 25 minutes. Good enough. After running this, the 16k bogus records where gone. But, of course about 2k of them came back when syncing (again) with my iPhone that seems to have got some of them from my Mac. I am happy I wrote this script, as I now could run it again. Much faster when only having 2k bogus records. But still slow.


tell application "Address Book"
	set r to 0
	set nr to 0
	set pp to every person
	repeat with p in pp
		set ee to every email of p
		repeat with e in ee
			if (value of e) = "bogus-email@example.com" then
				set r to 1
			end if
		end repeat
		if r = 1 then
			delete p
			set nr to nr + 1
			if nr = 200 then
				save addressbook
				set nr to 0
			end if
		end if
		set r to 0
	end repeat
	save addressbook
end tell

AppleScript that remove records with certain email address