Title: Re: BUG: Erage X doesn't format phone numbers
Interesting idea, so I did it; script below, or you can download from AppleScript Central.

On or near 1/7/02 6:22 PM, Rick Mann at [EMAIL PROTECTED] observed:

> on 1/7/02 6:09 PM, Paul Berkowitz at [EMAIL PROTECTED] wrote:
>
>> You have to paste it in totally unformatted - just numbers - no spaces,
>> parentheses, dashes or anything, and then it will format it for you. That's
>> so you can override the auto-formatting for foreign phone numbers, Entourage
>> 2001 used to change foreign phone numbers to your local format, or mess it
>> up, if you had the auto-format preference. The new way is much better, but
>> pasting in pre-formatted (wrong format) won't work now.
>
> So I would suggest that numbers that have 10 (or 7) digits should get
> formatted according to my preference. I can't imagine ever pasting in a
> completely unformatted number (since I usually copy from email messages, and
> people always format their numbers to some degree). Or put a little "force
> format" button next to the numbers list.
>
> Better still, I'd like to be able to tell ERage to just go through all the
> numbers once and format them.
>
tell application "Microsoft Entourage"
    activate
    try
        set theList to the selection
    on error theErr number errNum
        if errNum = -1728 then
            -- Try formatting the text on the clipboard
            set t to the clipboard
            set t to my reformat(t)
            set the clipboard to t
            try
                set the selection to t
            on error
                display dialog "Clipboard now contains: " & t
            end try
            return
        else
            display dialog "Error number:" & errNum & " " & theErr
            return
        end if
    end try
    repeat with theC in theList
        if class of theC is not contact then
            if class of theC is string then
                -- Try formatting the text on the clipboard
                set t to the clipboard
                set t to my reformat(t)
                set the clipboard to t
                try
                    set the selection to t
                on error
                    display dialog "Clipboard now contains: " & t
                end try
                return
            end if
            display dialog "No contacts are selected."
            return
        end if
        --    set theC to contact "Jenna Burrell"
        tell theC
            set home phone number to my reformat(home phone number)
            set business phone number to my reformat(business phone number)
        end tell
    end repeat
end tell

on onlyDigits(s)
    -- Strip all but digits from a string
    set theDigits to "1234567890"
    set newS to ""
    repeat with i from 1 to length of s
        set c to character i of s
        if c is in theDigits then set newS to newS & c
    end repeat
    return newS
end onlyDigits

on formatNum(n)
    -- Insert USA telephone formatting
    --Choose one of two formats, comment out one unused
    set mask to "(AAA) PPP-NNNN"
    -- set mask to "AAA-PPP-NNNN"
    if length of n is 10 then
        set aaa to text 1 thru 3 of n
        set ppp to text 4 thru 6 of n
        set nnnn to text 7 thru 10 of n
        set mask to my searchReplace(mask, "AAA", aaa)
        set mask to my searchReplace(mask, "PPP", ppp)
        set mask to my searchReplace(mask, "NNNN", nnnn)
        set n to mask
    else if length of n is 7 then
        set n to text 1 thru 3 of n & "-" & text 4 thru 7 of n
    end if
    return n
end formatNum

on reformat(pNum)
    set pNum to my onlyDigits(pNum)
    set pNum to my formatNum(pNum)
    return pNum
end reformat

-- routine to do a search and replace on text
on searchReplace(mainString, searchString, replaceString) -- Parameters: search, replace, the String
    set olddelis to AppleScript's text item delimiters
    
    set AppleScript's text item delimiters to (searchString)
    set theList to (every text item of mainString)
    
    set AppleScript's text item delimiters to (replaceString)
    set theString to theList as string
    
    set AppleScript's text item delimiters to olddelis
    return theString
end searchReplace

-----Readme for above
Reformat Phone Numbers -- By Allen Watson for Microsoft Entourage
Wednesday, January 9, 2002
This script will take a 7 or 10 digit telephone number in any format and reformat it to one of two "USA" formats:
(nnn) nnn-nnnn
nnn-nnn-nnnn

When you input telephone numbers into a contact record, Microsoft Entourage will not automatically format the number even though you have selected that option in Preferences, unless you enter exactly seven or ten digits with no other characters. This is deliberate, to allow the entry of numbers in formats other than your default format, such as numbers for other countries. It is annoying, however, because telephone numbers that are copied from mail messages or even simply mistyped the first time they are entered cannot be reformatted, except manually. This script meets that need.

The script takes a text string in any format and strips out all characters and spaces except digits. Then, if the number has 7 digits it is formatted as nnn-nnnn. If it has ten, it is formatted into one of the above two formats. By default, the format used is (nnn) nnn-nnnn. To use the other format you must edit the script, find the "formatNum" routine, and comment out the current "set mask" line, and uncomment the alternate format "set mask" line.

This script could, by a person who knows a little scripting, be easily adapted to format numbers in other non-USA formats.

Normal uses:

1. Open the Address Book. Select one or more contacts in the list. Run the script. Both Home and Business phone numbers will be reformatted if they exist. (FAX and other numbers are not currently handled. It would be trivial to add these at the point where you see the lines "set home phone number..." and "set business phone number...".)

2. Select a telephone number in any text field within Entourage (including messages), and copy the number to the clipboard before running the script. Run the script, and (in most cases) the selected text will be replaced with the reformatted telephone number. Note, if you select text that is not a telephone number, this script will delete it!

3. If you are currently editing a contact and have selected the text in an actual telephone number field, for some reason Entourage does not recognize this as a text selection; the script cannot directly replace it. Instead, it will place the text onto the clipboard and notify you that the reformatted number is on the clipboard; you can then manually paste it. This same action is taken in other cases where the selected text cannot be replaced, for example, incoming messages that are not set to be edited.

The actions of the script would be much more straightforward if it were possible to access the Copy, Cut and Paste commands from the Edit menu; however, there is no scripting support for these commands in Entourage at this time.

I have given the script the suffix, "\scm3". This assigns the keyboard shortcut "Control-Shift-Command-3" to the script. Shift-3 is "#". Using the number sign for this script seems a good way to remember the shortcut. Be careful to press all the keys, because "Cmd-Shift-3" is the OS "Screen Snapshot" command, and "Shift-3" alone will switch to the Calendar View in Entourage.
--
Microsoft MVP for Entourage/OE/Word
Allen Watson <[EMAIL PROTECTED]> XNS name: =Allen Watson
Applescripts for Outlook Express and Entourage: <http://homepage.mac.com/allenwatson/>

Reply via email to