understanding reference to a document vs its name
I'm having a hard time understanding how reference to a document works, and where in the AppleScript reference it is explained.
See this simple AppleScript:
tell application "TextEdit"
setMyDocumenttomakenewdocumentwith properties{name:"Name1"}
set text of MyDocument to "SomeText"
set text of MyDocument to text of MyDocument & " with more text"
set name of MyDocument to "Name2"
-- display dialog ("" & name of MyDocument) -- A: throws error
set MyDocument to document "Name2"
display dialog(""&nameofMyDocument)-- B: works fine
endtell
Should I remove the comments at the begining of the line where my comment A is, I would get an error from AppleScript like:
"Can’t make name of document "Name1" of application "TextEdit" into type Unicode text"
However, getting a new reference the "same" document using its new name allows me to display its name in a dialog.
So, I understand that, somehow, when I get a reference to a document object, that reference becomes obsolete if I change its name, as if the name actually defines - to some extent - the uniqueness of the object (although TextEdit can have more than one document named "Name2" at the same time, as is shown by the result of running the above script a few times without window cleaning).
I must say this behavior is somewhat a surprise to me, as I thought that, having a "pointer" to the object (I will avoid the term reference here), changing the value of a property of the object wouldn't render my pointer invalid. But no; it seems as if my pointer/variable is re-evaluated when used, and that this evaluation is done using the name property as if it was its unique key. Or is it something else?
What I would like to know is:
- How do I know what are the properties that "uniquely" defines an object (and which, if I change them, will screw up the validity of my variables)? Is the name property the only one, or are there other ones? Whenever I see a name property in a class, should I understand it is a "unique key", or are there classes where the name property can be changed without making any of the above problem?
- How do I know which property is used by default to evaluate where a variable points to?
- Is there any way to make a variable point to such an object without risking to lose it even when an "unique key" property is modified? I've played a bit with the "a reference to" operator with no success (although I might not have used it right).
- What part of the AppleScript reference explains this so my AppleScript-newbie/former-java-programmer brain can understand the logic behind this. I kinda understand what's happening, though I'd like to clearly be explained why it works that way.
Thanking in advance you, seasonned AP coders, for sharing your knowledge...