Wednesday, August 1, 2018

Sometimes You Need a Blunt Instrument

Sometimes You Need a Blunt Instrument


I have a very complex script that does a lot of automatic page composition. One of the elements it has to work with is an inline group of figures with a caption. During the process of importing the images into this group, the group changes size. Because of the way the group is constructed, it is impossible to keep the inline sitting properly on the baseline as this happens.

So, the solution is to move it back inline. Well, I already had a function at hand that does this:
function moveInlineToBaseline(theGraph) {
var myBase = theGraph.parent.baseline;
var myBounds = theGraph.geometricBounds;
theGraph.move(undefined, [0, myBase - myBounds[2]]);
theGraph.parent.parentStory.recompose();
}
I wrote this for any inline graphic; hence the argument name theGraph. But for some reason, this logic failed on one instance in a very long document. The move command threw up an error to the effect that the object couldnt be moved by the requested amount because it would fall outside the bounding box of the containing frame. This was not true based on my measurements and examining the data, and I cannot see anything wrong with the logic, but attempting some debugging in ESTK, I discovered that the group in question couldnt be moved at all. Clearly, something odd was going on, but what?

Well, sometimes you just have to bite the bullet and try some other approach. In this case, I went for the Cut/Paste blunt instrument approach:
function moveInlineToBaseline(theGraph) {
var myBase = theGraph.parent.baseline;
var myBounds = theGraph.geometricBounds;
try {
theGraph.move(undefined, [0, myBase - myBounds[2]]);
} catch (e) {
// I dont know why this should ever fail, but I have an example, so lets go at it a different way in this case
var myIPindex = theGraph.parent.index;
var myStory = theGraph.parent.parentStory;
app.select(theGraph);
app.cut();
app.select(myStory.insertionPoints[myIPindex]);
app.paste();
myStory.recompose();
return
}
theGraph.parent.parentStory.recompose();
}
And that worked. With any luck, that Cut/Paste path through the script will never again be used, but if it is, I can be confident it will work because it solved my problem.

Note that when you paste a frame or group inline, it sits on the baseline properly, even if, as in this case, it was not on the baseline when it was cut. Thats because I selected the object and not the character containing the object. Had I done the latter, the position relative to the baseline would have been part of the cut information.

visit link download