In need of a script for work

Hello all,

This is my first time posting, I have read the rules but please forgive me if I break etiquette.

I work as a Technology coordinator for an adult learning Center. I have a lot of emails to send out. I need a script that will take email addresses from a Word document and put them into an Apple Mail and into contact Group I only use a Mac at work until I get one for my birthday - so I have never scripted before but if anyone would be so kind as to post the code for this, my bosses and I would appreciate it very much.

Thank you very much,

apple2c1980

This is a relatively simple task. However, you need to either provide an example of what the word document looks like or describe it thoroughly so that people can get you started on a solution.
:slight_smile:

Hi,

Thanks for the reply.

The document is extremely simple, it looks like this:

Person’s name [space] xxx@xxx.com (or whatever the e-mail address ends in. They are all different. Where xxx@xxx is the e-mail address)

If you need a copy of the file, please let me know.

Hey There,

So. Only one address per Word document eh? :cool:

Or did you mean there would be one set of per line, and there might be 1 or many lines?

Please be more specific.

Also:

Why are you fooling with a group of recipients in Word instead of putting them into an easily accessible group in Contacts?

It seems like you’re making this job more difficult than it needs to be, but perhaps your process requires this.

If so I think you should post your task step by step, so we can see what it really is and provide the most economical solution.

Hi, thanks for the reply.

I’m sorry the process seems very convoluted, but that’s because the networking and technology resources we have at the Learning Program are SNAFU and FUBAR. I could write a book about how NOT to implement technology for a learning program after working here. I seriously think I may do that at some point!

The file is like this:

one person and e-mail per line and there are 15-20 lines or so.

I need the person’s name and e-mail address to go into Apple Mail, and, if possible, I need them to go into my iCloud Contacts as well.

I spent a lot of time years ago discovering the madness of Microsoft Word scripting. I pulled up one of my old scripts, and have modified it to get you started. Perhaps someone with more recent Mail and/or Contacts scripting can help finish it off:

set a to choose file  --Select your file with the names and addresses 
set paragraphTextData to {}
tell application "Microsoft Word"
	set newDoc to open file name (a as Unicode text)
	set paraCount to count every paragraph of newDoc's text object --count all the paragraphs in the document
	repeat with x from 1 to paraCount
		set lineText to content of text object of paragraph x of newDoc's text object
		if lineText contains "@" then set end of paragraphTextData to lineText --Only keep the paragraphs with @
	end repeat
end tell

--> {"Bob Jones bob@yahoo.com
", "Billy Joe bill@gmail.com
", "Donna Lee donna@cs.com
", "Joe Schmoe joe@joe.com
", "Wanda Lee Williams wanda@microsoft.net
"}

At least now you have a list with each person’s information. It will need to be parsed, and then the data sent to Mail and/or Contacts.

If I get some more time later, I can work further on it.

Good Luck,

Here is a little more. You now have records with each person’s name and email:

set a to choose file
set paragraphTextData to {}
tell application "Microsoft Word"
	set newDoc to open file name (a as Unicode text)
	set paraCount to count every paragraph of newDoc's text object --Works well
	repeat with x from 1 to paraCount
		set lineText to content of text object of paragraph x of newDoc's text object
		if lineText contains "@" then set end of paragraphTextData to lineText
	end repeat
	
end tell
--paraCount & return & para3

set personDataArray to {}

repeat with eachPerson in paragraphTextData
	set end of personDataArray to my ParsePerson(eachPerson as text)
end repeat

personDataArray

-----------------------------------
to ParsePerson(txt)
	set txt to txt's characters 1 thru -2 as text --This gets rid of the terminal return in the text line
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to space
	set personRecord to {pName:(txt's text items 1 thru -2 as text), pEmail:(txt's text item -1)}
	set AppleScript's text item delimiters to astid
	return personRecord
end ParsePerson

-->{{pName:"Bob Jones", pEmail:"bob@yahoo.com"}, {pName:"Billy Joe", pEmail:"bill@gmail.com"}, {pName:"Donna Lee", pEmail:"donna@cs.com"}, {pName:"Joe Schmoe", pEmail:"joe@joe.com"}, {pName:"Wanda Lee Williams", pEmail:"wanda@microsoft.net"}}

Thanks all for the replies.

There is only one problem.

My Mac at work does not have Microsoft Word. I have to put Apache Open Office on it. Do I just replace the Microsoft Word part with OpenOffice?

Also, what does the little box looking character mean in the script?

Thanks!

I do not believe that Open Office is scriptable, but you can try. Although it would need one more step in your workflow, all you need to do is save the Word (or Open Office) document as plain text, then run this script:

-- Reading from .txt file:
(*
Bob Jones bob@yahoo.com
Billy Joe bill@gmail.com
Donna Lee donna@cs.com
Joe Schmoe joe@joe.com
Frank
John
Wanda Lee Williams wanda@microsoft.net

*)

set a to choose file
set paragraphTextData to {}

set readFile to open for access a
set rawTextData to every paragraph of (read readFile)
close access readFile

repeat with eachLine in rawTextData
	if (eachLine as text) contains "@" then set end of paragraphTextData to (eachLine as text)
end repeat

set personDataArray to {}

repeat with eachPerson in paragraphTextData
	set end of personDataArray to my ParsePerson(eachPerson as text)
end repeat

personDataArray

-----------------------------------
to ParsePerson(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to space
	set personRecord to {pName:(txt's text items 1 thru -2 as text), pEmail:(txt's text item -1)}
	set AppleScript's text item delimiters to astid
	return personRecord
end ParsePerson

This is pretty much the same script as before, but designed to read a .txt file instead of Word document. Hopefully someone will chime in on how to add these persons to your Contacts.

The small font makes it look like a box, but it is really just two curly brackets: { }. They define an empty list.

I had some time to finish it up. This script SHOULD open a text file and work through each line, creating a new person with a Home email address. It should then add them all to your Contacts. The script currently adds them to a group entitled Word Testing, so you need to replace that with the name of the group that you wish to use.

-- Reading from .txt file:
(*
Bob Jones bob@yahoo.com
Billy Joe bill@gmail.com
Donna Lee donna@cs.com
Joe Schmoe joe@joe.com
Frank
John
Wanda Lee Williams wanda@microsoft.net

*)

set a to choose file
set paragraphTextData to {}

set readFile to open for access a
set rawTextData to every paragraph of (read readFile)
close access readFile

repeat with eachLine in rawTextData
	if (eachLine as text) contains "@" then set end of paragraphTextData to (eachLine as text)
end repeat

set personDataArray to {}

repeat with eachPerson in paragraphTextData
	set end of personDataArray to my ParsePerson(eachPerson as text)
end repeat

tell application "Contacts"
	repeat with eachOne in personDataArray
		set detailedName to my ParseName(eachOne's pName)
		set newContact to make new person with properties {first name:(detailedName's firstName), last name:(detailedName's lastName)}
		tell newContact
			make new email at end of emails of newContact with properties {label:"Home", value:(eachOne's pEmail)}
		end tell
		add newContact to group "Word Testing" --<<<<---- USE NAME OF GROUP IN CONTACTS THAT YOU WANT TO STORE THESE NAMES
		save
	end repeat
end tell

-----------------------------------
to ParsePerson(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to space
	set personRecord to {pName:(txt's text items 1 thru -2 as text), pEmail:(txt's text item -1)}
	set AppleScript's text item delimiters to astid
	return personRecord
end ParsePerson
-----------------------------------
on ParseName(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to space
	set nCount to count txt's every word
	set parsedName to {firstName:(words 1 thru (nCount - 1) of txt as text), lastName:(word nCount of txt)}
	set AppleScript's text item delimiters to astid
	return parsedName
end ParseName

It could certainly be cleaned up and more streamlined, but oh well.
Good luck,

I ran the script and it let me choose the appropriate file, but then it said Running… on the bottom and returned: Missing value

It doesn’t show me where though.

The only thing I did after copying the script into the editor is to create a new group in Contacts and rename the group in the script where the comments told me to do so.

Hi all,

Please disregard my last post as the script worked despite the warning/error message. Tip I discovered: Don’t get frustrated by the error message and run the script more than once - I ran it 3 times and it put the contacts in the contact group 3 times.

I fixed that with some quick deletion!

Anyway, thank you all for your amazing help! :smiley: :smiley: :smiley:

My guess is that the reason you are getting the error message is that there is an extra return at the end of the file. This is common with lists in word.

You got some great help here!
:slight_smile:

Glad to hear that it worked for you. If you continue to have problems in the future, you can send me an email or a PM (Personal Message) with more details, and we can figure it out. Just click either of those links under my name.

Hey apple2c1980,

Since you don’t have MS Word on the machine at work you can use the textutil command-line-utility to transform the Word file into text.

The following script operates on a single selected file in the Finder.

It reads a .docx file and converts it to text.

It then strips any junk characters out and produces an AppleScript list.

REQUIRES that the Satimage.osax AppleScript Extension be installed for regular expression support.


tell application "Finder" to set fSel to selection as alias list
if fSel ≠ {} then
	set _file to quoted form of (POSIX path of first item of fSel)
	set shCMD to "textutil " & _file & " -stdout -format docx -convert txt -encoding UTF-8"
	set emailList to do shell script shCMD
	set emailList to change "^\\W+" into "" in emailList with regexp without case sensitive
	set emailList to change "\\t+" into " " in emailList with regexp without case sensitive
	set emailList to change "(?m)\\A\\s+|\\s+\\Z" into "" in emailList with regexp without case sensitive
	set emailList to paragraphs of emailList
end if
  • The script doesn’t anticipate any possible kind of crud or irregularity that might be in the file, because I haven’t seen the files. Nevertheless oddities are easily accommodated.

Change docx to doc in the script if you need to support the old file format.

Easy enough to make the script pick the right verbiage for the two different Word file-types.

Easy enough to make the script operate on multiple files at once.

Craig has already provided a script for adding items to Contacts, so I won’t bother.

As for Mail. If you want to mess with that I’d need a bit more info.

Note that you should be using a utility to run AppleScripts via keyboard shortcuts like FastScripts or Keyboard Maestro for better productivity.

FastScripts demo mode will allow 10 keyboard shortcuts (and I believe an unlimited number of scripts), but at $9.95 US it gives a lot of bang for buck.

I’ve been using both utilities since 1993 & 1994 respectively.

If you don’t have BBEdit you should download and install its freeware sibling TextWrangler. Each app is highly scriptable and has its own script menu. They make working with plain text much more tolerable, and you could use one of them as a substitute for FastScripts or KM for this job.

Hey Craig

Remember ” you don’t have to open a file for access in order to read it ” only to write to it.

if the file already exists you don’t need it for writing either.

I am an old man and set in my ways.

I do, however, appreciate all such feedback.

Pages will also open any Word document, and export to plain text. I am not sure if that function is scriptable, however.

Just stay off my lawn.