Creating a "calculated" phone field in address book

I’m trying to create an Applescript to create a “calculated” phone field in Address Book, but I had no success so far.

I store the phone numbers in international format (eg.: +1 123 456 7890 or +55 12 1234 5678) and I use it to dial from the computer (skype). I have those numbers in sync with iPhone but due to local dialing rules I have to replace +55 (for Brazil) with 041 for Brazilian numbers and + with 0041 (for all other numbers).

The script I’m trying to create would check every phone number of a contact and create an additional field with the original tag + “X” and populate it with the replaced digits: eg. if Mobile is +55 12 3456 7890 it would create “Mobile X” and set the number 041 12 3456 7890. Before doing that it would have to check if Mobile X already exists and in that case it would not create the field.

Does anyone did something similar to that and have ideas on how to deal with all the phone numbers of a contact?

Thanks, Bruno

Hi,

try this, I recommend run it first with one group, replace “myGroup” with a valid group name
To consider all contacts, comment out of group “myGroup” in line 2


tell application "Address Book"
	set P to people of group "myGroup"
	repeat with onePerson in P
		repeat with onePhone in phones of onePerson
			set L to label of onePhone
			if not (L ends with " X") then
				set V to value of onePhone
				if V starts with "+55" then
					set newValue to "0041" & text 4 thru -1 of V
					tell contents of onePerson to make new phone at end of phones with properties {label:L & " X", value:newValue}
				else if V starts with "+" then
					set newValue to "0041" & text 2 thru -1 of V
					tell contents of onePerson to make new phone at end of phones with properties {label:L & " X", value:newValue}
				end if
			end if
		end repeat
	end repeat
end tell

I’m not sure if you are using Skype on the Mac or iPhone, but if on the Mac and AB

then I would just change all the numbers in AB to the 041 and 0041 numbers,
And then use a skype address book plugin to deal with the way skype wants to dial them, rather than clutter up the AB with duplicate numbers.

Slightly revised plugin from the post below

http://www.macosxhints.com/article.php?story=20051221143702865


property your_Brazil_code : "+55" 
property your_code : "+1" --set to you own country code.
using terms from application "Address Book"
	on action property
		return "phone"
	end action property
	
	on action title for p with e
		
		if label of e is equal to "Skype" then
			return "Call Skype Name"
		else
			return "Dial using Skype"
		end if
	end action title
	
	on should enable action for p with e
		return true
	end should enable action
	
	on perform action for p with e
		set _alphabet to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "Â
		q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "Â
		K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "+"}
		set phone_num to the value of e
		-- Clean up the number from any extraneous characters
		-- like blanks, '(', ')', '-' .
		set phone_num to replace_chars(phone_num, "-", "")
		set phone_num to replace_chars(phone_num, "(", "")
		set phone_num to replace_chars(phone_num, ")", "")
		set phone_num to replace_chars(phone_num, " ", "")
		set phoneC to character 1 of phone_num as string
		log phone_num
		if phoneC is not in _alphabet then
			set g to (characters 1 thru 3 of phone_num) as string
			if (characters 1 thru 3 of phone_num as string) is "041" then
				set phone_num to your_Brazil_code & characters 4 thru -1 of phone_num --addplus(phone_num, _alphabet)
			else if (characters 1 thru 3 of phone_num as string) is "004" then
				set phone_num to your_code & characters 5 thru -1 of phone_num --addplus(phone_num, _alphabet)
			end if
		else
			
		end if
		
		skype_out(phone_num)
		
	end perform action
	
	on addplus(phone_num, _alphabet)
		--if phone_num does not start with "+" then
		set phone_num to "+44" & characters 2 thru -1 of phone_num
		
		--end if
	end addplus
	
end using terms from

on skype_out(phone_num)
	tell application "Skype"
		activate
		set call_string to "callto:" & phone_num
		get URL call_string
	end tell
end skype_out

-- Text replace function
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