Skip to main content
Inspiring
June 2, 2023
Answered

Script converting numbering to text and then applying paragraph styles is affecting prior paragraphs

  • June 2, 2023
  • 5 replies
  • 1820 views

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');
}
This topic has been closed for replies.
Correct answer Marc Autret

Hi @SweatersInSummer,

 

Two quick ideas:

 

1. Between convert() and readjust(), re-compute par.

    par = doc.stories.everyItem().paragraphs.everyItem().getElements();

Why? Because par[i].convertBulletsAndNumberingToText() probably changes text content, hence the internal pointers of the actual paragraphs. Keep in mind that a Paragraph object is internally encoded as a simple character range. When text changes, the old ranges may become invalid.

 

2. When dealing with an array of Text entities (paragraphs, etc.), prefer loops in reverse order:

    for( i=par.length ; i-- ; ){ /* etc */ }

Why? Basically for the same reason.

 

Not sure that solves the problem, but may help.

 

Best,

Marc

5 replies

Marc Autret
Marc AutretCorrect answer
Legend
June 3, 2023

Hi @SweatersInSummer,

 

Two quick ideas:

 

1. Between convert() and readjust(), re-compute par.

    par = doc.stories.everyItem().paragraphs.everyItem().getElements();

Why? Because par[i].convertBulletsAndNumberingToText() probably changes text content, hence the internal pointers of the actual paragraphs. Keep in mind that a Paragraph object is internally encoded as a simple character range. When text changes, the old ranges may become invalid.

 

2. When dealing with an array of Text entities (paragraphs, etc.), prefer loops in reverse order:

    for( i=par.length ; i-- ; ){ /* etc */ }

Why? Basically for the same reason.

 

Not sure that solves the problem, but may help.

 

Best,

Marc

Inspiring
June 5, 2023

this one did it, thanks for the help! ill need to learn to do reverse order loops more often.

Community Expert
June 7, 2023

This is cool

What's the full script - I could use this! 

Thanks

m1b
Community Expert
Community Expert
June 2, 2023

Actually at a quick glance, I notice you are declaring global variables for your loops. Change

for (i = 0; i < par.length; i++) {

to

for (var i = 0; i < par.length; i++) {

Add "var" in all your loops. This may be enough to fix your problem.

- Mark

Inspiring
June 5, 2023

sorry, i prematurally marked this answer as correct before forgetting i had the script set up to call the second function from a second file. adding var did not fix the problem by itself, but thank you!

m1b
Community Expert
Community Expert
June 6, 2023

No worries! Was just my first glance observation. 🙂

m1b
Community Expert
Community Expert
June 2, 2023

Hi @SweatersInSummer, can you post a demo document that we can run your script on that shows the incorrect behaviour. That will make it easiest for someone to troubleshoot.

- Mark

Inspiring
June 2, 2023

I can get this to work by using the doScript method to call the readjust() function from a seperate file, but i would prefer to have it all within one file just to be cleaner

James Gifford—NitroPress
Legend
June 2, 2023

Just to be sure you're aware of it, there are no tabs in HTML. You have to apply CSS styles to <ol> and <ul> lists to adjust number/bullet spacing. (Browsers apply default spacing, but it's only "good" for basic web pages, not styled ones or e-book pages.)

Inspiring
June 2, 2023

I'm aware, thats why i'm using en spaces.

James Gifford—NitroPress
Legend
June 2, 2023

Well, there's two ways to do lists in HTML. The fundamental ol/ul method can be controlled with great precision using CSS; the alternative, which has slightly greater 'scope' but is much more complex, is to build lists using multiple styles. That is, simply using spaces instead of tabs is not (in my view — and I do a lot of HTML and EPUB export/conversion) a useful approach in itself. You may want to rethink your style structure to solve this problem, potential others, and gain more control over the result.

 

ID supports both methods for export to EPUB, and I think to HTML as well.