Strip Number Formatting from Address Book

I’ve searched around for this but don’t seem to be able to find the specific thing I’m looking for (or even close for that matter). I’m aware of the Address Book’s feature to automatically format phone numbers when entered, but unfortunately this only appears to work when numbers are ENTERED. Cards added via synchronisation or import don’t seem to be updated.

To make a ‘clean start’, I’d like to write a simple script to remove all formatting (essentially strip spaces and brackets () from all entries). Does anyone know of an easy way to do this?

I’m a complete novice at Applescript at this stage, so apologies - I’m still learning. :slight_smile:

Thanks,

Tom

Hi Tom,

this reformats all phone numbers of all contacts.
Everything except the ciphers (0-9) and the plus sign (+) will be ignored
I commented out the write value line, so you have a preview how it works without changing real data
To apply it, comment out the display dialog line and “decomment” the set value line

tell application "Address Book"
	repeat with aPerson in (get people)
		repeat with aPhone in phones of aPerson
			tell contents of aPhone
				display dialog my strip_numbers(get value)
				-- set value to my strip_numbers(get value)
			end tell
		end repeat
	end repeat
end tell

on strip_numbers(t)
	set r to {}
	repeat with i in (get characters of t)
		if i is in "+0123456789" then set end of r to contents of i
	end repeat
	return r as Unicode text
end strip_numbers

You’re depending on AppleScript’s text item delimiters without setting them.

Yes, I assume, that the responsible user has set the delimiters to the default {“”}

Maybe that responsible user also happens to be using a script from an irresponsible user that has changed the delimiters without settings them back. I don’t believe it is good practice to rely on an unknown environment when you can avoid potential problems yourself.

Thanks guys - will give this a try tonight.

Where would an Applescript-newbie user set their text delimiters?? :lol:

In this script at the beginning

set text item delimiters to {""}
tell application "Address Book"
	repeat with aPerson in (get people)
.

Note: within a tell block you must use Applescript’s text item delimiters

Addendum to note: Several other programs use text item delimiters, so if you mean AppleScript’s TID, in my view you should always say so. Because parsing text is often in a script, I’ve created three hotkey phrases for myself:

set tid to applescript’s text item delimiters
set applescript’s text item delimiters to
set applescript’s text item delimiters to tid

Here’s a couple of related articles:
Tutorial for Using AppleScript’s Text Item Delimiters
Text Item Delimiters are De-limitless

Also:
AppleScript Language Guide: AppleScript Properties