BUg, bugs!

tell application "TextEdit"
	set cl to "Test"
	tell application "Finder" to set the_path to (desktop as text)
	--> i get this result: "nameofstartupdisk:Users:Joy:Desktop:"
		set pg to document 1
	on error
		set pg to window 1
	end try
		set name of pg to cl
		save pg in (the_path & cl & "." & "rtf")
	end try
end tell

--this first script doesn't work well at the first time, once always after the second run. (a repeat-loop doesn't solve the problem) 
--This must be a bug!!!! The version below works always, but the same elements are given, why the script doesn't accept the !variable! 'pg' ??     >:(

			set name of window 1 to cl
			save window 1 in (the_path & cl & "." & "rtf")
		on error

			set name of document 1 to cl
			save document 1 in (the_path & cl & "." & "rtf")
		end try*)

Hi Joy,

It’s not necessary to address the Finder when you want to get the path to the desktop. Moreover you create a reference to the first document, but when you later rename this document, your reference points to an old and most probably invalid document object:

  1. Reference «pg« = e.g. «document “Untitled” of application “TextEdit”»
  2. Renaming the first document = e.g. «document “Test” of application “TextEdit”»
  3. Old reference «pg» = still e.g. «document “Untitled” of application “TextEdit”» from the first step!

The first reference is not automatically updated when you suddenly change the name of the document

Therefor I would suggest code like follows to solve your problems:

	-- no need to address the Finder!
	set the_path to ((path to desktop) as Unicode text)
	tell application "TextEdit"
		set cl to "Test"
		-- are there any open documents?
		set alldocs to every document
		if alldocs is {} then
			error "There are no open documents in Text Edit."
			set name of document 1 to cl
			save (document 1) in (the_path & cl & "." & "rtf")
		end if
	end tell
on error errmsg number errnum
	tell me
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop giving up after 30
	end tell
end try

Hi, Martin,

my question is connected to another post, where concerns a problem inside my “Text-app help”-code.

In other words, my new post represents only a demonstration (in my particular case) of a bug in Script editor, where causes a useless workaround. That can’t be true! (the problem appaers in Script editor only, launching the “Text-app help” script from external sources, e.g. the script-library (~/Library/Scripts/) or from Quicksilver (a killer-app)and not from the same front document in Scriptedit.)
You’ve to run the script 2 times to get the document saved (Scriptedit.), in Texteditor works already with the first run, how it should do.
I ran this script only with open documents, nevertheless, i’ll notice your tips, Martin.

Your sequence is correct, Jaques, but a minor problem persists:
the front document will not recognize his path, only after a second run, it does. (Perhaps a bug on Tiger, my laptop has only a cd-reading drive…)

It’s no program bug!
As Martin (and Jacques) described, renaming a new unsaved document destroys the reference to the document,
It must be referenced by name, a file path doesn’t exist (yet)

this sounds understandable. Now, the solution can’t be otherwise than:

set the_path to (y & cl & “.” & xt)
save in the_path
set name to cl
save in the_path

Thanks folks!