0

I'm trying to get an InDesign script that works like this: "In the currently selected text, whenever a Paragraph Style B appears after a Paragraph Style A, change that Paragraph Style B to Paragraph Style C."

If there's a way to do it with Find/Change, that would also work.

AI was able to write a functional script that does this globally, but failed to write one that works only in the current selection. Here's the failed attempt, for reference:

var selectedPars = app.selection;
for (var i = 0; i < selectedPars.length - 1; i++) {
    if (selectedPars[i].appliedParagraphStyle.name == "Paragraph Style A" && selectedPars[i + 1].appliedParagraphStyle.name == "Paragraph Style B") {
        selectedPars[i + 1].appliedParagraphStyle = "Paragraph Style C";
    }
}

And here's the script that runs globally, working as intended, also for reference:

var allPars = app.activeDocument.textFrames.everyItem().paragraphs.everyItem().getElements();
for (var i = 0; i < allPars.length - 1; i++) {
    if (allPars[i].appliedParagraphStyle.name == "Paragraph Style A" && allPars[i + 1].appliedParagraphStyle.name == "Paragraph Style B") {
        allPars[i + 1].appliedParagraphStyle = "Paragraph Style C";
    }
}

Any help is appreciated!

2
  • I think the problem is that app.selection will be a different class depending on what you have selected, but the script assumes that it's an array of paragraphs. Try running a script with alert(app.selection) with different things selected. Selecting a text frame will alert object TextFrame, a whole paragraph: object Paragraph, a single letter: object Character, a selection across paragraph: object Text, a whole column: object TextColumn etc. You need you script to differentiate between these.
    – Wolff
    Sep 29, 2023 at 16:06
  • This is a good documentation of the ExtendScript API. I've tried using ChatGPT (which I assume you've used here) to help me write scripts for InDesign. It's useful for inspiration I would say, but it always misses details about how the object model works. Uses methods/attributes that doesn't exist, simplifies too much and so on.
    – Wolff
    Sep 29, 2023 at 16:09

1 Answer 1

-1

Updated:

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Set Answer Styles");

function main() {

    var questionStyle = app.activeDocument.paragraphStyles.itemByName("Paragraph Style A");
    var answerStyleA = app.activeDocument.paragraphStyles.itemByName("Paragraph Style B");
    var answerStyleB = app.activeDocument.paragraphStyles.itemByName("Paragraph Style C");
    var answerStyleAParagraphIndex = 1; // first answer (change to 2 for 2nd answer, etc.)

    app.findTextPreferences = null;
    app.findTextPreferences.appliedParagraphStyle = questionStyle;
    var myResults = app.activeDocument.findText();
   
    // iterate over each found questionStyle paragraph
    for (i = 0; i < myResults.length; i++) {
        var myParagraph = myResults[i];
        var paragraphIndex = 1; // counter

        // iterate over the non-question-style paragraphs directly after the question
        while (myParagraph.isValid) {
            myParagraph = myParagraph.parentStory.paragraphs.nextItem(myParagraph);
            if (!myParagraph.isValid || myParagraph.appliedParagraphStyle === questionStyle) {
                // is either invalid (doesn't exist) or it's another question
                break;
            }

            // use index of paragraph to set styles
            if (paragraphIndex == answerStyleAParagraphIndex) {
                myParagraph.appliedParagraphStyle = answerStyleB;
            } 
            paragraphIndex++;
        }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.