I am trying to write a simple script to modify phone numbers in my address book so that “+1” preceedes each entry. I am close to a solution but still am having problems. Here is my code so far:
tell application "Address Book"
set all_people to every person
repeat with this_person in all_people
set all_phones to every phone of this_person
repeat with this_phone in all_phones
set phone_num to value of this_phone as string
set phone_num to "+1" & phone_num
set this_phone to phone_num
end repeat
end repeat
save addressbook
end tell
Anybody have any idea what I am failing to do? Im new to this!
PS-I probably should do a check if the phone number already has a “+1” in the beginning too
set default_country_code to "+30 " -- Country code for Greece.
tell application "Address Book"
set all_people to every person
repeat with this_person in all_people
set all_phones to every phone of this_person
repeat with this_phone in all_phones
set phone_num to value of this_phone
if (phone_num does not start with "+") then set phone_num to default_country_code & phone_num
set value of this_phone to phone_num
end repeat
end repeat
save
end tell
This puts "+30 " in front of any number which doesn’t already begin with “+”. You may want to refine this condition! Perhaps include a check on the country of the person’s address?
It’s not clear if you have a particular country in mind or several, but the script below should handle both. You have to enter your own relevant countries and dialling codes (in matching order) into the lists at the top of the script before running it.
Addresses aren’t linked to phone numbers in Contacts. I’ve crudely assumed that if any of a contact’s addresses contain a country, that country applies to all the person’s phones. Consequently, if a country in a contact adddress appears in the list at the top of the script, all that contact’s phone numbers will be given that country’s dialling code unless they already begin with “+”. It is of course possible to indicate the relevant country for a phone using the label next to it in the contact record, but the script would have to be written with the knowledge of the labelling convention used.
Since writing the script above, I’ve taken to reading all the relevant data from Contacts at once and sifting through the resulting lists, which is faster than continually asking Contacts for individual bits of information.
-- Example countries and dialling codes. Substitute your own.
set relevant_countries to {"Greece", "UK", "USA", "Canada"}
set dialling_codes to {"+30 ", "+44 ", "+1 ", "+1 "}
-- Get the ID, address countries, phone IDs, and phone numbers of every contact in Contacts.
tell application "Contacts"
set {contact_IDs, countries, {phone_IDs, phone_numbers}} to {id, country of addresses, {id, value} of phones} of every person
end tell
-- Examine the relevant data for each contact in turn.
repeat with p from 1 to (count contact_IDs)
-- Search this contact's address countries for any listed in the 'countries' list at the top of this script.
set this_contacts_countries to item p of countries
repeat with c from 1 to (count this_contacts_countries)
set this_country to item c of this_contacts_countries
if (this_country is in relevant_countries) then
-- If a match is found, get the corresponding dialling code from the 'dialling_codes' list.
repeat with cy from 1 to (count relevant_countries)
if (item cy of relevant_countries is this_country) then
set this_code to item cy of dialling_codes
exit repeat
end if
end repeat
-- Search through this contact's phone numbers for any not beginning with "+".
set this_contacts_phone_numbers to item p of phone_numbers
repeat with ph from 1 to (count this_contacts_phone_numbers)
set this_number to item ph of this_contacts_phone_numbers
if (this_number does not start with "+") then
-- Where a number doesn't begin with "+", create a new version beginning with the country's dialling code.
set edited_number to this_code & this_number
-- Using the relevant contact and phone IDs, set the new version of the phone number in Contacts itself.
set contact_ID to item p of contact_IDs
set phone_ID to item ph of item p of phone_IDs
tell application "Contacts"
set value of phone id phone_ID of person id contact_ID to edited_number
end tell
end if
end repeat
end if
end repeat
end repeat
-- Save the changes and make them visible in Contacts.
tell application "Contacts" to save
Edit: Corrected the mistyped dialling code for Greece.