saving TextEdit document

How can i save a TextEdit document with the name i want, were i want and with the text i want?(all without promt the user) Thanks!!

You don’t say enough. Did you create the file from the script or open an existing file and do something to it? Do you care if a copy already exists and should your new version replace it? Have you read TextEdit’s AppleScript Dictionary?

i create the file from the script, the file couldn’t exists and yes i’ve read the Dictionary
thanks!


--this_data can be any text coerced to this variable
set this_data to "i create the file from the script, the file couldn't exists and yes i've read the Dictionary, thanks!"

--Initiate a new file . "((path to desktop folder) as text)" can be replace with the path to any folder
set target_file to (((path to desktop folder) as text) & "MyCustomName" & ".txt")

--Write Data To New Text Document  
set openTargetFile to open for access file target_file with write permission
write this_data to openTargetFile
close access openTargetFile

SC

If the content of the file is just text inserted by your script, then TextEdit is irrelevant. The usual drill goes like this:


-- set up all your content as text in a variable like my_data
-- the_file is your file name in a full (AppleScript) path to the place you want the file, i.e. "Target_Folder & File_Name" in a variable "my_file", say.
-- the_data is the text you want to insert in the file in a variable "my_data"
-- creator_code for TextEdit is ttxt
-- file_type for TextEdit is APPL, and by entering these, double clicking on the file will open TextEdit to read it.
-- then
writeToFile(my_file, my_data, ttxt, APPL)
-- the handler
on writeToFile(the_file, the_data, creator_code, file_type)
	try
		set f to open for access file the_file with write permission
		write the_data to f starting at eof as (class of the_data)
		close access f
		try
			tell application "System Events"
				if creator_code is not missing value then set creator type of (file the_file) to creator_code
				if file_type is not missing value then set file type of (file the_file) to file_type
			end tell
		end try
		return true
	on error the_error
		try
			close access file the_file
		end try
		return the_error
	end try
end writeToFile

Thanks a lot!

It is indeed a good script. My problem now is that I want to be able to overwrite my textfiles, but it won’t let me. As recommendend in several places, I’ve reset the eof, as in:

		
     set f to open for access file the_file with write permission
     set eof f to 0
     write the_data to f starting at eof as (class of the_data)

I keep getting error -49, file is already opened. I then tried catching the error and trying to gently tell applescript that I know the file exists and that it is okay to overwrite, but it still doesn’t work.


	on error m number n from f to t partial result p
		try
			close access file the_file
		end try
		if n is equal to -49 then
			set eof f to 0
			write the_data to the_file starting at eof as (class of the_data)
		end if
		return m
	end try

I could do a shell script echo to file, but it seemsto me that “open for access” shouldn’t behave this way.

Actually, the close access in the error handler should logically come after the second attempt to write. But it still makes no odds, the thing refuses to cooperate.