Add to end of document?

I have a script that opens up a simple-text document in TextEdit and adds text to the beginning. Now, I am trying to find a way to add text to the end. I have tried all sorts of stuff, to no avail. I’m not sure if there is some way I can get the current paragraph count and then add 1 and use a set paragraph #.

I am pretty sure there’s an easy answer to this that I just haven’t stumbled onto yet. Thanks for your time and any thoughts.

Steven

Hi

Does this work:

tell application "TextEdit"
	tell document 1
		set t to return & "hello steve"
		make new paragraph at end with data t
	end tell
end tell

Perfect! Thanks so much!

And you inadvertently answered another nagging question I had about returns.

Thanks for your help!

If you do not need to open the text file in TextEdit then this might
suit your needs as well.

Cheers,

Craig


set filePath to "path:to:textfile.txt"
set textToAdd to "Add to end of text file"
-- the double >> appends to the end of a file while a single > overwrites the file
do shell script "echo " & quoted form of POSIX path of textToAdd & " >> " & quoted form of filePath

Hi, Craig.

I think you meant ‘POSIX path of filePath’. :slight_smile:

set filePath to "path:to:textfile.txt"
set textToAdd to "Add to end of text file"
-- the double >> appends to the end of a file while a single > overwrites the file
do shell script "echo " & quoted form of textToAdd & " >> " & quoted form of POSIX path of filePath

Just for completeness, here’s the File Read/Write method:

set filePath to "path:to:textfile.txt"
set textToAdd to "Add to end of text file"
-- No need to open explicitly with write permission if the file exists and it's not open already.
write textToAdd as string to file filePath starting at eof

Thanks Nigel.