Applescript to find contacts that are not in any group

applescriptcontacts

I have been using a script that does not work anymore in mountain lion even though I have changed the words "address book" to "contacts".

If you select all the contacts, and then use the script, it will find all those contacts that are not in a group for you. The short script is available at MacOSX hints:

http://hints.macworld.com/article.php?story=20051126084705352

Can anybody modify this to work again? Thanks in advance.

Best Answer

Try this, I have a huge contact list so testing it fully on mine would take a billion years. But the initial tests work

These sort of script are really only handy on small lists as the overhead with all the repeating is exponential the larger they get.

property orphan : "Orphans"

tell application "Contacts"
    if not (exists group orphan) then

        (*Create the new group*)
        make new group at the end of groups with properties {name:orphan}
    end if

    --set thePeople to first person whose first name is "TEST"
    (*get all entries*)
    set thePeople to every person

    (*iterate through the entries*)
    repeat with i from 1 to number of items in thePeople

        (*get the entry*)
        set this_person to item i of thePeople

        (*get the entries group list *)
        set inGroup to name of group of this_person

        if inGroup is {} then
            (* entry is not in any group*)
            add this_person to group orphan
            save


        else if inGroup contains "Orphans" and inGroup is not equal to {"Orphans"} then
            (*if entry is already in a group/s then check it is not also inĀ¬
             Orphans or only in orphans*)

            (* entry is in orphans and another group so remove it from orphans*)
            remove this_person from group orphan
            save
        end if
    end repeat
    save
end tell