Skip to main content
Jonlin Creative
Inspiring
July 3, 2023
Answered

Find 2-line paragraphs with a certain style and change the style

  • July 3, 2023
  • 1 reply
  • 476 views

Hello,

I'm typesetting a workbook that has questions with lines to write the answers. Most of the questions are a single line, but some are two lines. All are set with the same paragraph style. I need to change all the two-line questions to a different paragraph style and I'm wondering if there's an automated way to do that.

Thanks!

This topic has been closed for replies.
Correct answer FRIdNGE

… by Script:

 

var myDoc = app.activeDocument;

var myParaStyle1 = myDoc.paragraphStyles.item("xxx");
var myParaStyle2 = myDoc.paragraphStyles.item("yyy");

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = ".+";
app.findGrepPreferences.appliedParagraphStyle = myParaStyle1;  
myFound = myDoc.findGrep();
var F = myFound.length,  f;
for ( f = 0; f < F ; f++ ) if ( myFound[f].lines.length == 2 ) myFound[f].appliedParagraphStyle = myParaStyle2;
app.findGrepPreferences = app.changeGrepPreferences = null;

alert( "Done!" )

 

(^/)

1 reply

Steve Werner
Community Expert
Community Expert
July 4, 2023

No doubt it will require a script because it's not built-in to InDesign. If you're lucky, someone may have created one or could write one.

FRIdNGE
July 4, 2023

… Hmm!

 

Of course, it could be done by a simplistic script but let's try to do it with … Grep [for those who know it]!

 

Supposing we have 2 para styles: "xxx" and "yyy".

 

First, add 2 nested lines styles to your para style "xxx":

First line: [none]

Second line: "zzz" char style (no attribute)

 

… And just play this simplistic Grep Find/Replace:

 

Find: $ + "zzz" char style

Replace by: "yyy" para style

 

Funny: 0 result, but it makes the job!

 

(^/)  The Jedi

FRIdNGE
FRIdNGECorrect answer
July 4, 2023

… by Script:

 

var myDoc = app.activeDocument;

var myParaStyle1 = myDoc.paragraphStyles.item("xxx");
var myParaStyle2 = myDoc.paragraphStyles.item("yyy");

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = ".+";
app.findGrepPreferences.appliedParagraphStyle = myParaStyle1;  
myFound = myDoc.findGrep();
var F = myFound.length,  f;
for ( f = 0; f < F ; f++ ) if ( myFound[f].lines.length == 2 ) myFound[f].appliedParagraphStyle = myParaStyle2;
app.findGrepPreferences = app.changeGrepPreferences = null;

alert( "Done!" )

 

(^/)