Sunday, February 12, 2006

 

Another Justification Job

The vertical justification feature built-into text frame options treats all paragraphs as equal. But for what I need to do right now, this is not the case. I have a story that runs across a number of pages (single-column text frames, actually) that has a paragraph style named "Cytokine" that is in effect a sub-head. I want to vertically justify these pages adding space before only to paragaphs in this style.

So, the first step is to identify the story. Because this particular story is the only one that uses this particular paragraph style, it is easy to find (assuming myDoc is set to the activeDocument):
app.findPreferences = null;
app.changePreferences = null;
var myStory = myDoc.search("", false, false, undefined, {appliedParagraphStyle:"Cytokine"})[0].parentStory;
So, now the task is to iterate through the text frames of the story distributing any spare space at the foot of each frame to the relevant paragraphs.
var myTFs = myStory.textFrames;
for (var j = myTFs.length - 1; j >= 0; j--) {
  processTF(myTFs[j]);
}
It occurs to me as I write this that a more general purpose version of this script would check that myStory is not overset before proceeding, but hey, I know it isn't. So, now all we need to do is write the function to process the text frames.
function processTF(theFrame) {
  // Note: assumes no stroke or inset
  var myParaStyles = theFrame.paragraphs.everyItem().appliedParagraphStyle;
  var myBase = theFrame.lines[-1].baseline;
  var targ = theFrame.geometricBounds[2];
  var CytoParas = new Array();
  for (var k = myParaStyles.length - 1; k >= 0; k--) {
    if (myParaStyles[k].name == "Cytokine") { CytoParas.push(k) }
  }
  if (CytoParas.length == 0) {
    selectIt(theFrame);
    errorExit("Frame has no eligible paragraphs");
  }
  var delta = (targ - myBase)/CytoParas.length
  for (var j = CytoParas.length - 1; j >= 0; j--) {
    theFrame.paragraphs[CytoParas[j]].spaceBefore = theFrame.paragraphs[CytoParas[j]].spaceBefore + delta
  }
}
I put in the test for zero eligible paragraphs out of a sense of duty: I didn't want to allow for the possibility of a divide by zero, even though I know the story in question doesn't have this problem. You can find the selectIt function elsewhere in this blog.

What you don't see here (because I've cleaned them up) are the four typos I made that resulted in a variety of syntax and runtime errors. But now the job is done and I can for now at least forget about this script.

This page is powered by Blogger. Isn't yours?