Skip to main content
Known Participant
November 27, 2024
Answered

Update the script that find paragraph style and change with another style + next styles

  • November 27, 2024
  • 1 reply
  • 307 views

Hi everyone,

Is it possible to set this script (by @luis_guimaraes) 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

 

 

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;
    }
}

 

This topic has been closed for replies.
Correct answer m1b

Hi @marko_5038 how about this? - Mark

 

function main() {

    const styleNames = ['Title', 'Text1', 'Text2', 'Text3'];

    var doc = app.activeDocument,
        text = doc.selection[0];

    if (
        !text
        || !text.hasOwnProperty('paragraphs')
        || 0 === text.paragraphs.length
    )
        return alert('Please select some paragraphs and try again.');

    // collect the styles
    var styles = [];
    for (var i = 0; i < styleNames.length; i++) {
        if (app.activeDocument.paragraphStyles.itemByName(styleNames[i]).isValid)
            styles.push(app.activeDocument.paragraphStyles.itemByName(styleNames[i]));
        else
            return alert('Error: no paragraph style "' + styleNames[i] + '".');
    }

    // process the paragraphs
    var paragraphs = text.paragraphs,
        previousStyle = text.paragraphs[0].appliedParagraphStyle;

    for (var i = 1, para; i < paragraphs.length; i++) {

        para = paragraphs[i];

        if (styles[0] !== para.appliedParagraphStyle) {

            // set the style according based on the previous style
            switch (previousStyle) {

                case styles[0]:
                    // title then text1
                    para.appliedParagraphStyle = styles[1];
                    break;

                case styles[1]:
                case styles[3]:
                    // text1 or text3 then text2
                    para.appliedParagraphStyle = styles[2];
                    break;

                case styles[2]:
                    // text2 then text3
                    para.appliedParagraphStyle = styles[3];
                    break;
            }

        }

        previousStyle = para.appliedParagraphStyle;

    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Paragraph Styles');

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
November 27, 2024

Hi @marko_5038 how about this? - Mark

 

function main() {

    const styleNames = ['Title', 'Text1', 'Text2', 'Text3'];

    var doc = app.activeDocument,
        text = doc.selection[0];

    if (
        !text
        || !text.hasOwnProperty('paragraphs')
        || 0 === text.paragraphs.length
    )
        return alert('Please select some paragraphs and try again.');

    // collect the styles
    var styles = [];
    for (var i = 0; i < styleNames.length; i++) {
        if (app.activeDocument.paragraphStyles.itemByName(styleNames[i]).isValid)
            styles.push(app.activeDocument.paragraphStyles.itemByName(styleNames[i]));
        else
            return alert('Error: no paragraph style "' + styleNames[i] + '".');
    }

    // process the paragraphs
    var paragraphs = text.paragraphs,
        previousStyle = text.paragraphs[0].appliedParagraphStyle;

    for (var i = 1, para; i < paragraphs.length; i++) {

        para = paragraphs[i];

        if (styles[0] !== para.appliedParagraphStyle) {

            // set the style according based on the previous style
            switch (previousStyle) {

                case styles[0]:
                    // title then text1
                    para.appliedParagraphStyle = styles[1];
                    break;

                case styles[1]:
                case styles[3]:
                    // text1 or text3 then text2
                    para.appliedParagraphStyle = styles[2];
                    break;

                case styles[2]:
                    // text2 then text3
                    para.appliedParagraphStyle = styles[3];
                    break;
            }

        }

        previousStyle = para.appliedParagraphStyle;

    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Paragraph Styles');
Known Participant
November 27, 2024

Hi @m1b ,

the script is perfect.

Thank you so much