clear all text and paste new text

I have looked everywhere on the forums:-

i am trying to write a script which opens a text file, clears the contents of the text file and then pastes a new piece of text. This script merely appends the text file.

tell application "TextEdit"
				launch 
				set f to open for access (p2d as text) & fName with write permission
				try
					set t to read f
					set t to ""
				on error
					set t to "why is that?"
				end try
				
				write theText & (ASCII character 59) as string to f			
				
				close access f
			end tell

Hi skipzone

try this, you said “pastes a new piece of text.”, so im presuming that this is the contents of the clipboard?

set ClipBoardContents to the clipboard --get contents of clipboard
set theFile to ((path to desktop as text) & "Untitled 4.txt") --path to your text file
open for access file theFile with write permission --open text file
set eof file theFile to 0 --clear contents of text file
write ClipBoardContents to file theFile starting at eof --write contents of clip board
close access file theFile --close text file