Text Edit- write the clipboard to a new document

All I am trying to do is open a new text edit document and write the clipboard to it. I have seen other posts, but only those that already refer to an existing document. In my scenario it could possibly be “Untitled 1”, “Untitled 2”, “Untitled 3”, etc., etc., depending upon how many unsaved documents that might already be open.

This script does not work, but it must be close?

tell application "TextEdit"
	activate
	set newFile to make new document
	set myTextFileName to name of document 1 as text
end tell

set tFile to open for access (path to desktop as text) & myTextFileName with write permission
try
	set eof of tFile to 0 -- to overwrite what's there, or leave this line out to append the clipboard to whatever is already in it.
	write (the clipboard) to tFile
	close access tFile
on error
	close access tFile
end try

I ran your script with no change and it worked flawlessly ” at least if what you want to save is the text component of the passed clipboard.
But there is no need to use TextEdit to do that.
This stripped script does exactly the same job.


set myTextFileName to "azertyuiop.txt"

set tFile to open for access (path to desktop as text) & myTextFileName with write permission
try
	set eof of tFile to 0 -- to overwrite what's there, or leave this line out to append the clipboard to whatever is already in it.
	write (the clipboard) to tFile
	close access tFile
on error
	close access tFile
end try

Yvan KOENIG (VALLAURIS, France) dimanche 27 juillet 2014 11:05:10

If I’ve understood correctly, Jeffkr wants to create a new document in TextEdit and save it with the default name assigned to it during creation. Assuming this is best for his purposes, it’s quite easy to do. Simply create the document with the text from the clipboard and save it. The file name used must have an appropriate “.txt” or “.rtf” extension:

set the clipboard to "I am some text."
set desktopPath to (path to desktop as text)

tell application "TextEdit"
	activate
	set newDoc to (make new document with properties {text:(the clipboard)})
	save newDoc in file (desktopPath & (name of newDoc) & ".txt")
end tell

It works best if the extension used matches the default text format for new documents as set in TextEdit’s preferences. That is, use “.txt” if documents are created as plain text and “.rtf” if they’re created as rich text. You can in fact use either extension and get the corresponding kind of file, but there are pecularities when saving from one type to another.

The comment here’s not quite accurate. The write will start at the beginning of the file by default anyway immediately after ‘open for access’. What ‘set eof of tFile to 0’ does is to delete the old contents of the file so that if the new contents are shorter, there’s none of the old left sticking out at the end. If you want to append to the end of the existing contents, you have to use ‘starting at eof’ with the ‘write’ command:

set tFile to open for access (path to desktop as text) & myTextFileName with write permission
try
	write (the clipboard) to tFile starting at eof -- Append to existing contents, if any.
	close access tFile
on error
	close access tFile
end try

Thank you Nigel,
This works perfectly and I’m sure I’ll be using this basic new text document functionality often. My actual intent was to capture my script’s rather extensive dialog within a txt file so the user can manipulate the output for other purposes to their liking. Most importantly, thank you for explaining a few things rather than simply posting the answer. it’s much appreciated.

-Jeff