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

Using JavaScript to move paragraphs to anchored objects

Return to Member Forum

  • Author
    Posts
    • #14346910
      Jonathan Leathwood
      Participant

      Hi all, I’m a new member and this is my first post!

      I’m looking for help with a script. I’m new to scripting and to programming in general. I’ve been through Peter Kahrel’s book and a lot of the InDesign scripting guide.

      WHAT I’M TRYING TO DO
      1. Collect a bunch of paragraphs in the document, all with the same paragraph style. (I want every such paragraph.)
      2. For each paragraph in the collection, take it out of the body text and put it inside a new text frame. The new text frame needs to be anchored to its original location in the document. Character styles also need to be preserved.

      WHY I’M TRYING TO DO IT
      The source Word document is an article that includes image captions inside the body text. Each caption is its own paragraph with a style applied. When I come to place the images, I’ll need each caption to be in its own text frame so that it can be styled and placed with the image.

      Before I place the images, I’d like to have all the captions in text frames ready to go on the correct page with one click.

      HOW I’M TRYING TO DO IT
      I have tried to do this with a loop:
      1. Start at the next-to-last paragraph (the last paragraph cannot have the required style). If the paragraph has the required paragraph style:
      2. Create a new text frame at the first insertion point of the following paragraph (i.e. an anchored text frame).
      3. Apply object style to new text frame.
      4. Using move(), put the current paragraph inside the new text frame.
      5. Loop back, going back one paragraph each time.

      I will put the code at the end of the post.

      THE PROBLEM

      Here’s the problem. If there are just a few such paragraphs, the script works instantly and I get exactly what I want. But on a long document with, say, forty such paragraphs to be processed, InDesign stops responding and I have to force quit.

      I have tried to troubleshoot the problem in the following ways:
      1. Don’t use anchored text objects. Instead, place the text frame using geometric bounds derived from the paragraph line. This works instantly every time, but the result is a bit less useful.
      2. Remove various elements from the script. It’s clearly the part where the anchored text frames get populated with text that is too much for my computer.
      3. Run the script with the document closed. This still makes InDesign unresponsive.
      4. Limit the loop so that it stops after creating and populating a couple of text frames. This works, confirming that it’s the length of the document that is causing the problem.
      5. It’s possible to write the script working through the document forwards (updating the paragraph count depending on whether a paragraph has been removed or not), but it makes no difference.
      6. A completely different approach, finding each paragraph with GREP and then putting each found text object into a new text frame. But character styles are not preserved.

      Maybe I need to find a new approach that isolates the paragraphs in Word and places the text from there? But I’m wondering if there is something new I could learn about that would work more efficiently.

      Any help would be much appreciated.

      
      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 paraStyleCapt = app.activeDocument.paragraphStyleGroups.item("Captions").paragraphStyles.item("Caption");
      var objectStyleCapt = app.activeDocument.objectStyleGroups.item("Images and Captions").objectStyles.item("Caption Holder");
      
      // Loop to make the change on every paragraph that has the style "Caption"
      
      for (i = myParaCount - 2; i >= 0; i--) {
          if (myParas[i].appliedParagraphStyle == paraStyleCapt) { 
              var myCaption = myParas[i]; // Capture the paragraph
              var myNewTextFrame = myParas[i + 1].insertionPoints[0].textFrames.add(); // Make a new text frame
              myNewTextFrame.appliedObjectStyle = objectStyleCapt;
              myParas[i].move(LocationOptions.AT_BEGINNING, myNewTextFrame.insertionPoints[0]); // Move the paragraph into the new text frame at the first insertion point
              if (myNewTextFrame.characters[-1].contents == "\r") { // Check to see if the caption has a trailing paragraph at the end
                  myNewTextFrame.characters[-1].contents = ""; // Remove it
                  }
          }
      }
          alert("High five!"); 
      
    • #14346926
      David Blatner
      Keymaster

      I hope a real scripter jumps in here before too long (we don’t have as many reading here as we used to). There’s also a good scripting forum here:
      https://www.facebook.com/groups/639232573329900

    • #14346941
      Jonathan Leathwood
      Participant

      Thanks for your reply, David!
      I figured out what I was doing wrong.
      If there are two paragraphs in a row that need to be moved, then my script tries to place the second text frame in the same insertion point as the first. That’s easily fixed.

    • #14346950
      Jonathan Leathwood
      Participant

      I feel like I should reply to my own post one last time in case anyone sees it and is misled. There was in fact nothing wrong with the script. There was something in the object style that caused it to fail with a particular paragraph. I never did find the wrong setting, but by using the object model directory, https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.html, I was able to build up the object settings line by line, and then create a new object style out of the finished result.

    • #14346952
      David Blatner
      Keymaster

      Thanks for coming back and letting us know! So glad it’s working.

    • #14347288
      Nikki Jam
      Participant

      Here’s the problem. If there are just a few such paragraphs, the script works instantly and I get exactly what I want. But on a long document with, say, forty such paragraphs to be processed, InDesign stops responding and I have to force quit.

      I have tried to troubleshoot the problem in the following ways:
      1. Don’t use anchored text objects. Instead, place the text frame using geometric bounds derived from the paragraph line. This works instantly every time, but the result is a bit less useful.
      2. Remove various elements from the script. It’s clearly the part where the anchored text frames get populated with text that is too much for my computer.
      3. Run the script with the document closed. This still makes InDesign unresponsive.
      4. Limit the loop so that it stops after creating and populating a couple of text frames. This works, confirming that it’s the length of the document that is causing the problem.
      5. It’s possible to write the script working through the document forwards (updating the paragraph count depending on whether a paragraph has been removed or not), but it makes no difference.
      6. A completely different approach, finding each paragraph with GREP and then putting each found text object into a new text frame. But character styles are not preserved.

      Maybe I need to find a new approach that isolates the paragraphs in Word and places the text from there? But I’m wondering if there is something new I could learn about that would work more efficiently.

      —Yes all these happens. thanks https://opautoclick.com

Viewing 5 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads