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

Enthusiast ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

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;

 

 

 

Apply PS Style then Next Style Automatically.jpg

Best
M.Hasanain
TOPICS
Scripting

Views

92

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Enthusiast , Jul 19, 2021 Jul 19, 2021
Hi @M.Hasanain 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, "S...

Likes

Translate

Translate
Enthusiast , Jul 19, 2021 Jul 19, 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('AnswersStyl...

Likes

Translate

Translate
Enthusiast ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Hi @M.Hasanain 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;
            }
        }
    }
}

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

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.

Best
M.Hasanain

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

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

}

 

 

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Thanks a lot, I'm very gratful , yes this suits my needs, Thanks again

Best
M.Hasanain

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

LATEST

You're welcome!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines