multiple pastes in TextEdit

Hi,
I’m trying to take a bunch of text files and open them one by one, copy the contents of the file and paste them into one master document. The problem that I’m having is that every time I place the text into my master document it overwrites the existing text. I know my TextEdit syntax is not correct but I don’t know how resolve this issue. Please help.
Thanks

tell application "Finder" to set the source_folder to (folder "ad_records")
set item_list to every file of source_folder
set the DocName to "master ad doc.txt"
set mainFile to choose file with prompt "choose master file"

repeat with i from 1 to number of items in item_list
	set this_item to item i of item_list
	tell application "TextEdit"
		activate
		open this_item
		set TheContents to the text of the front document
		close window 1
		--set copiedtext to text of window 1
		open mainFile
		set paragraph 1 of the front document to return & TheContents
		save document 1 in file (ThePath & DocName)
		close window 1
	end tell
end repeat

Hi blend3,

In this line:

set paragraph 1 of the front document to return & TheContents

you’re overwriting the first paragraph. You can do this:

make new text at after last paragraph of front document with data (return & TheContents)

Also, you don’t need to keep opening, saving, and closing the main document. Just leave it open and save at the end. Another thing is do you need to preserve the format?

gl,

Hi Kel,
Thanks for your help but the mainFile text is still being overwritten by the last text file opened. The format that I’m using is tab delimited so that I can import the data into FileMaker. One other question if I want to close the file at the end of the repeat how do I reference that window or document with the close statement?
Thanks,
Nik

set ThePath to path to desktop as string
tell application "Finder" to set the source_folder to (folder "ad_records")
set item_list to every file of source_folder
set the DocName to "master ad doc.txt"
set mainFile to choose file with prompt "choose master file"

repeat with i from 1 to number of items in item_list
	set this_item to item i of item_list
	tell application "TextEdit"
		activate
		open this_item
		set TheContents to the text of the front document
		close window 1
		--set copiedtext to text of window 1
		open mainFile
		make new text at after last paragraph of front document with data (return & TheContents)
		save document 1 in file (ThePath & DocName)
		close window 1
	end tell
end repeat

Hi Kel,
Please ignore last message, you’re correct, I just needed a little adjustment and it works fine now.
Many Thanks,
Nik :o

set ThePath to path to desktop as string
tell application "Finder" to set the source_folder to (folder "ad_records")
set item_list to every file of source_folder
set the DocName to "master ad doc.txt"
set mainFile to choose file with prompt "choose master file"

repeat with i from 1 to number of items in item_list
	set this_item to item i of item_list
	tell application "TextEdit"
		activate
		open this_item
		set TheContents to the text of the front document
		close window 1
		--set copiedtext to text of window 1
		open mainFile
		make new text at after last paragraph of front document with data (TheContents & return)
	end tell
end repeat

tell application "TextEdit"
	save document 1 in file (ThePath & DocName)
	close window 1
end tell

Hi blend3,

Here’s an example:


set main_file to choose file
set files_folder to choose folder
tell application "Finder" to set file_list to every file of files_folder
tell application "TextEdit"
	launch
	activate
	-- open main file first
	open main_file
	-- open each file next
	repeat with this_file in file_list
		open this_file
		set t to text of front document
		close front document
		-- now the main document is the front document
		-- write to it
		make new text at after last paragraph of front document with data (return & t)
	end repeat
	-- now save main document ...
end tell

If you have a lot of documents in the folder, you might find that handling them in bulk is faster. Basically, open all the documents at once, assemble the text, then set the text of the master document to the result.

I’m not clear whether or not ‘mainFile’ and file “master ad doc.txt” of the desktop are supposed to be the same or different files. If they’re the same, you could simply save the document back to ‘mainfile’. However, I’ve gone with the “different file” possibility here.

set ThePath to (path to desktop as Unicode text)
set the DocName to "master ad doc.txt"

tell application "Finder"
	set the source_folder to folder "ad_records"
	try
		set docFiles to (every file of source_folder) as alias list
	on error
		set docFiles to {(first file of source_folder) as alias}
	end try
end tell
set docCount to (count docFiles)

tell application "TextEdit"
	activate
	set mainFile to (choose file with prompt "choose master file")
	open mainFile
	set masterDocName to name of front document
	set masterText to text of front document
	
	open docFiles
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return as Unicode text
	set (document masterDocName)'s text to masterText & text of documents 1 thru docCount
	set AppleScript's text item delimiters to astid
	
	save document masterDocName in file (ThePath & DocName)
	close (windows 1 thru (docCount + 1))
end tell