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:
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:
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).
insertionPoint.contents = "\r";
withinsertionPoint.contents = "\r" + phonetics;
?newPhoneticsParagraph.contents = phonetics + "\r";
line..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 :)