Search code examples
javascriptmacostexteditjavascript-automation

How do I return a manipulated array as text to TextEdit?


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


Solution

  • 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"))
    }