Thursday, July 26, 2012

iCloud group replication resolved by AppleScript

It's been a month since my disastrous MobileMe to iCloud migration. Over the course of a week I figured out a convoluted solution for iCloud's CRLF/LF end of line bug, but I was left with "metastatic group replication" in my primary iCloud account/quaternary AppleId [1]. Each of about five Address Book groups had been replicated about 50-100 times (only one of the replicants has associated contacts).
The Groups were in iCloud - not Address Book. So even deleting my Address Book Library files didn't remove them. Bento couldn't see them. Mountain Lion didn't help. The only option appeared to be tedious manual deletion. My iCloud account was poisoned.
Today I figured out a solution. I had to learn a bit of AppleScript [2] but I'll recover. It was worth it (in ML Address Book has been renamed Contacts):
tell application "Contacts"
repeat 50 times
set theGroup to group "MyGroupName"
delete theGroup
end repeat
end tell

It's a crude hack. I had to change "MyGroupName" five times, and when the replicant count dropped I had to reduce the repeat number so it would execute. (One could drop the count  parameter and see if the repeat loop stops when it errors with "missing value"; that would be much faster. I was being cautious.)

At one point Contacts didn't respond very well. I quit and restarted and completed the process.

When I was done I confirmed iCloud's web view was also Group free. Then I purged all the Contacts in that account [3], it's now pristine.

I'm sure an AppleScripter could turn this into a general script for removing all groups all at once. Or could get very clever, and only remove groups that had no contacts (so one could salvage the group/contact relationships).

I didn't need that. My problem has been fixed.

Incidentally, I think iCloud sync works a lot better in ML than Lion. iCloud updated very quickly. 

[1] I have 4 AppleIDs. Two or three of them have iCloud accounts. One has all of my iTunes and AppStore purchases. One was my MobileMe account. A third, which I've never used, acquired, apparently by email transfer, all of my Apple device records. Does Apple have any idea how screwed up their AppleID/iCloud accounts are?

[2] Of which Jamie Zawinski wrote: "I used to think that PHP was the biggest, stinkiest dump that the computer industry had taken on my life in a decade. Then I started needing to do things that could only be accomplished in AppleScript."

[3] They're safe in Snow Leopard until I move my primary machine to Mountain -- which might be sooner than expected. ML seems to be everything Lion should have been.


  1. Spent hours on this problem myself. I have tried this script, and it runs but result is
    missing value
    Any idea what happened? I am a first timer at apple srcipting, (did the tutorial you suggested on Apple Forum. I am so unhappy and tears are streaming.

  2. Sarah, I think your best bet is to find a friend who is a Mac geek and ask them to help run the script. Everyone needs a local Mac geek. They will usually work for wine or beer.

  3. Well, turns out it DID work. I just had so many of the "untitled groups" (10,000+) that it didn't seem to be working at first. The system was over burdoned with the number of times I was running it and really slowed down, but the good news is it worked and then I drank the wine.