Bulk edit Address Book contacts

Unfortunately Address Book doesn’t support bulk editing a large number of contacts at once, but there are a couple of clever workarounds to achieve the same effect. Here are two different methods, the first using TextEdit and the second using AppleScript.

Manually editing a vCard file

This method is great if you have a group of people who share the same information. For example, if you have a whole load of people at the same address, and they have all moved to a new address, or if a group of people share a phone number and they recently changed it to something new.

The first thing to do is to make a backup of your Address Book. Open up Address Book and choose Export - Address Book Archive from the File menu. If anything goes wrong in the following process, you can always just find this file and double-click it to restore your original Address Book.

Next, create a contact group by clicking on the plus (+) icon in the bottom left. Add all of the contacts you want to bulk edit into this group by dragging their names across from the contact list. Once this is done, drag the group from Address Book onto the Desktop to create a single .vcf file that contains contact information for everyone in the group.

The next step is to open this .vcf file with TextEdit. Either open TextEdit and choose “Open...” from the File menu, or just drag the .vcf file onto the TextEdit icon in the Dock. Now we are going to use TextEdit’s Find and Replace feature to find the existing information and replace it with the new information.

Choose “Find...” from the Edit menu or press Command-F. In the “Find” text field, enter the old information, such as the old address or phone number. You might have to look in the file to see how this is formatted. For example, addresses have semicolons to separate lines like

item2.ADR;type=HOME;type=pref:;;8 High Street;Earls Court;London;SW5 9EK;UK

Press the Next button a few times to check that it is entered correctly, then enter the new information in the “Replace with” field. Now just click the “Replace All” button and every instance of the old information should be replaced with the new information. Check the file to make sure it looks correct, and then save it.

Find and Replace

Finally, go to the Desktop and double-click on the .vcf file to import the contacts back into Address Book. You will be warned that the contacts are duplicates and will be updated. If you want, you can click “Review Duplicates...” to check that the changes are correct, or you can just click Import.

Using AppleScript

This second method is actually more flexible, but it does require that you get familiar enough with AppleScript to create your own script or modify one of my examples to suit your needs.

Start by opening up AppleScript Editor (located in Applications/Utilities). This is called Script Editor and is located in Applications/AppleScript if you are using Leopard or earlier.

Copy and paste the following lines into the AppleScript Editor window, and click the Compile button. If the AppleScript has been entered correctly, you should receive no errors.

tell application "Address Book"
    set peopleToChange to people whose (street of first address) contains "321 Old Street"
    repeat with thePerson in peopleToChange
        set (street of first address) of thePerson to "123 New Street"
    end repeat
    save
end tell

One of the great things about AppleScript is that it is designed to be readable by someone who knows nothing about AppleScript. Here’s a walk through what the above script does.

The first line says that we are about to tell Address Book to do something. The second line finds all the people in your address book whose street of their first address (people can have more than one address) contains “321 Old Street.” It then stores a list of all these people under the name peopleToChange. The third line says that we are going to go through the list peopleToChange one item at a time, and we will refer to each item in the list as thePerson. The next line sets the street of the first address of thePerson to “123 New Street.” Then the fifth line says we are finished going through the list peopleToChange, the sixth line saves the changes, and the last line says we are finished telling Address Book to do something.

You will have to create your own AppleScript to fit your exact needs, but the general structure of it will be similar to the above example. When you have created the script you want to use, just press the Run button to run the script and make the changes. Included below are a few more example scripts.

Find and replace phone numbers

tell application "Address Book"
    repeat with i from 1 to (count every person)
        set phoneProperties to properties of phones of person i
        repeat with j from 1 to (count of phoneProperties)
            if value of item j of phoneProperties contains "020 6704 3205" then
                set value of item j of phones of person i to "020 1523 6843"
            end if
        end repeat
    end repeat
    save
end tell

Clear the Notes field for every contact

tell application "Address Book"
    repeat with i from 1 to (count every person)
        set note of person i to ""
    end repeat
    save
end tell

A more advanced address replacement script

tell application "Address Book"
    repeat with i from 1 to (count every person)
        set theAddresses to properties of addresses of person i
        repeat with j from 1 to (count of theAddresses)
            if street of item j of theAddresses contains "321 Old Street" then
                set street of item j of addresses of person i to "123 New Street"
                set city of item j of addresses of person i to "NewCity"
                set zip of item j of addresses of person i to "12345"
                set country of item j of addresses of person i to "USA"
            end if
        end repeat
    end repeat
    save
end tell

Replace googlemail.com emails with gmail.com

tell application "Address Book"
    repeat with i from 1 to (count every person)
        set theEmailAddresses to properties of emails of person i
        repeat with j from 1 to (count of theEmailAddresses)
            if value of item j of theEmailAddresses contains "@googlemail.com" then
                set oldAddress to value of item j of theEmailAddresses
                set AppleScript's text item delimiters to {"@"}
                set firstBit to first text item of oldAddress
                set newAddress to firstBit & "@gmail.com"
                set value of item j of emails of person i to newAddress
            end if
        end repeat
    end repeat
    save
end tell

You can find more information about AppleScript in Address Book by going to “Open Dictionary...” in the File menu in AppleScript Editor and choosing Address Book from the list.

blog comments powered by Disqus