Sync only Address Book entries with phone numbers to iPhone

Oct 23, '09 07:30:00AM

Contributed by: bigkm

I used to have a mobile phone which I synced all the time with iSync, where there was an option to only sync contacts with phone numbers. After getting an iPhone, I noticed this didn't exist. So why not use a Smart Group to do the job? Because they don't sync with the iPhone. But I also wanted everyone's emails and other details.

So I wrote up an AppleSript that updates a regular Address Book group with only people who have phone numbers. This worked great, then after a while, I wanted some numbers to not show up (people you want to keep in your phone but will never call, or whose name you don't want to see every time you scroll down your list). I created a group called Not Phone, and modified my script so that they are removed from the Phone group.

This script will create the groups for you. Warning: if you have a group called Phone already in existence, I suggest renaming the group (or changing the group name that the script uses in the first line), as that group will be modified. Here's the code:

property thePhoneGroup : "Phone"
property theNotPhoneGroup : "Not Phone"
on createGroup(str)
  tell application "Address Book"
    try
      get group str
    on error
      make new group with properties {name:str}
      save
    end try
  end tell
end createGroup

tell application "Address Book"
  my createGroup(thePhoneGroup)
  my createGroup(theNotPhoneGroup)
    
  set allContacts to every person
  
  repeat with p in allContacts
    if (count of phone of p) is 0 then
      -- Dont have a number
      -- if they are in the group then remove them
      if (name of groups of p contains thePhoneGroup) then remove p from group thePhoneGroup
    else
      -- They have a number, add to the group
      if (name of groups of p does not contain thePhoneGroup) then
        add p to group thePhoneGroup
      end if
      if (name of groups of p contains thePhoneGroup and name of groups of p contains theNotPhoneGroup) then remove p from group thePhoneGroup
    end if
  end repeat
  save
end tell
So now I can just leave the iPhone set to sync the Phone group, and I have no problems.

[robg adds: This worked as described for me in OS X 10.5.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091008150146513