Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

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

Return to Member Forum

  • Author
    Posts
    • #14402069
      Ian
      Member

      What I’m trying to do

      1. Find all instances of paragraphs with a certain paragraph style.
      2. Cut the paragraphs and create a text frame where each group of paragraphs was.
      3. Apply an object style to the new text frame.
      4. Copy the cut text into the new text frame.
      5. Convert the new text frame to an interactive text field (Haven’t gotten to this step yet)

      Why I’m trying to do it
      I have a document with a bunch of blank lines printed in it so users can fill in their notes. I accomplish this with carriage returns that have a Ruled paragraph that adds a full width paragraph underline. I want to also create an interactive PDF that has form fields for users to add their notes digitally. My hope is that I can take my current document, run a script on it, and export a PDF ready to go.

      What isn’t working
      Currently, my javascript creates a new text box for each paragraph. But I have multiple lines for print users to add notes, and only need one text field for digital users. So I need a way to select all instances of multiple paragraphs with the same paragraph style.

      My code

      
      var myDoc = app.activeDocument; // The open document: the scene of the action
      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
      
      // Make a reference to the paragraph style for captions
      var paraStyleSelect = app.activeDocument.paragraphStyleGroups.item("Main").paragraphStyles.item("Ruled");
      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) {
          var myNewTextFrame = myParas[i + 1].insertionPoints[0].textFrames.add(); // Make a new text frame
          myNewTextFrame.appliedObjectStyle = objectStyleSelect;
          myParas[i].move(LocationOptions.AT_BEGINNING, myNewTextFrame.insertionPoints[0]); // Move the paragraph into the new text frame at the first insertion point
          
          // HAVEN'T EVEN TESTED THIS YET. JUST MY FIRST STAB AT MAKING THE TEXT FRAMES FILLABLE FORM FIELDS
          // Convert text box to fillable form text field.
          // var sel = app.selection[0]; // Text frame, rectangle, oval or polygon should be selected:
          // var convertToTextField = app.menuActions.itemByName("$ID/$$$/Command/TextField");  // Menu command "Convert To Text Field":
          // // If available:
          // if (convertToTextField.enabled) {
          //   convertToTextField.invoke();
          // };
        }
      }
      alert("High five!"); 
      

      Shout out to the original idea for this code!

    • #14402072
      Ian
      Member

      Update: Got the conversion to fillable form field working:

      
          // 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();
          };
      
    • #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();
      }
Viewing 2 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads