Read contents of file, add to, write back to same file

This is driving me nuts.

Quite simply, I want to add text to an existing file:

set thePath to (path to desktop as Unicode text) & "somefile.txt"
set fRef to open for access file thePath with write permission
set FileContentsRaw to (read file thePath) as text
write "bob" & return to fRef
close access fRef

I’m forever getting:

Yes, I could do this easily in bash but I’d like to do it properly…

Ideas?

Hi,

read file . calls open for access implicitly.
If you want to do read/write operations in the same file you have to read using the file descriptor (fRef)

Change that to:

set FileContentsRaw to read fRef as text

Use the reference, and you don’t want those parentheses.

But you can also avoid the whole business of reading in the first place with:

write "bob" & return to file thePath starting at eof