Reply To: Scripting: In a loop, find consecutive paragraphs with the same style

Home Page / Forums / General InDesign Topics (CLOSED) / Scripting: In a loop, find consecutive paragraphs with the same style / Reply To: Scripting: In a loop, find consecutive paragraphs with the same style

#14402209
Ian
Member

Ended up using an array to solve the multiple paragraphs issue.

// Original code by Jonathan Leathwood
// https://creativepro.com/topic/using-javascript-to-move-paragraphs-to-anchored-objects/
//
//DESCRIPTION:Cuts all paragraphs of a certain paragraph style, reinserts them as text anchors, and applies an object style

var myDoc = app.activeDocument; // The open document: the scene of the action

if (app.selection[0] instanceof TextFrame) {

  var myParas = app.selection[0].parentStory.paragraphs; // The collection of paragraphs
  var myParaCount = app.selection[0].parentStory.paragraphs.length; // The number of paragraphs in the story
  var myCapture = []; // Create an array to store selected paragraphs

  myResult = confirm("This script requires a paragraph style named Main > Ruled, another paragraph style named Main > Fillable Form, and an object style named Fillable Form. Do you have those set up?");
  if (!myResult) {
    exit();
  }

  // Make a reference to the paragraph style
  var paraStyleSelect = app.activeDocument.paragraphStyleGroups.item("Main").paragraphStyles.item("Ruled");
  var paraStyleNew = app.activeDocument.paragraphStyleGroups.item("Main").paragraphStyles.item("Fillable Form");
  var objectStyleSelect = app.activeDocument.objectStyles.item("Fillable Form");

  // Loop to make the change on every paragraph that has the selected style
  for (i = myParaCount - 2; i >= 0; i--) {
    if (myParas[i].appliedParagraphStyle == paraStyleSelect) {

      myCapture.push(myParas[i]);

      var j = i - 1;
      while (myParas[j].appliedParagraphStyle == paraStyleSelect) {
        myCapture.push(myParas[j]);
        j--;
      }

      myParas[i + 1].insertionPoints[0].contents = "\r"; // Add a new hard return i.e. a new paragraph
      myParas[i + 1].appliedParagraphStyle = paraStyleNew; // i + 1 is now that newly created paragraph
      var myNewTextFrame = myParas[i + 1].insertionPoints[0].textFrames.add(); // Make a new text frame

      for (var k = 0; k < myCapture.length; k++) {
        myCapture[k].move(LocationOptions.AT_BEGINNING, myNewTextFrame.insertionPoints[0]); // Move the paragraph into the new text frame at the first insertion point
      }

      // Convert text box to fillable form text field
      app.select(myNewTextFrame);
      var convertToTextField = app.menuActions.itemByName("$ID/$$$/Command/TextField");  // Menu command "Convert To Text Field":
      // If available:
      if (convertToTextField.enabled) {
        convertToTextField.invoke();
        app.selection[0].applyObjectStyle(objectStyleSelect, true, true);
        app.selection[0].multiline = true;
        app.selection[0].appliedFont = "Georgia";
      };
    }
  }
  alert("High five!");

} else {
  alert("Please select a text frame and try again.");
  exit();
}

This article was last modified on April 2, 2024

Comments (0)

Loading comments...