Mental block on TextEdit

I have hardly ever used TextEdit so I’m not familiar with scripting it. How do you do this simple task:

With text read from Microsoft Word (for example) in a variable, myText, how do I open a new TextEdit document/window and stick myText into it? This doesn’t work:

tell application "TextEdit"
activate
make new something at (path to desktop) with data myText with properties {name:"SomeText"}
end tell

-- as 'something' I've tried both document and window. In the at phrase I've tried every conceivable way of addressing the desktop folder. Balks at all of them.

I know the text transfer works: I can copy the text in a Word X document and paste it into TE.

See http://scriptbuilders.net/files/texteditexamples1.0.html for syntax examples. This should work:

set myText to "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam."
tell application "TextEdit"
	make new document at the beginning of documents with properties {text:myText}
	set the name of window 1 to "Some Text"
end tell

Incidentally, this is the issue that disgusts me with AppleScript; but I always thought it was because I didn’t understand it well enough. You read the guide, you read the dictionary, you write the command according to the rules, and… it doesn’t work. In this instance, why does “make new document” generate an error? For that matter, why doesn’t "with properties {name:“Some Name”} work? etc. Oh well…

thanks

make new document at the beginning of documents with properties {text:myText}

I never would have guessed that from the dictionary. Thanks.

Clearly, I don’t understand the document object model for TE at all.

You’ll see that elsewhere too.

AS Studio example:

tell user defaults
	make new default entry at end of default entries with properties {name:"myTextField", content:"default answer"}
end tell

To be fair to AppleScript, it’s not necessarily the language itself that’s the cause of confusion - but the different ways in which it might be implemented by developers in various applications. In addition, an application’s Applescript dictionary may not always be entirely helpful - or totally accurate. (I’m afraid the fact that an application is developed by Apple is no guarantee of conformity or exemplification, either.) For certain operations, an application may also rely on the standard Cocoa Scripting implementation - which, in itself, could be considered a work in progress…

That said, there are one or two points that may help to clarify things a little here.

Even in its simplest form (without location, property or data parameters - or, for that matter, an explicit ‘new’), the Standard Suite’s application command ‘make’ should work for a new TextEdit document:

tell application "TextEdit" to make document
tell application "TextEdit" to make new document

Where possible, it’s usually a good idea to focus on the application-specific dictionary entries. TextEdit’s Standard Suite, for example, lists a document’s properties as ‘modified’, ‘name’ and ‘path’ - but none are particularly relevant here (and should all, perhaps, be designated read-only). While the only document property in the TextEdit Suite is ‘text’, this is much more useful for our purposes.

Note that ‘with properties’ (rather than ‘with data’) is used to override the default properties of an application-created object. Since the default text property for a new document is an empty string, it can be replaced using something like:

tell application "TextEdit" to make new document with properties {text:"Hello world."}

Using ‘with data’ in the above context results in an error number 6: “NSArgumentsWrongScriptError” (indicating that an argument is of the wrong type, or is otherwise invalid). However, ‘with data’ should be used when the object is effectively defined by data from an outside source (such as a string or a file). As this next snippet demonstrates, the location parameter is also more critical with certain objects:

set The_Owl_and_the_Pussy_Cat to "And hand in hand on the edge of the sand\rThey danced by the light of the moon,\r"
tell application "TextEdit" to tell (make new document with properties {text:The_Owl_and_the_Pussy_Cat})
	make new paragraph at after last paragraph with data (get last paragraph)'s text from word -2 to -1
	tell last paragraph to set character 1 to ASCII character ((ASCII number (get character 1)) - 32)
	make new paragraph at after last paragraph with data last paragraph
	make new paragraph at after last paragraph with data second paragraph
	set character -2 to "."
end tell