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

batch change address book phone number

Hello everyone,


I wonder is someone knows how do a batch change in address book. I'm changing phone operator and I have to change the phone number prefix from "041" to "015" to all my contacts. I've tried to find some applescript but I've found only to change the whole number. Any ideias?



Thank you



Leandro

Posted on Jun 3, 2012 4:55 AM

Reply
Question marked as Best reply

Posted on Jun 3, 2012 8:18 AM

Can you give me an example of the phone number string such as:

(041) 123456

+041 123456


I could search for 041 and replace all, but it would replace any 041 in the main number, also. So, I need to know how the number is formatted to write the replacement code correctly. If the first three characters are just 041, that's pretty easy:

property oldPrefix : "041"

property newPrefix : "015"


tell application "Contacts"

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 characters 1 thru 3 of theNumber = characters of oldPrefix then

try

set newNumber to my replace_chars(theNumber, oldPrefix, newPrefix)

set value of aPhoneNumber to newNumber

on error

copy name of aPerson to end of errorList

end try

end if

end repeat

if errorList is not {} then

display dialog "Couldn't change: " & items of errorList

end if

end if

end repeat


save

end tell


on replace_chars(this_text, search_string, replacement_string)

set AppleScript's text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the replacement_string

set this_text to the item_list as string

set AppleScript's text item delimiters to ""

return this_text

end replace_chars

11 replies
Question marked as Best reply

Jun 3, 2012 8:18 AM in response to ohdadi

Can you give me an example of the phone number string such as:

(041) 123456

+041 123456


I could search for 041 and replace all, but it would replace any 041 in the main number, also. So, I need to know how the number is formatted to write the replacement code correctly. If the first three characters are just 041, that's pretty easy:

property oldPrefix : "041"

property newPrefix : "015"


tell application "Contacts"

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 characters 1 thru 3 of theNumber = characters of oldPrefix then

try

set newNumber to my replace_chars(theNumber, oldPrefix, newPrefix)

set value of aPhoneNumber to newNumber

on error

copy name of aPerson to end of errorList

end try

end if

end repeat

if errorList is not {} then

display dialog "Couldn't change: " & items of errorList

end if

end if

end repeat


save

end tell


on replace_chars(this_text, search_string, replacement_string)

set AppleScript's text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the replacement_string

set this_text to the item_list as string

set AppleScript's text item delimiters to ""

return this_text

end replace_chars

Oct 7, 2012 10:54 AM in response to PJBG

Does they all start with 09?


If so, this should do it:


property oldPrefix : "09"

property newPrefix : "099"


tell application "Contacts"

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 characters 1 thru 2 of theNumber = characters of oldPrefix and characters 1 thru 3 of theNumber ≠ characters of newPrefix then

try

set newNumber to newPrefix &(characters 3 thru (number of characters in theNumber) of theNumber)

set value of aPhoneNumber to newNumber

on error

copy name of aPerson to end of errorList

end try

end if

end repeat

if errorList is not {} then

display dialog "Couldn't change: " & items of errorList

end if

end if

end repeat


save

end tell

Oct 7, 2012 5:05 PM in response to Barney-15E

Hello Barney ! Thanks for your time.


No, no all the numbers begins with 9 some of them begins with 08, 07, 06 etc but all of them begins with 0.


The other thing that i forgot to teell you is that i have too others numbers, hause phones... and that ones begins with 02 and with 2, like this: 02 2 390675



And some mobilephones(I need to change just mobilephones) also begins with 593 or +593 like this: 59393978452 and i need to add a 9 after 593 too. But hte problem is that these ones dont has a 0 as the others ones.


I hope that you can help me again ! 🙂



Thanks again !

Oct 8, 2012 1:37 PM in response to PJBG

That will show up after the script runs. I don't think it is a problem.


If you can list the pattern you want to change in the oldPrefix and put the new pattern into newPrefix, then change the number of characters it is looking for in these lines:

if characters 1 thru 2 of theNumber = characters of oldPrefix and characters1 thru 3 of theNumber ≠ characters of newPrefix then

try

set newNumber to newPrefix &(characters 3 thru(number of characters in theNumber) oftheNumber)

So, if your old prefix is 593, set the oldPrefix to 593 and set newPrefix to 5939

Then, change the number of characters like this:

if characters 1 thru 3 of theNumber = characters of oldPrefix and characters1 thru 4 of theNumber ≠ characters of newPrefix then

try

set newNumber to newPrefix &(characters 4 thru(number of characters in theNumber) oftheNumber)

The first number needs to be the length of the old prefix.

The second and third number needs to be one more than that.

To do +593, set oldPrefix to +593 and newPrefix to 5939 (or +5939).

Set the numbers to 4, 5, 5

Oct 15, 2013 2:15 PM in response to Barney-15E

Hi, I have a related issue.


I travel a lot lately and so have been collecting phone numbers from all over the world.


To make my contact info clear, I've started creating custom labels -- for examle, phone numbers from Great Britain have a "UK #" label, from the United States 'US #' etc.


With new numbers, I apply the labels straigt away -- but I have a couple hundred contacts from before I started this system. So my question is as follows:


Is it possible to make a script that will batch change the labels on multiple entries?


Ideally if it could set it based on the prefix (which I always add), meaning that +1 will result in a 'US #' label but a +44 number will score a 'UK #" one?

batch change address book phone number

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