# Getting a file to read/write in Applescript



## sts24 (Dec 2, 2002)

I need to know what is wrong with this script:

on will quit theObject
	set savepath to do shell script "whoami"
	set pathName to "Users:" & savepath & ":Library:Application Support:Note2Self:mainnote.n2s"
	set theData to contents of text view "text" of scroll view "text" of window "main" as string
	set theFile to open for access pathName with write permission
	write theData to theFile as string
	close access theFile
	return true
end will quit

I can get the thing to write a file, but the contents of the text view "text" doesn't write to the file. I was also wondering how to read this same file on launch. I can't get that to read. Is there an applescript command to overwrite a file so that on each quit, the file updates what is in the text view. Then on the next launch, it will open that same file in the text view "text".

Any help, thanks.


----------



## Vocal Velocity (Dec 4, 2002)

here is a handler that does the trick for me.

on write_to_file(this_data, target_file_path)
	try
		set the target_file_path to the target_file_path as text
		set the open_target_file to open for access the file target_file_path with write permission
		set eof of the open_target_file to 0 --> remove this line if you want written data appended to existing data
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access the open_target_file
		end try
		return false
	end try
end write_to_file


----------

