Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Keeping italicised words when applying paragraph styles via a script

Community Beginner ,
Oct 13, 2025 Oct 13, 2025

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.");

 

 

 

 

 

 

TOPICS
Scripting
270
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 13, 2025 Oct 13, 2025

You can apply a paragraph style non-destructively:

 

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

 

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

Translate
Community Expert ,
Oct 13, 2025 Oct 13, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 13, 2025 Oct 13, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 13, 2025 Oct 13, 2025

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

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 13, 2025 Oct 13, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 13, 2025 Oct 13, 2025

Sorry, Prabu! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 13, 2025 Oct 13, 2025

.

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 13, 2025 Oct 13, 2025

Hi @ed_ben_c , You might want to search for italics and apply a character style—something like this (change Minion Pro   Italic to your font):

 

//active document
var doc = app.activeDocument;

//create styles—-makeStyle checks if the style already exists
var ital = makeStyle(doc, "Italic", "characterStyles")
var ifnt = "Minion Pro	Italic"
ital.appliedFont = ifnt
var bodyCopy = makeStyle(doc, "0 BODY TEXT", "paragraphStyles")
var catchlineCopy = makeStyle(doc, "1 CATCH LINE", "paragraphStyles")
var headlineCopy = makeStyle(doc, "2 Col sec HEADING", "paragraphStyles")
var introCopy = makeStyle(doc, "3 Body text INTRO", "paragraphStyles")
var quoteIntroCopy = makeStyle(doc, "4 Body intro QUOTES", "paragraphStyles")

var selection = doc.selection;
//NOTE doesn’t always work
if (!selection || selection.length === 0) {
    alert("No text box is currently selected.");
}
var textBox = selection[0];

//gets any words set as Minion Pro	Italic and applys the ital character style
app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
app.findTextPreferences.appliedFont = ifnt;
var res = app.activeDocument.findText()
for (var i = 0; i < res.length; i++){
    res[i].appliedCharacterStyle = ital
};   

//apply paragraph styles to the first 3 paragraphs
selection[0].paragraphs.everyItem().properties = {appliedParagraphStyle: bodyCopy}
textBox.paragraphs.item(0).appliedParagraphStyle = catchlineCopy;
textBox.paragraphs.item(1).appliedParagraphStyle = headlineCopy;

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


/**
* Make a new collection https://community.adobe.com/t5/indesign-discussions/how-to-create-various-styles-with-scripts/m-p/15344854#M626385
* @ param the document 
* @ param the collection name string 
* @ param the collection class as a string 
* @ return the named collection 
*/
function makeStyle(d, n, c) {
    if (d[c].itemByName(n).isValid) {
        return d[c].itemByName(n);
    } else {
        return d[c].add({name:n})
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 14, 2025 Oct 14, 2025

Thanks for this suggestion Rob.

Its very close to what i was using as a work-around (albeit much neater that what i had put together!) but I was keen to find an alternative method as this one would tie the text style of the italicised words to whatever the character style set it to, even if the style needed to change in the future (ie a different font if the story moved to a different section). 

Peter's answer below turned out to be exactly what i hoped for, so I'm going to go with that one. But again, thanks for the idea!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 13, 2025 Oct 13, 2025

You can apply a paragraph style non-destructively:

 

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

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 14, 2025 Oct 14, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2025 Oct 14, 2025

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

 

Screen Shot 27.png

 

 

Screen Shot 26.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 14, 2025 Oct 14, 2025
LATEST

Thanks for this, Rob. It will become my first port of call next time I'm stuck...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines