简体   繁体   中英

Reliably detecting text overflows in TextFrames in InDesign Server using javascript scripting?

I'm doing some Javascript scripting with InDesign Server at the moment. I'm having trouble trying to reliably detect overflows in TextFrames, after changing formatting or placing XML into them.

For example, I've got a function that shrinks a 4 column TextFrame's height until the text overflows the frame. Then it increases the height until it no longer overflows. This should result in as close to equal column heights as possible.

while(!bodyTextFrame.overflows) {
  var bounds = bodyTextFrame.geometricBounds;
  bodyTextFrame.geometricBounds = [bounds[0], bounds[1], bounds[2] - 1, bounds[3]];
  //app.consoleout("shrinking");
}

while(bodyTextFrame.overflows) {
  var bounds = bodyTextFrame.geometricBounds;
  bodyTextFrame.geometricBounds = [bounds[0], bounds[1], bounds[2] + 1, bounds[3]];
  //app.consoleout("expanding");
}

In InDesign desktop this works fine (with some modifications to make it use the currently selected object), but in InDesign Server this seems to overshoot during the shrinking phase, and then only expand once.

A similar problem also occurs after placing XML into a TextFrame and then detecting whether that text has caused an overflow. If I check for the overflow directly after placeXML(), it always returns false, but if I check for the overflow at a later part of the script, it detects it correctly.

It's a bit like there's a delay in calculating whether the text overflows, but it carries on through the script regardless until the overflow property is updated on the TextFrame.

Is there a way of forcing the script to wait until the overflow property has been updated? Or setting the mode of the script to wait for the refresh? Or am I just doing it wrong?

As you found, composition is a deferred task. Most scripting activities that require valid composition will force it automatically, but sometimes you have to use the recompose() method, eg

myDocument.recompose()

So, it turns out that this was being caused by a side effect of how my XML was structured. The XML I was applying to the TextFrame contained a number of <p> tags which seemed to confuse the layout engine when assessing the overflows. I ran my XML through a script to replace the tags with &#x2029; (the paragraph separator character) and it works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM