You can get the fourth paragraph (item separated by a linefeed, a carriage return, or carriage return + linefeed) like this:
tell application "TextEdit"
paragraph 4 of document 1
end tell
In TextEdit you can actually change the paragraphs:
tell application "TextEdit"
set paragraph 4 of document 1 to "zz" & linefeed
end tell
Normally trying to change a paragraph results in an error though:
set paragraph 1 of "aa" & linefeed & "bb" to "zz"
-- error "Can’t set paragraph 1 of \"aa
-- bb\" to \"zz\"."
If others search for how to append text to a plain text file, you can use the starting at eof
specifier with the write
command:
set fd to open for access "/tmp/a" with write permission
write "aa" & linefeed to fd as «class utf8» starting at eof
close access fd
This replaces the fourth line with zz
:
set input to read "/tmp/a" as «class utf8»
set text item delimiters to linefeed
set ti to text items of input
set item 4 of ti to "zz"
set fd to open for access "/tmp/a" with write permission
set eof fd to 0
write (ti as text) to fd as «class utf8»
close access fd