Script converting numbering to text and then applying paragraph styles is affecting prior paragraphs
For context, I'm using in5 to convert to an HTML5 eBook, where the tab charaacter in numbered lists acts really strange, so i'm using en spaces after the number in a list.
I've written a script that converts all targeted paragraphs with specified bullet and numbering list paragraph styles to text, but since i'm using en spaces insted of tabs in the lists I need to apply a series of custom para styles so they align properly as if using tabs. Both main functions of the script work as intended when run as seperate scripts, but when run together, the lists are converted to text, then the new para styles are applied based on some regex, but the paragraph above the intended targets are having the style applied as well. to reiterate, if i seperate the convert() function and readjust() function into seperate instances it all works correctly. setTimeOut is not a thing in ExtendScript and the $.sleep function does not seem to make them work correctly. what am i doing wrong?
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
var doc = app.activeDocument;
par = doc.stories.everyItem().paragraphs.everyItem().getElements();
re = /(Bullet|Num List) lvl [123]/;
num = /^[0-9]{2}\./gm;
lvl2fr = /^(f|r|t)\./gm;
lvl2ijl = /^(i|j|l)\./gm;
lvl2m = /^m\./gm;
lvl2w = /^w\./gm;
lvl3i = /^i\./gm;
lvl3iii = /^(iii|iv|vi|ix|xi)\./gm;
lvl3vii = /^(vii|xii|xv|xx)\./gm;
lvl3viii = /^(viii|xiii|xiv|xvi|xix)\./gm;
lvl3xvii = /^xvii\./gm;
lvl3xviii = /^xviii\./gm;
listGroup = app.activeDocument.paragraphStyleGroups.itemByName("Body Styles").paragraphStyleGroups.itemByName("Lists").paragraphStyleGroups.itemByName("Converted List MultiDigit");
twoDigiStyle = listGroup.paragraphStyles.itemByName("Num lvl 1 - 2 digit");
lvl2frStyle = listGroup.paragraphStyles.itemByName("Num lvl 2 - f,r");
lvl2ijlStyle = listGroup.paragraphStyles.itemByName("Num lvl 2 - i, j, l");
lvl2mStyle = listGroup.paragraphStyles.itemByName("Num lvl 2 - m");
lvl2wStyle = listGroup.paragraphStyles.itemByName("Num lvl 2 - w");
lvl3iStyle = listGroup.paragraphStyles.itemByName("Num lvl 3 - i");
lvl3iiiStyle = listGroup.paragraphStyles.itemByName("Num lvl 3 - iii, iv, vi, ix, xi");
lvl3viiStyle = listGroup.paragraphStyles.itemByName("Num lvl 3 - vii, xii, xv, xx");
lvl3viiiStyle = listGroup.paragraphStyles.itemByName("Num lvl 3 - viii, xiii, xiv, xvi, xix");
lvl3xviiStyle = listGroup.paragraphStyles.itemByName("Num lvl 3 - xvii");
lvl3xviiiStyle = listGroup.paragraphStyles.itemByName("Num lvl 3 - xviii");
convert();
readjust();
//Convert list items to text
function convert() {
for (i = 0; i < par.length; i++) {
if (re.test (par[i].appliedParagraphStyle.name)) {
par[i].convertBulletsAndNumberingToText();
}
}
alert('Bullet lvl 1, 2, 3 and Number List lvl 1, 2, 3 converted to text.');
}
//change paragraph style of two digit number list items
function readjust() {
for (i = 0; i < par.length; i++) {
if(num.test (par[i].contents)) {
par[i].appliedParagraphStyle = twoDigiStyle;
}
else if(lvl2fr.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl2frStyle;
}
else if(lvl2ijl.test (par[i].contents)){
if (par[i].leftIndent != 78){
par[i].appliedParagraphStyle = lvl3iStyle;
}
else {
par[i].appliedParagraphStyle = lvl2ijlStyle;
}
}
else if(lvl2m.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl2mStyle;
}
else if(lvl2w.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl2wStyle;
}
else if(lvl3iii.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl3iiiStyle;
}
else if(lvl3vii.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl3viiStyle;
}
else if(lvl3viii.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl3viiiStyle;
}
else if(lvl3xvii.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl3xviiStyle;
}
else if(lvl3xviii.test (par[i].contents)) {
par[i].appliedParagraphStyle = lvl3xviiiStyle;
}
}
alert('Converted List Paragraph styles applied');
}