javascriptadobeextendscriptindesign-server

InDesign Server CS6 Scripting - Get TextFrame containing DataMergeField


I am unable to determine if a DataMergeField is contained within a TextFrame.

var document = app.open('template.indd');
var dataMerge = document.dataMergeProperties;
var field;
for (field in document.dataMergeTextPlaceholders) {
    var story = field.parentStory;
    var content = story.contents;
    var textFrame = story.textFrames.item(0);
    //textFrame is null
}
//....

I am attempting to provide wrap, fit, fill options for the textual contents of any DataMergeFields, adjusting them based on the TextFrame dimensions. Without knowing any of the DataMergeFields or TextFrames properties used in the document.


Solution

  • I was able to get this to work with the following. I am guessing that using document as opposed to app.documents.item(0) was the issue.

    app.open('template.indd');
    var phs = app.documents.item(0).dataMergeTextPlaceholders;
    var i, textFrame, ph, story;
    for (i = 0; i < phs.length; i++) {
        ph = phs.item(i);
        if (ph instanceof DataMergeTextPlaceholder) {
            story = ph.parentStory;
            if (story.textFrames.length > 0) {
                textFrame = story.textFrames.item(0);
                //...
            }
        }
    }