Add text to end of file

I found this in another a posting by StefanK (http://macscripter.net/viewtopic.php?id=23789) and it is almost what I am looking for:

set s to "new line "
repeat with i from 1 to 10
	set ff to open for access file ((path to desktop as Unicode text) & "test_insert.txt") with write permission
	try
		set t to read ff
	on error
		set t to ""
	end try
	write (s & (i as text) & return & t) to ff starting at 0
	close access ff
end repeat

Unfortunately I only need to to add a line of text that is received from a User via a text box to the bottom of a file. Can someone modify this or provide other code to help me accomplish this task?

Thanks in advance,
Eric

Try this handler:

on run
	set writeFile to (choose file with prompt "Choose the file to write to")
	set writeData to text returned of (display dialog "Data to write" default answer "Hello World")
	
	set success to appendDataToFile(writeData, writeFile)
	if success is false then
		display dialog "Writing failed"
	else
		display dialog "Writing succeeded"
	end if
end run

on appendDataToFile(myData, aFile)
	set OA to (open for access aFile with write permission)
	try
		write (return & myData) as text to OA starting at eof
		close access OA
		return true
	on error
		try
			close access OA
		end try
		return false
	end try
end appendDataToFile

Hope it helps,
ief2

ief2,

Your scriptlet code is accepting the user input but I am not sure why it is formatting it as shown below:

Data as shown in vi editor:

Test the Sort Function,telnet,5.5.5.5,goober
GGSG-AS-UNIX5,ssh,172.18.149.232,emidd
Edge2,ssh,4.4.4.4,eric
Core2,telnet,2.2.2.2,emidd
Edge1,telnet,3.3.3.3,eric
Core1,ssh,1.1.1.1,eric
^Mtestdevice1,telnet.5.5.5.5,goober^Mtestdevice2,telnet,7.7.7.7,dork
~
~

Data as shown by “cat ”:

emidd@~/Documents$cat AppleScriptTextFile.csv
Test the Sort Function,telnet,5.5.5.5,goober
GGSG-AS-UNIX5,ssh,172.18.149.232,emidd
Edge2,ssh,4.4.4.4,eric
Core2,telnet,2.2.2.2,emidd
Edge1,telnet,3.3.3.3,eric
Core1,ssh,1.1.1.1,eric
testdevice2,telnet,7.7.7.7,dorkemidd@~/Documents$

***Notice testdevice1 doesn’t show up in this output but it does exist in the file. ***

There is a ^M being inserted with each run of the appendDataToFile function. Not sure if there is anything we can do about that or will I have to just modify the way I retrieve the data. Could it be the type of file I am using?

As always, thanks very much!!! I think I am going to have to start paying you for your help!!!

v/r
Eric

Strange. Maybe it’s something with the text encoding? UTF-8? UTF-16? Western? Chinese?

I suppose the "^M"s are escape codes for the returns. You could try using linefeeds instead to see if they made a (useful) difference.

Hello,

use a line feed (ascii 10 ) instead of a return (ascii 13) as line separator.

Thanks everyone, your input enabled me to fix the issue. I changed the write statement to the following and that took care of it.

write (myData & (ASCII character 10)) as text to OA starting at eof

Thanks again!
Eric

Since Leopard there is a keyword linefeed for ASCII character 10
and if myData is a string the additional coercion to text is useless.
The default class of the result of a concatenation (&) is always the class of the leftmost operand.

write (myData & linefeed) to OA starting at eof