0

I'm trying to automatically insert a new paragraph below every paragraph that has a particular style.

This is working well, but there is the side effect of changing the style of the paragraph following the one inserted with the style of the inserted one. That'd be Problem 1.

If I add a \r after my inserted content, then the following paragraph keeps its style, but an extra empty line appears below each inserted paragraph. That'd be Problem 2.

If I try to remove this empty line using scripts or find/replace, then I fall back to the Problem 1 of losing the style of the following paragraph.

But, weirdly enough, if I just place my cursor in the empty paragraph and manually press backspace, the empty paragraph disappears and both paragraph retain their style. I don't have a clue why it's different between manual processing and scripting.

Here's a visual demo of my issue:

Demo of my problem with inserting a new paragraph

The desired output would be:

You can download here a minimal .inds file for replication.

And the script itself with the lines to replicate Problems 1 and 2:

var document = app.activeDocument;

for (var storyIndex = 0; storyIndex < document.stories.length; storyIndex++) {
  var story = document.stories.item(storyIndex);
  
  for (var paragraphIndex = story.paragraphs.length - 1; paragraphIndex >= 0; --paragraphIndex) {
      var currentParagraph = story.paragraphs.item(paragraphIndex);
      
      if (currentParagraph.appliedParagraphStyle.name === "Tibetan") {
          var phonetics = generatePhoneticsFor(currentParagraph.contents);
          
          var insertionPoint = currentParagraph.insertionPoints.item(-1);
          insertionPoint.contents = "\r";
          
          var newPhoneticsParagraphIndex = paragraphIndex + 1;
          if (newPhoneticsParagraphIndex < story.paragraphs.length) {
              var newPhoneticsParagraph = story.paragraphs.item(newPhoneticsParagraphIndex);
              newPhoneticsParagraph.contents = phonetics; // Problem 1
              // newPhoneticsParagraph.contents = phonetics + "\r"; // Problem 2
              newPhoneticsParagraph.appliedParagraphStyle = document.paragraphStyles.itemByName("Phonetics");
          }
      }
  }
}

The background is that I'm trying to set up a script that would insert below every Tibetan line the phonetics of how it should be pronounced for those who don't read Tibetan. I'm successfully using a Perl script that does this relatively complex task, but I can't get to do this relatively simple thing of appending a new paragraph!

Any help would be much appreciated :)

Thanks for reading this far, Have a great day!

Edit:

Doing insertionPoint.contents = "\r" + phonetics; as suggested by @cybernetic.nomad results in:

enter image description here

Here the problem is that we have an extra new line of Phonetics style, and the phonetics paragraph is applied the same style as the english translation (with left justify, resulting in the weird spacing).

9
  • 1
    You show us the original and two problems, but could you also include a sample of what the end result should be?
    – user183813
    Commented Apr 10 at 12:51
  • @cybernetic.nomad Just added the desired output and a sample.inds file to try to replicate. Thanks :)
    – Jeremy F.
    Commented Apr 10 at 20:58
  • What happens if you replace insertionPoint.contents = "\r"; with insertionPoint.contents = "\r" + phonetics;?
    – user183813
    Commented Apr 11 at 12:55
  • 1
    Have you checked to make sure that every paragraph ends with an actual carriage return and not a soft return? I just tried your script (admittedly not on your file -- can't download from unknown sources, company policy) and it seems to work fine for me with the newPhoneticsParagraph.contents = phonetics + "\r"; line.
    – user183813
    Commented Apr 11 at 13:56
  • 1
    @cybernetic.nomad Okay just figured it out! It turns out you were right about soft breaks. You couldn't reproduce it because the soft break actually came from my generatePhonetics function. I used a .replace(/\r\n/g, '') to ensure the output of that function was free of new lines when I really meant .replace(/[\r\n]/g, ''). Would you like to post an answer that I would mark as the good answer? That would seem fair since you did point me precisely to the core of the issue :)
    – Jeremy F.
    Commented Apr 12 at 14:33

1 Answer 1

1
+50

So this screen capture:

enter image description here

Indicates that the second line of text ends with a soft return. This seemed odd as InDesign ignores soft-returns when looking at the end of paragraphs, only considering hard-returns. I couldn't recreate the problem because I did not have access to the Perl script, which ended the string with a soft return instead of a carriage return. According to Jeremy, a fix in that script resolves the problem

Side note: One thing to help spot these kinds of problems is to have the invisible characters visible (Command-Option-I)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.