Convert MS Word documents to TextEdit rtf

Hi all.

I have several hundred MS word files that I need to make TextEdit rtf formated files. This is my first applescript project and after a few days I’ve realized that I need some help.

Here’s my attempt so far. It uses folder actions, but it doesn’t need to, that was just the easiest way I could think of.

on adding folder items to this_folder after receiving these_items
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
tell application “TextEdit”
set mydoc to open this_item

		-- a this point I need to save the documents to a folder as a rtf and then close the document

	end tell
end repeat

end adding folder items to

Looking over some of the posts in this forum I now think that I’ve gone about this the wrong way, still trying to get my head around how applescript works.

Thanks for your help

jess

Folder actions are hard to test in development. I prefer selecting a folder full of files. Do you want to save all the contained information when opening MS docs in Text Edit, or just the formatted text? Do you have MS Word on your comp?
SC

I was going to send the saved documents through a perl script which requires a TextEdit rtf documents. That said, the whole point of this project is to take 1000 MS Word documents and to turn it into one document that retains the formating. I don’t have MS Word, but I do have a trial version (test drive) that I could load, would that be the better way to go?
thanks for your help.

jess

Yes, your going to need Word or some program that will read and display the formatted text. Text Edit displays the code and the text.
This is the basic format for your script:

That is assuming all files are in one folder and in order for processing. WriteToFile() is a handler, post back if you need it.
SC

I’m getting an AppleSript error “Can’t make every paragraph of window 1 into type reference.” I’ll try some other methods to get the content of the window; I think that’s what you are aiming for. Also, I don’t have the WriteToFile() handler, so if you could post please.
thanks for all your help, jesse

Here is a working prototype; If all your docs are in a folder, and named in sequential order, this will create a single text edit document of them all. It saves to your desktop. You can then open it and in the menu select “Format/Make Rich Text” to save as rtf. You could script that part, but I’ll leave that to you since its a one time operation. Make a test folder with 4 or so files to test before running the whole batch.


set theFolder to choose folder
tell application "Finder" to set theDocuments to files of theFolder

tell application "Microsoft Word" to launch

repeat with ThisFile in theDocuments
	tell application "Microsoft Word"
		
		open ThisFile
		delay 1--May need to adjust how long it takes to open file
		set this_data to text of window 1
		close window 1
	end tell
	
	set Filename to "TestFile" --Name the file 
	set target_file to (path to desktop) & Filename --You can eliminate "Filename" as a variable and type the filename as part of the path (if you wish) but you need a full path 
	set append_data to true --Set this to false if you want to overwrite, true if you want to add text to an existing file 
	
	try --This part writes the file 
		set the target_file to the target_file as text
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
	on error
		try
			close access file target_file
		end try
	end try
end repeat

SC

A few notes and then the script;

The text copies without font and size info. There are possibilities to capture this info and send it to TextEdit, but none that were easy. I may have missed something, but the final solution was to offer the user a choice of font and size for the entire document.

The order the files process in is going to be a big deal. They need to be in alphabetic order (use list view) for processing. If the files are not arranged this way they will need to be (for a thousand docs that would be a whole other script).

MS Word docs to TextEdit “rtf” document:


property FontList : {"American Typewriter", "American Typewriter Light", "American Typewriter Bold", "American Typewriter Condensed", "American Typewriter Condensed Light", "American Typewriter Condensed Bold", "Arial", "Arial Italic", "Arial Bold", "Arial Bold Italic", "Arial Black", "Arial Narrow", "Arial Narrow Italic", "Arial Narrow Bold", "Arial Narrow Bold Italic", "Arial Rounded MT Bold", "Baskerville", "Baskerville Italic", "Baskerville SemiBold", "Baskerville Bold", "Baskerville SemiBold Italic", "Baskerville Bold Italic", "Big Caslon Medium", "Brush Script MT Italic", "Comic Sans MS", "Comic Sans MS Bold", "Copperplate", "Copperplate Light", "Copperplate Bold", "Courier New", "Courier New Italic", "Courier New Bold", "Courier New Bold Italic", "Didot", "Didot Italic", "Didot Bold", "Futura Medium", "Futura Medium Italic", "Futura Condensed Medium", "Futura Condensed ExtraBold", "Geneva", "Georgia", "Georgia Italic", "Georgia Bold", "Georgia Bold Italic", "Gill Sans", "Gill Sans Italic", "Gill Sans Light", "Gill Sans Light Italic", "Gill Sans Bold", "Gill Sans Bold Italic", "Herculanum", "Lucida Grande", "Lucida Grande Bold", "Marker Felt Thin", "Marker Felt Wide", "Optima Regular", "Optima Italic", "Optima Bold", "Optima Bold Italic", "Optima ExtraBlack", "Papyrus", "Trebuchet MS", "Trebuchet MS Italic", "Trebuchet MS Bold", "Trebuchet MS Bold Italic", "Verdana", "Verdana Italic", "Verdana Bold", "Verdana Bold Italic", "Zapfino"}

property SizeList : {"12", "14", "16", "18", "24"}

set theFolder to choose folder with prompt "Select target folder"

set the_font to (choose from list FontList with prompt "Select the font" default items item 1 of FontList)
set the_size to (choose from list SizeList with prompt "Select the type size" default items item 2 of SizeList) as number

tell application "Finder" to set theDocuments to files of theFolder

tell application "Microsoft Word" to launch

repeat with ThisFile in theDocuments
	tell application "Microsoft Word"
		
		open ThisFile
		delay 1 --May need to adjust how long it takes to open file 
		set this_data to text of document 1
		close document 1
	end tell
	
	
	set Filename to "TestFile" --Name the file 
	set target_file to (path to desktop) & Filename --You can eliminate "Filename" as a variable and type the filename as part of the path (if you wish) but you need a full path 
	set append_data to true --Set this to false if you want to overwrite, true if you want to add text to an existing file 
	
	try --This part writes the file  to an appending text file
		set the target_file to the target_file as text
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
	on error
		try
			close access file target_file
		end try
	end try
end repeat

set FinalData to read alias target_file

tell application "TextEdit"
	try
		quit --Avoids errors if TextEdit is open
		delay 1
	end try
	activate
	save window 1 --Create and save blank "rtf" document
	set text of document 1 to FinalData--Copy the text doc to rtf doc
	set the font of text of document 1 to (the_font as string)
	set the size of text of document 1 to the_size
end tell

SC

Wow. I didn’t realize what I was getting myself into. Thanks for your help. I’m starting to understand some of the limitations of applescript. Hopefully it will help me to plan things in the future. Thanks for your help.