11 Replies Latest reply: Jun 12, 2014 5:13 AM by Barney-15E
ohdadi Level 1 Level 1 (0 points)

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

  • Barney-15E Level 8 Level 8 (42,680 points)

    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

  • ohdadi Level 1 Level 1 (0 points)

    Barney,

     

    it worked like a charm!!!! Yes, the numbers are the first 3 characters!!

    THANK YOU THANK YOU THANK YOU!!!!

  • PJBG Level 1 Level 1 (0 points)

    Hello Everyone,

     

     

     

     

    I am trying to do the same, in Ecuador we need to add a 9 in all my numbers, Could you help me please ? The numbers here are like this: 093978454 and i need to convert to 0993978454. I mean that i need to add a 9 after the first 0.

     

     

    Thank you for your time

  • Barney-15E Level 8 Level 8 (42,680 points)

    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 theNumbercharacters 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

  • PJBG Level 1 Level 1 (0 points)

    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 !

  • PJBG Level 1 Level 1 (0 points)

    When I use your formula I' ve got an error it appears as " missing value".

  • Barney-15E Level 8 Level 8 (42,680 points)

    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 theNumbercharacters 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 theNumbercharacters 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

  • pHghost Level 1 Level 1 (0 points)

    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?

  • pHghost Level 1 Level 1 (0 points)

    Resolved it with some help on another site, thanks!

  • Francisco Dreux Level 1 Level 1 (0 points)

    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'm a young Jurassic guy, please in plain"English"

     

    Thank you

    Francisco

  • Barney-15E Level 8 Level 8 (42,680 points)

    Sorry, there isn't a "plain English" way of batch changing contacts. You must use Applescript which is considered an "English-like" programming language. The exact example you ask is posted as the solution to this question.