Finding Ungrouped Contacts in Address book

In OS X Lion there is a bug preventing the standard Smart Group criteria for selecting contacts not in any group from working. This is quite irritating if you wish to find all contacts not in any group. 

So, I wrote a script to find all contacts not in any group, and dump them into a group called 'Orphans'.

It will only create the group if necessary. It will tell you how many orphans you have.

This is the script:

-- Finds ungrouped contacts and puts them into a group called Orphans


set orphanCount to 0


tell application "Address Book"

-- Create new group, if necessary

if not (group "Orphans" exists) then

make new group with properties {name:"Orphans"}

end if

-- Remove old orphans

set oldOrphans to every person in group "Orphans"

repeat with oneOrphan in oldOrphans

remove oneOrphan from group "Orphans"

end repeat

-- Add new orphans

set thePeople to every person

repeat with thisPerson in thePeople

if number of groups of thisPerson = 1 then

add thisPerson to group "Orphans"

set orphanCount to orphanCount + 1

--display alert "Orphan found: " & name of thisPerson

end if

end repeat

display alert "Orphan count: " & orphanCount

--Remove group if not needed.

if orphanCount is 0 then

delete group "Orphans"

end if

save

end tell


You can open up AppleScript Editor Utilities, paste in the script above, and run it. Or, alternatively, and easier - just download this script, and run it.

© Alex Wasserman 2011