javascriptmacrosemeditor

EmEditor macro in JS: how to get the full content of a document object


To get the content of a document in EmEditor macros, it seems that the full content need to be selected first and then Window.Document.Selection.Text is used to get its content. But this would lose the current position and seletion status.

Is there something like Window.Document.Content to do the job in a better way? Thank you!


Solution

  • You can use a loop to get the content of the document in a macro by appending each line, like so:

    var content = "";
    numberOfLines = document.GetLines();
    for ( i = 1; i <= numberOfLines; i++ ) {
        content = content + document.GetLine( i ) + "\r\n";
    }
    

    It will leave the cursor and all selections in place. But this method is slow if your file has more than hundreds of lines.