0

I'm new to JXA and I'm trying to learn how to do some very basic things in TextEdit. I know how to get the paragraphs of a document as an array:

app = Application('TextEdit')
docPars = app.documents[0].paragraphs()

And then, say, sort it. But I can't figure out how to send it back to TextEdit as an array (i.e. multiple paragraphs in a TE document).

TIA

  • Is there an API reference? Is there any way you can do e.g. `console.log` or `alert` to see the output of any given command? If so, you could see if there is a `setParagraphs` method? – Aurel Bílý Apr 06 '16 at 12:52
  • @AurelBílý, you can open the TextEdit scripting dictionary within the Script Editor application on OS X (just make sure you've set the language to Javascript as it may default to Applescript). I don't think there is a `setParagraphs` method. I think you create an object and then use the `push` method to make it appear in a document. See [the documentation under the heading "Creating Objects."](https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW12) – Jack Brannen Apr 07 '16 at 12:40

1 Answers1

0

Here is an example for you

var TextEdit = Application("TextEdit");
var newDocument = TextEdit.Document();
TextEdit.documents.push(newDocument);

for(var i = 0; i < 10; i++){
    newDocument.paragraphs.push(TextEdit.Paragraph({ color:"red", size:20 }, "Test line " + i + "\n"))
}

// update example with array

var TextEdit = Application("TextEdit");
var newDocument = TextEdit.Document();
TextEdit.documents.push(newDocument);

var array = ["test first line", "i love stackoverflow", "i love jxa", "i love apple"]

for(var i = 0; i < array.length; i++){
    newDocument.paragraphs.push(TextEdit.Paragraph({ color:"red", size:20 }, array[i] + "\n"))
}
maleeb
  • 890
  • 7
  • 15
  • Thanks, but not quite what I'm asking. I want to know how to push an array back to the document as paragraphs. – Jack Brannen Apr 07 '16 at 12:44
  • yeah you can modify it. iterate over an array to fill the lines. i update the answer with an example. – maleeb Apr 07 '16 at 19:15