Skip to main content
Known Participant
November 26, 2024
Answered

Script - Find paragrapf style and change with apply my style then next style

  • November 26, 2024
  • 2 replies
  • 397 views

Hi community,

i would like to find a solution for the next problem

I have a 400 pages book with a text set up in this way:

paragraph style "Subtitle 1" then

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

(the number of paragraphs "Body text" isn't a fix number

...

paragraph style "Subtitle 1" then

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

etc.

...

I would like to find, in selection text, all paragraps "Body text" between each "paragraph style "Subtitle 1" and apply paragraph styles in this way

"My new style 1"

than next style "My new style 2"

then next style "My new style 3"

than next style "My new style 2"

then next style "My new style 3"

....

until the next paragraph style "Subtitle 1" 
and than repeat all until the and of selection

Is this possible with a script?
Thank you in advance
Marko

 

 

This topic has been closed for replies.
Correct answer luis_guimaraes

You can tweek it to suit your needs better:

 

app.doScript(Main, language=ScriptLanguage.JAVASCRIPT, withArguments=[], 
    undoMode=UndoModes.ENTIRE_SCRIPT, undoName='Testing');

function Main(){
    //reference to each style
    const titleStyle = app.activeDocument.paragraphStyles.itemByName('Title')
    const textStyle1 = app.activeDocument.paragraphStyles.itemByName('Text1')
    const textStyle2 = app.activeDocument.paragraphStyles.itemByName('Text2')
    const textStyle3 = app.activeDocument.paragraphStyles.itemByName('Text3')

    //selected paragraphs (including first title)
    var paras = app.selection[0].paragraphs;
    var prevstyle = paras[0].appliedParagraphStyle;
    
    for (i=1; i<paras.length; i++){
        var para = paras[i];
        var curstyle = para.appliedParagraphStyle;
        
        //ignore titles
        if (curstyle==titleStyle){
            continue;
        }

        //apply new style
        if (prevstyle==titleStyle){
            para.applyParagraphStyle(textStyle1);
        }
        else if (prevstyle==textStyle1){
            para.applyParagraphStyle(textStyle2);
        }
        else if (prevstyle==textStyle2){
            para.applyParagraphStyle(textStyle3);
        }
        else if (prevstyle==textStyle3){
            para.applyParagraphStyle(textStyle2);
        }

        //update prev
        prevstyle = para.appliedParagraphStyle;
    }
}

2 replies

Known Participant
November 27, 2024

Hi everyone,

Is it possible to set this script so that every time it encounters the "Title" style it starts over with the "Text1" style and then the Next styles?
For example:
"Title" -"Text1" - "Text2" - "Text3"" - "Text2" - "Text3" ....... "Title" - "Text1" - "Text2" - "Text3"" - "Text2" - "Text3" ...

Thank you in advance

luis_guimaraesCorrect answer
Participating Frequently
November 26, 2024

You can tweek it to suit your needs better:

 

app.doScript(Main, language=ScriptLanguage.JAVASCRIPT, withArguments=[], 
    undoMode=UndoModes.ENTIRE_SCRIPT, undoName='Testing');

function Main(){
    //reference to each style
    const titleStyle = app.activeDocument.paragraphStyles.itemByName('Title')
    const textStyle1 = app.activeDocument.paragraphStyles.itemByName('Text1')
    const textStyle2 = app.activeDocument.paragraphStyles.itemByName('Text2')
    const textStyle3 = app.activeDocument.paragraphStyles.itemByName('Text3')

    //selected paragraphs (including first title)
    var paras = app.selection[0].paragraphs;
    var prevstyle = paras[0].appliedParagraphStyle;
    
    for (i=1; i<paras.length; i++){
        var para = paras[i];
        var curstyle = para.appliedParagraphStyle;
        
        //ignore titles
        if (curstyle==titleStyle){
            continue;
        }

        //apply new style
        if (prevstyle==titleStyle){
            para.applyParagraphStyle(textStyle1);
        }
        else if (prevstyle==textStyle1){
            para.applyParagraphStyle(textStyle2);
        }
        else if (prevstyle==textStyle2){
            para.applyParagraphStyle(textStyle3);
        }
        else if (prevstyle==textStyle3){
            para.applyParagraphStyle(textStyle2);
        }

        //update prev
        prevstyle = para.appliedParagraphStyle;
    }
}
Known Participant
November 26, 2024

Woow!

Greate job @luis_guimaraes !

Thank you very much

Marko