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