Improving a Contact-from-text script

P

Pitch

I've got a script here that can often take someone's contact info and
place it into a new Contact. (I'm placing the script below).

However, it doesn't always work, and I can't figure out why. If anyone
can improve upon the script, I'd be most grateful. (I use this
particular script ALL the time, for when I have someone's Name,
Address, Phone, Email).

As you'll see in the script, there are a few formats that it's supposed
to work in. However, here's a fresh example of where I can't get it to
work. I just received the following info:

John Smith School
2850 N. Wabash #1200
Chicago, IL 60616
310-225-5300

Yet when I run this script, it gives me either this missmatched Conct
info:
John Smith School
Chicago IL 60616 310, 225 5300

....or it gives me a parsing error on the Address line.

I've tried adding paragraph marks at the end of each line, and copying
and pasting this from a basic Text Edit doc, all sorts of methods for
the original contact text. Yet it doesn't work.

And yet that contact info is in a format that the script is supposed to
allow for.

Can anyone tweak this so that it would work? I'd even be willing to
give up all the different formats and just make sure that my contact
info is in one particular format, such as:

Name
Street
City, ST, zip
phone
email

Hre's the script:


(* Make new contact from selected text
Selection must contain, at a minimum, 2 lines:
FirstName LastName
email address

OR

FirstName LastName
City, State Zip

It can contain, optionally, one of these formats:

FirstName LastName [Required]
address line 1 (second line optional)
City, State Zip

FirstName LastName [Required]
address line 1 (second line optional)
City, State Zip
email address

FirstName LastName [Required]
address line 1
address line 2 (will be appended to line 1 in Entourage if included)
City, State Zip
email address

FirstName LastName [Required]
address line 1 (second line optional)
City, State Zip
telephone number (only digits, spaces, hyphens, and parentheses can be
included)

FirstName LastName [Required]
address line 1 (second line optional)
City, State Zip
email address
telephone number (only digits, spaces, hyphens, and parentheses can be
included)

If the contact name has more than 2 words, the last word will become
the last name, and the rest will be the first name. If both email
address and telephone are
included, they can appear in either order.
*)

tell application "Microsoft Entourage"
set {addr, eml, telephone, cntry} to {"", "", "", ""}
set x to the selection
if (class of x) is in {text, Unicode text, string} then
-- Parse into lines
try
set input to paragraphs of x
if item -1 of input is "" then set input to items 1 thru -2 of input
set cnt to count input
set aLine to item 1 of input
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set firstname to words 1 thru -2 of aLine
set firstname to firstname as text
set AppleScript's text item delimiters to oldDelims
set lastname to word -1 of aLine
repeat with i from 2 to cnt
set aLine to item i of input
if cnt = 2 or ((cnt > 3) and (cnt = (i + 2))) or (cnt = 3 and i =
3) then -- It is the last line or next to last
if aLine contains "@" then -- email address
set eml to aLine
else -- City, state, zip, or else telephone #
--Check if it is a phone number
set chs to every character of aLine
set isPhone to true
repeat with ch in chs
if ch is not in "0123456789-() " then
set isPhone to false
exit repeat
end if
end repeat
if isPhone then
set telephone to aLine
else
try
-- Check to see if ZIP is on following line by itself
set ZIPonLine2 to false
if i is not cnt then -- Don't check if we are at last line of
input
set nextLine to item (i + 1) of input
repeat with k from 1 to (length of nextLine)
if character k of nextLine is in "1234567890- " then
set ZIPonLine2 to true
else
set ZIPonLine2 to false
exit repeat
end if
end repeat
end if
on error theErr
display dialog theErr & return & " Exiting."
return
end try
if ZIPonLine2 then set aLine to aLine & " " & nextLine
set temp to every word of aLine
if (count of temp) = 1 then
set cntry to temp
else
set zp to item -1 of temp
set ste to item -2 of temp
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set cty to (get items 1 thru -3 of temp) as text
set AppleScript's text item delimiters to oldDelims
end if
end if
end if
else
set addr to addr & aLine & " "
end if
end repeat

on error theErr
display dialog "Parsing error; problem with input format." & return
& theErr
return
end try
else
display dialog "Please select the contact info and try again."
return
end if
set c to make new contact with properties {first name:firstname, last
name:lastname, address:eml, main phone number:telephone}
set hm to home address of c
set street address of hm to addr
set city of hm to cty
set state of hm to ste
set zip of hm to zp
set country of hm to cntry
set home address of c to hm
open c
end tell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top