InDesign Server CS6 Scripting - Get TextFrame containing DataMergeField

400 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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);
            //...
        }
    }
}
0
On

Something like this should work in JS:

var document = app.open('template.indd');
var hs = document.dataMergeTextPlaceholders;
var n = hs.length, h, tf;
while (n--) {
  h = hs[n];
  if ( h.storyOffset.parentTextFrames.length ) {
    tf = h.storyOffset.parentTextFrames[0]; 
    //do something with tf
   }
}