javascriptadobe-indesignindesign-server

Text frames disappearing after applying a ParagraphStyle


I don't have a lot of Indesign experience to say the least but I was asked to investigate if it was possible (using indesign server of scripting) to start a new document, apply a master spread page, insert some paragraphs and applying some paragraph styles.

The solution I came up is this

// define template
var indesignTemplate = new File("/e/mytemplate.indt");

// open the template
var doc = app.open(indesignTemplate);

// get master page
var masterPage = doc.masterSpreads.item("A-Master");

// get first page
var page = doc.pages.item(0);

// apply master page to our first page
page.appliedMaster = masterPage

// get paragraph style
var paragraphStyle = doc.paragraphStyles.item("_2.ondertitel_bladzijde");

for (var i = 0; i < masterPage.textFrames.length; i++) {
     var textframe = masterPage.textFrames.item(i);
    if (textframe.label === "flow") {
        for (var x = 0; x < 5; x++) {
            // insert another new paragraph
            textframe.parentStory.insertionPoints.item(-1).contents = "Lorem ipsum dolor... \r\r";
        }

       for (var x = 0; x < textframe.paragraphs.length; x++) {
         textframe.paragraphs.item(0).applyParagraphStyle(paragraphStyle);
       }

     }
}

//Save the document (fill in a valid file path).
doc.save(new File("/c/mybook.indd"));

// Save the document as an PDF
doc.exportFile(ExportFormat.pdfType, new File("/c/mybook.pdf"));

// close the document.
app.documents.item(0).close();

This works and can see my text when I comment out the applyParagraphStyle code.

From the moment that I try to apply a paragraphStyle the text gets hidden. When I then open the saved indd file in Indesign and command+shift click in the empty text frame, the text appears and I also see an extra layer appearing.

I also tried applying CharacterStyles in more or less the same way but that doesn't give any problems.

I assume that the problem lays in the fact that I'm may approaching this in the wrong way ?


Solution

  • In the end I came up with the following which fixed my problem. I don't know if i'm overengineering a bit or this is under the rules of the art.

    var indesignTemplate = new File("/e/boek_nl.indt");  
    
    // open the template  
    var doc = app.open(indesignTemplate);  
    
    // get master page  
    var masterPage = doc.masterSpreads.item("A-Master");  
    
    // get first page  
    var page = doc.pages.item(0);  
    
    // apply master page to our first page  
    page.appliedMaster = masterPage  
    
    // get paragraph style  
    var paragraphStyle = doc.paragraphStyles.item("_2.ondertitel_bladzijde");  
    
    // get text frame  
    function getTextFrame(pageObj, frameName) {  
       var allItems = pageObj.appliedMaster.pageItems.everyItem().getElements();  
    
    for(var j=0;j<allItems.length;j++)  
    {  
        if(allItems[j].label === frameName) {  
            return allItems[j].override(pageObj);  
        }  
    }  
    
    var textFrame = getTextFrame(page, "flow")  
    
    // insert a paragraph 5 times  
    for (var x = 0; x < 5; x++) {  
       textFrame.parentStory.insertionPoints.item(-1).contents = "text \r\r";  
    }  
    
    // apply paragraph style  
    textFrame.paragraphs.item(0).applyParagraphStyle(paragraphStyle);  
    
    //Save the document (fill in a valid file path).  
    doc.save(new File("/c/boek_nl.indd"));  
    
    // Save the document as an PDF  
    doc.exportFile(ExportFormat.pdfType, new File("/c/boek_nl.pdf"));  
    
    // close the document.  
    app.documents.item(0).close();