You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Need to add area code to all local contacts' phone numbers

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)

Posted on Jan 2, 2010 11:13 AM

Reply
9 replies

Jan 2, 2010 11:32 AM in response to kearnine

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

Jan 2, 2010 11:44 AM in response to kearnine

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.

Jan 11, 2010 3:24 PM in response to hadieandre

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

Mar 1, 2010 7:43 PM in response to Corbin S.

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.

Need to add area code to all local contacts' phone numbers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.