tell application "Finder" to set filenames to name of every item of folder "Your:Path:Here:"
set text item delimiters to ASCII character 10
tell application "TextEdit" to make new document at end of documents with properties {text:filenames as Unicode text}
Not to be competitive, but this one is a bit shorter, faster and safe (tids were not reset in hhas’ sample):
tell application "TextEdit" to make new document at end of documents with properties ¬
{text:do shell script "ls " & quoted form of POSIX path of "path:to:dir:"}
None of which is important. Code length is irrelevant; clarity and simplicity is what counts. Speed concerns here are irrelevant, being an example of premature optimisation (OMM they’re both about the same speed). And TIDs are inherently unsafe; it’s a flaw in the language’s design. The only way to write safe code is to always set TIDs before you perform any operation that involves them, which my example does. Whether you restore their previous value afterwards is one-tenths technical decision (does your code need to operate forgivingly in a stupid, unsafe environment?) and nine-tenths religion (which is completely worthless).
Whether you use ls or Finder here is largely an implementation detail: the point of my example was to generate some general principles behind application scripting, not least how to manipulate apps via the Apple Event Object Model as opposed to mimicking the way one would perform the same task in the GUI (make a selection, then cut-n-paste). You could just as easily demonstrate how to display a directory listing in TextEdit completely from the shell:
ls DIRECTORY > FILE; open -e FILE
BTW, a useful addendum might be demonstrating how to insert the filenames into an existing TextEdit document at the current cursor position, which would require GUI scripting as TE doesn’t provide a ‘selection’ property. I conveniently skipped over this as GUI scripting is a kludge and not the ideal thing to show as a first example, and I guessed the OP maybe just wanted to place the text in a new document anyway which can be done via TE’s existing scripting interface.
Also, in TextEdit, there’s no way to insert text at the insertion point as hhas pointed out, but you can replace text, paragraphs, words, or characters. Somthing like this:
tell application “TextEdit”
activate
tell front document
try
set last_char to last character
set last character to (last_char & return & “Here’s more text.” & return)
on error – no text
set text of it to “This is the first entry.” & return
end try
end tell
end tell
The simplest way to append text to a TextEdit document is:
make new paragraph at end of document 1 with data "Hello World"
Not very logical or attractive-looking, but it does the job. Cocoa Scripting’s standard Text Suite is kinda weak and counterintuitive, unfortunately. (e.g. If you really do want to add a new paragraph, you’ll need to include your own newline character, as in “nHello World”.)
In Tex-Edit Plus, which has a far more powerful and much better designed Apple Event Object Model (to the point it seems almost to read your mind!), it’s much more obvious:
set end of document 1 to "Hello World"
Learning how different applications implement their Apple Event Object Models is one of the challenges to learning application scripting. The scope, quality and style of scripting support can vary wildly from one app to another.
Actually, I was trying to show how to insert text anywhere in the document. I don’t know how you can ‘make’ text anywhere in the document. I was thinking that ActionB might want to do this later.
I don’t know if this one helps you further. With Tex-Edit it’s possible to work with the offset of a character or style run. Here’s a snippet of code to insert text in a document:
set char1 to offset of (first character of (every style run whose style is theStyle))
set char2 to offset of (last character of (every style run whose style is theStyle))
repeat with x from (count of items of char1) to 1 by -1
copy endofStyle to after character (item x of char2)
copy theCode to before character (item x of char1)
end repeat