9 Replies Latest reply: Mar 1, 2010 7:43 PM by Barney-15E
kearnine Level 1 Level 1 (0 points)
My area recently made a change requiring that the local area code be dialed for each local phone number. My local Address Book/iPhone contacts are all entered without the area code. Is there a way to make a batch change that would add the area code to those numbers? My cheapo house phone from Walmart can do it...

Thanks in advance.

MacBook Pro, iPhone 16GB, Mac OS X (10.4.7)
  • V.K. Level 9 Level 9 (56,130 points)
    paste the following into AppleScript Editor. it's located in /Applications/Utilities. change 555 in the script to your area code.
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px;
    color: #000000;
    background-color: #ADD8E6;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    tell application "Address Book"
    set mlist to selection
    repeat with eachPerson in mlist
    repeat with eachNumber in phones of eachPerson
    set theNum to (get value of eachNumber)

    set value of eachNumber to "555" & theNum

    end repeat
    end repeat
    save
    end tell
    </pre>


    select the contacts in your phone book whose numbers you want to change and press run in Script Editor. this will add 555 to the front of all phone numbers of all selected people
  • Barney-15E Level 8 Level 8 (40,035 points)
    Open up the Applescript Editor and paste in this script
    property myAreaCode : "(505) "
    tell application "Address Book"
    repeat with aPerson in people
    set thePhones to phones of aPerson
    if thePhones is not {} then
    set errorList to {}
    repeat with aPhoneNumber in thePhones
    set theNumber to value of aPhoneNumber
    if length of (theNumber) is less than 10 then

    try
    set value of aPhoneNumber to myAreaCode & value of aPhoneNumber
    on error
    copy name of aPerson to end of errorList
    end try
    end if
    end repeat
    if errorList is not {} then
    display dialog errorList
    end if
    end if
    end repeat
    save
    end tell
    Change the myAreaCode property to reflect your area code and the format you want (the part inside the quotes). E.g. if you want to use dashes as separators, put "505-" inside the quotes.
    You may also want to verify if "less than 10" will find all of your phone numbers. Remember that the phone number is a text string, so 123-4567 is eight characters, not seven. I chose 10 to account for using a US number with area code and no punctuation (e.g. 5051234567).

    I also couldn't verify if my error tracking would work as I couldn't generate a failure to test it. So, if you do have errors, you may get an error when it tries to display the list of unchanged numbers.
  • V.K. Level 9 Level 9 (56,130 points)
    Barney's script is better than mine as it automatically skips numbers that have area codes already and does some error checking so use his script instead.
  • kearnine Level 1 Level 1 (0 points)
    You guys are the best!! Thank you so much. I will try it right now and let you know if it worked ...
  • kearnine Level 1 Level 1 (0 points)
    Fantastic! It worked perfectly. I did use the second one, as both of you recommended that.

    Thank you very much.

    Caroline
  • hadieandre Level 1 Level 1 (0 points)
    I would like to change the area code in a batch too, but I need first to remove a 0 at the beginning of each number. How should I change your code to do that??

    For example, this number:

    020 1234567

    should become:

    +31 20 123456
  • Barney-15E Level 8 Level 8 (40,035 points)
    If you want to change all the start with a 0, then this will work
    property myAreaCode : "+31 "
    tell application "Address Book"
    repeat with aPerson in people
    set thePhones to phones of aPerson
    if thePhones is not {} then
    set errorList to {}
    repeat with aPhoneNumber in thePhones
    set theNumber to value of aPhoneNumber
    if theNumber begins with "0" then

    try
    set value of aPhoneNumber to myAreaCode & (characters 2 thru (number of characters of theNumber) of theNumber)
    on error
    copy name of aPerson to end of errorList
    end try
    end if
    end repeat
    if errorList is not {} then
    display dialog errorList
    end if
    end if
    end repeat
    save
    end tell
  • Corbin S. Level 1 Level 1 (0 points)
    How do I add a number "1" before all my US contacts' phone numbers? I could probably use this same script and just replace the area code with "1", right? I'm sorry for my newbiness, but I tried opening applescript editor, and I don't know where to go from there.

    Any help is appreciated.

    Thanks
    Corbin
  • Barney-15E Level 8 Level 8 (40,035 points)
    There are a lot of problems with that. If you have only US phone numbers, it is simpler. If you have numbers from other countries, then this is my best guess at getting them changed. It first checks to see if there is a US address. If it does, it then checks to see if there are 10 numbers. If that is ok, it adds the 1. However, it won't catch those without an address.
    tell application "Address Book"
    set errorList to {}
    repeat with aPerson in people
    set USAddress to false
    repeat with anAddress in addresses of aPerson
    if country code of anAddress is "us" then set USAddress to true
    end repeat
    if USAddress is true then
    set thePhones to phones of aPerson
    if thePhones is not {} then
    repeat with aPhoneNumber in thePhones
    set theNumber to value of aPhoneNumber
    if length of theNumber is 10 then

    try
    set value of aPhoneNumber to 1 & theNumber
    on error
    copy name of aPerson to end of errorList
    end try
    end if
    end repeat
    end if
    end if
    end repeat
    if errorList is not {} then
    display dialog "Change failed for " & errorList
    end if
    save
    end tell
    I did limited testing on this, but it seemed to work. Before running it, you should export your addresses to an Address Book Archive (File Menu). If you need to recover them, you just have to double-click the address book archive.