Skip to main content
M.Hasanin
Inspiring
July 19, 2021
Answered

Applying Paragraph Style then Next Style not Working as Expected via JavaScript

  • July 19, 2021
  • 1 reply
  • 1072 views

Hi Experts, I Hope you are all fine, I'm trying to apply Paragraph Style then Next Style Automatically Based on Previous Found Style, heres is my code and Screen Shot, the Probelm is everytime excuting the code it only Apply Paragraph Style (AnswerStyle_B) to first Line after QuestionStyle Line and Ignore the rest of Lines, I'm trying to achive this Automatically via Script on all lines So Every line Start with A. will take the Style (AnswerStyle_A) and the Others (B-C-D) will take the Style (AnswerStyle_B) , I'm Still Learning JavaScript and try to figure out some problems , Please Help and Thanks in Advance  :

 

 

 

var doc = app.documents[0];
app.findTextPreferences = null;
app.findTextPreferences.appliedParagraphStyle = "QuestionStyle";
var myResults = app.activeDocument.findText();
for (i=0; i<myResults.length; i++)
myResults[i].parentStory.paragraphs.nextItem(myResults[i].paragraphs[0]).appliedParagraphStyle = app.activeDocument.paragraphStyles.item("AnswersStyle_A").nextStyle;

 

 

 

This topic has been closed for replies.
Correct answer m1b

No worries. This version will choose the first paragraph after each question. See if this suits your needs without converting anything.

 

 

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

function main() {

    var questionStyle = app.activeDocument.paragraphStyles.item('QuestionStyle');
    var answerStyleA = app.activeDocument.paragraphStyles.item('AnswersStyle_A');
    var answerStyleB = app.activeDocument.paragraphStyles.item('AnswersStyle_B');
    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 = answerStyleA;
            } else {
                myParagraph.appliedParagraphStyle = answerStyleB;
            }
            
            paragraphIndex++;
        }
    }

}

 

 

 

 

1 reply

m1b
Community Expert
Community Expert
July 20, 2021

Hi @M.Hasanin you made a good start. I think you may have confused the "nextItem" method of Paragraph with the "nextStyle" property of ParagraphStyle. The latter is just a reference to whatever style is set as the Next Style in that paragraph style, ie. the one that is applied automatically when you type a return while in that style.

 

Have a look at this code and see if you can make sense of what I've done here:

 

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

function main() {

    var questionStyle = app.activeDocument.paragraphStyles.item('QuestionStyle');
    var answerStyleA = app.activeDocument.paragraphStyles.item('AnswersStyle_A');
    var answerStyleB = app.activeDocument.paragraphStyles.item('AnswersStyle_B');
    var firstAnswerTest = RegExp('^A\.'); // matches first character A with period after it

    var doc = app.documents[0];
    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];
        // 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) {
                // paragraph is either invalid (doesn't exist) or it's another question
                break;
            }
            // use regular expression to check if first answer
            if (firstAnswerTest.test(myParagraph.contents)) {
                // this is answer "A"
                myParagraph.appliedParagraphStyle = answerStyleA;
            } else {
                // this is another answer
                myParagraph.appliedParagraphStyle = answerStyleB;
            }
        }
    }
}

 

 

M.Hasanin
M.HasaninAuthor
Inspiring
July 20, 2021

Thank you a lot for your reply, the script is working great but unlikly i forget to mention that (A. - B. - C. -D.) actually is Numbered List!, so the Script only works great if i convert all the Bullets and Numbering to Text!, i used this code first :
app.activeDocument.stories.everyItem().convertBulletsAndNumberingToText();
because its faster from Type > Bullet and Numbering > Convert numbering to text
But I wonder if there are any method to keep my Numbered List (Actually its Type is A,B,C,D, etc)
I tried to Alter the Variable firstAnswerTest to the following But with No Succeed :
var firstAnswerTest = RegExp('~8');
or
?<!\r)^.
^\d+?\.\s+
^.
^\.
^(?=.)
all the previous was a Trying to Change the Variable to Match the Numbered List of Type (A,B,C,D..etc), but not working! ,  any Way Thank you a lot for Helping me.

Mohammad Hasanin
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 20, 2021

No worries. This version will choose the first paragraph after each question. See if this suits your needs without converting anything.

 

 

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

function main() {

    var questionStyle = app.activeDocument.paragraphStyles.item('QuestionStyle');
    var answerStyleA = app.activeDocument.paragraphStyles.item('AnswersStyle_A');
    var answerStyleB = app.activeDocument.paragraphStyles.item('AnswersStyle_B');
    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 = answerStyleA;
            } else {
                myParagraph.appliedParagraphStyle = answerStyleB;
            }
            
            paragraphIndex++;
        }
    }

}