How can I batch edit the country codes in my contacts?

Greetings everybody:


My contacts (address book) on mountain lion, which is synced with my iphone via icloud, is a mess right now because I have phone numbers from different countries but with no country codes at the begining.


I know this will take multiple steps. I tried to use automator flowchats but I couldn't find a "mobile" or "phone number" item in the "Filter contacts" order, so I now believe this is best done with applescripts, hence I need help.


Basically what I need, is for an applescript to fetch through my contact numbers (all categories, iphone, home, work, mobiles) and check if there's a + sign at the begining, if yes, then leave it as is. If no then I will need to add some country codes according to the first digits. For example, if the starting number is (03) or (01) then I need to add a +20 to the begining of the number. There are other numbers that I will need to batch with the relevant country codes, but I think I can edit the applescript accordingly.


I have no experience whatsoever with applescripts, so I would appreciate any help.


Thanks.

MacBook Pro, OS X Mountain Lion (10.8.2)

Posted on Mar 13, 2013 12:12 AM

Reply
17 replies

Mar 16, 2013 6:10 AM in response to bisho79

Make sure you Archive you contacts before runninng this so you can go back to the start if it mucks things up.

File Menu, Export > Contacts Archive


I set this up to work on a single group called "Test Group"

You can run it on idividual groups by changing "Test Group", or

You can run it on all contacts by removing:

ofgroup "Test Group"



tell application "Contacts"

repeat with aPerson in people of group "Test Group"


-- get all the phone numbers for a person

set thePhones to phones of aPerson

if thePhones is not {} then


-- person has at least one phone number

set errorList to {} -- initialize empty error list

repeat with aPhoneNumber in thePhones

set theNumber to value of aPhoneNumber

try

set value of aPhoneNumber to my getNewNumber(theNumber)

on error

copy name of aPerson & return to end of errorList

end try

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 getNewNumber(aNumber)

if aNumber starts with "03" or aNumber starts with "01" then

return "+20" & aNumber

else if aNumber starts with "04" then

return "+30" & aNumber


-- Add else ifs for each possible replacement


-- else if aNumber starts with " " then


-- return "+ " & aNumber

else


-- no match, so just return the original number

return aNumber

end if

end getNewNumber

Mar 17, 2013 8:46 AM in response to Barney-15E

Thanks Barney.


In your post dated

Re: How can I batch edit the country codes in my contacts?Mar 16, 2013 9:22 AM(in response to bisho79)a


The number 2 script that finds contacts beging with 4 or 5. It gives application time out error and gives me only a partial list thats not scrollable, meaning I could not get to the end of the list, only what fits one screen, is there a way to fix this?

Mar 16, 2013 8:47 AM in response to Barney-15E

Thanks. It worked perfectly.


I used other combinations for the rest of my country codes mess, and they worked, all except 2.


The 2 situations that are left with me, are one that I need to search all the numbers that begin with 00 and REPLACE it with a +, not add to it. How can I change the script to do that?


The second one is more tricky. I have some numbers that does not start with any zeroes whatsoever. They just begin with a 4 or a 5. These numbers actually could belong to a number of countries, and the only way to know is by going through them one by one and check the name and other information and add the relevant country and city code manually. The thing is, if I do a contacts search for a 4 or a 5 on my mac contacts or on my iphone, it gets all phone numbers that includes a 4 or 5 and not ones that only starts with them. How can I use an apple script or an automator flow chart (or other means) to show me the numbers that only STARTS with these digits?


Thank you so much for your help.

Mar 16, 2013 9:22 AM in response to bisho79

1)

tell application "Contacts"

repeat with aPerson in people of group "Test Group"


-- get all the phone numbers for a person

set thePhones to phones of aPerson

if thePhones is not {} then


-- person has at least one phone number

set errorList to {} -- initialize empty error list

repeat with aPhoneNumber in thePhones

set theNumber to value of aPhoneNumber

try

set value of aPhoneNumber to my getNewNumber(theNumber)

on error

copy name of aPerson & return to end of errorList

end try

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 getNewNumber(aNumber)

if aNumber starts with "03" or aNumber starts with "01" then

return "+20" & aNumber

else if aNumber starts with "00" then

return "+" & (characters 3 thru (number of characters of aNumber) of aNumber)


-- Add else ifs for each possible replacement


-- else if aNumber starts with " " then


-- return "+ " & aNumber

else


-- no match, so just return the original number

return aNumber

end if

end getNewNumber


2)

tell application "Contacts"

set peopleList to {}

repeat with aPerson in people


set thePhones to (phones of aPerson)

if thePhones is not {} then

set foundMatch to false

repeat with aPhone in thePhones

if value of aPhone starts with "4" or value of aPhone starts with "5" then

set foundMatch to true


end if

end repeat

if foundMatch then

copy name of aPerson & return to end of peopleList

end if

end if

end repeat


display dialogpeopleList as string

end tell

Mar 16, 2013 10:22 AM in response to Barney-15E

Wow.


All your scripts works perfectly. I could not thank you enough.


If I can ask one more of you, and maybe it could help others as well.


The labelling of the numbers are a miss. They are all labelled "mobile". There is of coarse a smart way to know if this "mobile" or "work/home" by asking the program to look at the area code. For example the country code is +20, followed by an area code of 12, or 11, then is is a mobile, of 2 or 3 then this is a land line.


I know I have already asked too much, and got invaluable help from you Barney, but is there a script to fix this?


Thank you, thank you, thank you 🙂

Mar 16, 2013 12:16 PM in response to bisho79

I put in a test to see if it is a company or not, then used that to choose between "home" and "work"

However, that may not always be true, so you may have to go back to do some manual editing.

tell application "Contacts"

repeat with aPerson in people of group "Test Group"


set thePhones to (phones of aPerson)

if thePhones is not {} then

repeat with aPhone in thePhones

if value of aPhone starts with "+2011" or value of aPhone starts with "+2012" then

set the label of aPhone to "mobile"

else if value of aPhone starts with "+202" or value of aPhone starts with "+203" then

if company of aPerson is true then

set the label of aPhone to "work"

else

set the label of aPhone to "home"

end if

end if

end repeat

end if

end repeat


save

end tell


There are also department and Job Title fields that might help. If you don't have the Company checkbox set, you could use this instead.

if department of aPerson is not "" or job title of aPerson is not "" then

set the label of aPhone to "work"

else

set the label of aPhone to "home"

end if

Mar 17, 2013 11:08 AM in response to bisho79

I hope it timed out because you couldn't click the Ok button.


Try this, it creates a TextEdit document with the matching names.

tell application "Contacts"

set peopleList to {}

repeat with aPerson in people


set thePhones to (phones of aPerson)

if thePhones is not {} then

set foundMatch to false

repeat with aPhone in thePhones

if value of aPhone starts with "4" or value of aPhone starts with "5" then

set foundMatch to true


end if

end repeat

if foundMatch then

copy name of aPerson & return to end of peopleList

end if

end if

end repeat

tell application "TextEdit"


makenewdocumentwith properties {text:peopleList as string}


activate

end tell

end tell

Mar 18, 2013 7:22 AM in response to Barney-15E

The script that changes the labels of the phone numbers was not working properly. I then ealised the problem was with the line:


ifcompanyofaPersonistruethen


The "true" value did not apply unless I checked the check box next to company on my mac contacts, but did not apply when it was simply filled up by text but the check box was not ticked, so I borrowed a command from your alternative script regarding the job tile. So finally, that line went as follows:


if company of aPerson is not "" or department of aPerson is not "" or job title of aPerson is not "" then


This way, whenever the script found any text in the company section or the department section or the job title, it changed the label to work, and it worked perfectly. Thank you.

Feb 5, 2015 8:12 AM in response to bisho79

Hi, great-great post. I have a similar issue, but more complex.


First, I need the system to identify EVERY phone number (even the ones labeled "other") that does not start with "+54911", and do the following:

If they start with "15" and the number is 10 digits long, the system must replace the "15" with "+54911". If the label is not "mobile", it should turn it into "mobile".

If they start with "15" and the following numbers are "11" or "011" (regardless of the phone's character length), the system must replace the "1511" or "15011" with "+54911". If the label is not "mobile", it should turn it into "mobile".

All other phone numbers that do not meet this criteria should be left alone.


Then (after the above process has completed), I need the system to identify every MOBILE phone number that does not start with "+54911", and of those mobile phone numbers (the ones which do not start with "+54911"), do the following:

If they start with "11" or "011", the system must replace the "11" and "011" with "+54911".

If they don't start with "15", "11" or "011" and are 8 digits long, the system must ADD "+54911" to the beginning.



Sorry if I'm complicating something which can be explained in a simpler way, but I'm not a programmer!


A big thanks in advance to anyone who can help me out on this one.


Cheers,

Charles

Feb 5, 2015 8:13 AM in response to Barney-15E

Hi, great-great post. I have a similar issue, but more complex.


First, I need the system to identify EVERY phone number (even the ones labeled "other") that does not start with "+54911", and do the following:

If they start with "15" and the number is 10 digits long, the system must replace the "15" with "+54911". If the label is not "mobile", it should turn it into "mobile".

If they start with "15" and the following numbers are "11" or "011" (regardless of the phone's character length), the system must replace the "1511" or "15011" with "+54911". If the label is not "mobile", it should turn it into "mobile".

All other phone numbers that do not meet this criteria should be left alone.


Then (after the above process has completed), I need the system to identify every MOBILE phone number that does not start with "+54911", and of those mobile phone numbers (the ones which do not start with "+54911"), do the following:

If they start with "11" or "011", the system must replace the "11" and "011" with "+54911".

If they don't start with "15", "11" or "011" and are 8 digits long, the system must ADD "+54911" to the beginning.



Sorry if I'm complicating something which can be explained in a simpler way, but I'm not a programmer!


A big thanks in advance to anyone who can help me out on this one.


Cheers,

Charles

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I batch edit the country codes in my contacts?

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