Thursday, December 29, 2005

 

Quick & Dirty Paragraph Shuffler

My client had me insert new paragraphs into a long structured document. Due to a miscommunication, I initially inserted the paragraphs in the wrong place on each page. It transpired that the paragraphs (in the paragraph style "Workshop") needed to be immediately before the corresponding "Description" paragraph rather than after the "Isotype" paragraph where I'd put it. Here's how I solved the problem:
Document.prototype.longestStory = function() {
  var myStories = this.stories.everyItem().length;
  var myLim = myStories.length;
  var longStory = 0;
  for (var i = 0; myLim > i; i++) {
    if (myStories[i] > longStory) {
      var myStory = i;
      longStory = myStories[i];
    }
  }
  return this.stories[myStory]
}
myDoc = app.activeDocument;
myStory = myDoc.longestStory();
myStyles = myStory.paragraphs.everyItem().appliedParagraphStyle;
n = 0; // Indicates most recent index of a Description paragraph
for (var j = myStyles.length - 1; j >= 0; j--) {
  theName = myStyles[j].name;
  if (theName == "Description") {
    n = j;
    continue
  }
  if (theName == "Workshop") {
    if (n == 0 ) {
      alert ("Oops");
      exit();
    }
    myStory.paragraphs[j].move(LocationOptions.before, myStory.paragraphs[n]);
  }
}
I put in the test just in case I had misspelled the name of the style. I didn't want all those paragraphs moved to the start of the story.

Comments: Post a Comment

<< Home

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