SenseTalk Question

Hi,

Is there a way to write into the middle of a text file?

e.g.

Obj.txt

Apple
Train
Baby
Moon

To ->

Apple
Train
Baby Age 02
Moon

I was writing something like this


open file “Obj.txt”
seek in file “Obj.txt” to the beginning

repeat forever
read from “Obj.txt” until return

if it is empty then exit repeat
if it contains “Baby”
???
end if
end repeat
close file “Obj.txt”

What can I put in there that will replace the line with “Baby”?

Thanks,
Jason

There are a few ways to do this, but the simplest is to just treat the file as a container. If you use “open file” and “read from file” etc. you’ll be able to overwrite things in the file, but not insert text.

Here’s one approach that works, stepping through the lines of a file “by reference” which allows you to modify them directly:

set file "/tmp/testData" to {{
Apple
Train
Baby
Moon
}}

repeat with each line of file "/tmp/testData" by reference
	if it starts with "Baby" then put " Age 02" after it
end repeat

put file "/tmp/testData"