Skip to main content
Participating Frequently
Oct 13, 2025 5:20 PM UTC
Answered

Keeping italicised words when applying paragraph styles via a script

Hi everyone. I wonder if anyone smarter than me can give me a nudge in the right direction here....

 

I've put together a script that automatically formats the text of a selected box to general body copy  and then formats the first three paras as headers and one of two intro styles, all using paragraph styles. So far so good. But...

 

Occassionally the text will have some italicised words in it. When I manually set Paragraph Styles it don't disrupt these italics, it just change the font family and the size, but leave the italicisation as it is. The same is not true of the same process when i use this script. I cannot work out why there would be a difference here.

 

Trying to find out more I came across a post that was answered with what might have been a solution by Jongware back in 2012, but his link points to what now seems to be a walware infested site, so i think the sands of time have swallowed that avenue...

 

Can anyone explain what I'm doing wrong? here's the code i've written so far. It works perfectly for a lump of text with no italics in it.

 

Thanks in advance to anyone who can give me any advice here. I'm just about ready to call it a day otherwise!

 

All the best,

Ed

 

***THE CODE***

 

var bodyCopy = "0 BODY TEXT"
var catchlineCopy = "1 CATCH LINE"
var headlineCopy = "2 Col sec HEADING"
var introCopy = "3 Body text INTRO"
var quoteIntroCopy = "4 Body intro QUOTES"
var selection = app.selection;

if (!selection || selection.length === 0) {
alert("No text box is currently selected.");
}

var textBox = selection[0];

for (var j = 0; j < textBox.paragraphs.length; j++) {
textBox.paragraphs.item(j).appliedParagraphStyle = bodyCopy;
}

var j = 0; {textBox.paragraphs.item(j).appliedParagraphStyle = catchlineCopy;}
var j = 1; {textBox.paragraphs.item(j).appliedParagraphStyle = headlineCopy;}
var j = 2;

var firstChar = textBox.paragraphs.item(j).contents.charAt(0);
if (firstChar === '“') {
textBox.paragraphs.item(j).appliedParagraphStyle = quoteIntroCopy;
}
else {
textBox.paragraphs.item(j).appliedParagraphStyle = introCopy;
}

alert("The paragraph style has been updated in the selected text box.");

 

 

 

 

 

 

Peter Kahrel
Peter KahrelCorrect answer
Community Expert
Oct 13, 2025 8:25 PM UTC

You can apply a paragraph style non-destructively:

 

textBox.paragraphs.item(j).applyParagraphStyle (introCopy, false);

 

where 'false' indicates 'do not remove local formatting'.

12 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
Oct 13, 2025 8:25 PM UTC

You can apply a paragraph style non-destructively:

 

textBox.paragraphs.item(j).applyParagraphStyle (introCopy, false);

 

where 'false' indicates 'do not remove local formatting'.

Translate
ed_ben_cAuthor
Participating Frequently
Oct 14, 2025 8:57 AM UTC

Thanks so much for this, Peter.

 

This is exactly the functionality i was hoping would be baked into InDesign's scripting language. I couldn't find any info on it anywhere, so you really are very kind for sharing.

 

I had no idea that as well as 'AppliedParagraphStyle' there was a whole separate 'ApplyParagraphStyle', with all its accompanying modifiers - it seems to almost never be mentioned in the research i've done on this up until now which is mad considering how useful it is.

 

One thing i found, and is probably worth feeding back - using this method with my Paragraph Style vars as I called them in my original script didn't work. I had to call them in a much more specific way so the script knew that they were Paragraph Styles, so for instance:

var bodyCopy = app.activeDocument.paragraphStyles.item("0 BODY TEXT");

Anyway, thanks again Peter, this is definitely the top answer and should be shouted about a lot more often so it flags up in searches more frequently!

 

All the best,

Ed

Translate
rob day
Community Expert
Community Expert
Oct 14, 2025 12:38 PM UTC

I couldn't find any info on it anywhere

 

Hi @ed_ben_c , Just in case you are not aware, you can find a very well organized API here:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Paragraph.html#d1e503310__d1e509635

 

 

 

Translate
Anantha Prabu G
Legend
Oct 13, 2025 6:23 PM UTC

You just need to preserve local formatting when applying paragraph styles in your script.

Translate
Design smarter, faster, and bolder with InDesign scripting.
ed_ben_cAuthor
Participating Frequently
Oct 13, 2025 6:59 PM UTC

Hi Anantha. That's interesting - I haven't heard this before. is that a command that you can specify in the jsx?  If so would you mind telling we where to call it?

Translate
ed_ben_cAuthor
Participating Frequently
Oct 13, 2025 7:03 PM UTC

Sorry, Prabu! 

Translate
Barb Binder
Community Expert
Community Expert
Oct 13, 2025 5:29 PM UTC

Hi @ed_ben_c:

 

I can't address the scripting part of this question, but perhaps can offer a clue to send you in right direction while you wait for someone who can: before you apply the paragraph styles, change the inline italics to a character style defined as italics (in your script or a via simple find/change query).

 

The character styles will hold even as you assign a paragraph style that removes overrides. 

 

~Barb

Translate
~Barb at Rocky Mountain Training
ed_ben_cAuthor
Participating Frequently
Oct 13, 2025 5:47 PM UTC

Thanks for the lead, Barb.

I did try fiddling with character styles using grep settings in the script but i feel like i was getting myself tied up in knots. Somehow, even though it only actually visually changed the italic words, the script i made gave ALL of the text in the box a Character style of italic (but with the appended +), which then threw things out of whack for the next part of the styling. Perhaps there's someone out there who can help me hone that part until it works... in the meantime, I'll continue to experiment with that...

 

Thanks again 🙂

Ed

Translate