Contacts.app: Batch add existing contacts to group via text file and AppleScript #

tell application "Contacts"

    -- Path to file containing list of contacts (one per line) to add to group
    set groupList to "Macintosh HD:Users:foo:grouplist.txt"

    -- Read lines from groupList
    set groupMembers to paragraphs of (read file groupList as «class utf8»)

    -- Add each name to a pre-existing Contacts.app group named "Friends"
    repeat with i in groupMembers
        set aMember to (first person whose (name is i))
        add aMember to group "Friends"
    end repeat

    save

end tell

-- Any mismatch between names in grouplist.txt and Contacts.app will return an invalid index error along with the errant name, resulting in no names being added to the group.

-- Tested in macOS 10.12 and 10.13

-- Hat tip: mklement0's script: https://stackoverflow.com/a/30407353/

/mac | Sep 12, 2018


Subscribe or visit the archives.