Address Book phone num update

Hello,

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

I’m searching for the same thing for my country…

it’ll be great if someone completes that…

Model: MBP 15" Late 2010
Browser: Safari 7534.48.3
Operating System: Mac OS X (10.6)

Hi. Welcome to MacScripter.

This should work:

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?

How to make the change depending on the country?

Hi. Welcome to you too.

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. :slight_smile:

You forgot some codes very important for Apple :

Ireland : +353
Luxemburg : +352
Barbados: +1246
Bahamas: +1242
Vanuatu: +678
Belize: +501
Brunei: +673
Dominica: +1767
Samoa-Américaines: +1684
Samoa-Occidental: +685
Seychelles: +248
St. Lucia: +1758
St. Vincent and Grenadine: +1784
Turks and Caicos: +1649

I wonder if for these codes it wouldn’t be more logical to replace the [+] by a [-] :rolleyes:

Yvan KOENIG (VALLAURIS, France) dimanche 8 février 2015 20:42:56

Nope, you need the ‘+’ t dial out, you can view it as the ‘/’ signifying the root directory, but here the directory is a country code.

Hello McUsr

It seems that I was joking about Apple’s fiscal behaviour.

The listed countries are some of the ones defined as “paradis fiscal”.

Yvan KOENIG (VALLAURIS, France) lundi 9 février 2015 09:46:56

:slight_smile:

(The big grin isn’t working for some reason.)