adobe-indesignextendscriptgrep-indesign

InDesign Endnote Automation


I'm using this simple script to grab text wrapped in [ENDNOTE][/ENDNOTE] tags and make them into actual InDesign endnotes. The problem I'm having is that only the first 4 characters are being placed between the endnote markers (see screenshot below), any idea why this is happening, or how to make it work right?

var doc = app.activeDocument;

function footnoteToEndnotes () {
    app.findGrepPreferences = null;
    app.findGrepPreferences.findWhat = '\\[ENDNOTE\\](.+?)\\[\\/ENDNOTE\\]';
    var endnote,
        fnotes = doc.findGrep();

    for (var i = fnotes.length-1; i >= 0; i--) {
        var taggedText = fnotes[i].contents.replace('[ENDNOTE]', '').replace('[/ENDNOTE]', '');
        endnote = fnotes[i].insertionPoints[0].createEndnote();
        endnote.texts[0].contents = taggedText;
        fnotes[i].remove();
    }
}

if (parseInt (app.version) < 13) {
    alert ('This script requires InDesign CC2018 or later');
    exit();
}

doc.endnoteOptions.frameCreateOption = EndnoteFrameCreate.NEW_PAGE;
footnoteToEndnotes();

enter image description here


Solution

  • So, when I set the content of insertionPoints[0], the full endnote ended up inside the endnote markers. But, there was an extraneous tab character at the end of the string... I stripped it out and it's working like I want!

    I replaced this line:

    endnote.texts[0].contents = taggedText;
    

    With these:

    endnote.texts[0].insertionPoints[0].contents = taggedText;
    endnote.texts[0].contents = endnote.texts[0].contents.replace('\t', '');