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;
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, "Se
...
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
...
Copy link to clipboard
Copied
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;
}
}
}
}
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.
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++;
}
}
}
Copy link to clipboard
Copied
Thanks a lot, I'm very gratful , yes this suits my needs, Thanks again
Copy link to clipboard
Copied
You're welcome!