• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Participant ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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');
}
TOPICS
Scripting

Views

444

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 1 Correct answer

Guide , Jun 02, 2023 Jun 02, 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 (paragrap

...

Votes

Translate

Translate
Community Expert ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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.)


╟ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) ╢

Votes

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
Participant ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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

Votes

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
Community Expert ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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.


╟ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) ╢

Votes

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
Participant ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

Sorry James, but i think this a bit off base for what my request is. I am using the in5 plugin to convert to HTML 5 as it has a lot more control and options over what is produced compared to InDesign's native export for HTML. However lists, especially multi-tiered lists and ones where overrides are applied necessatates the need to convert the bullets and numbering to text in order for the export to render as it appears in the indesign file. 

Votes

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
Community Expert ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

I just noticed the tiny little detail that you're using In5. All I can add is that In5 is capable of doing an exceptional job of HTML export if things are set up for it correctly; I suggest that trying to jank the list styles with nonstandard layout may not be a good method to get where you're going. And if the plugin isn't doing what you need, or is causing other faults, part of its rather high cost is exceptional support. 🙂


╟ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) ╢

Votes

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
Participant ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

Trust me when i say i've explored every other option in regards to list options and in5 exports. this is my result of months of testing and back and forth on the support forums (i've got dozens of questions on their support forums at this point haha) and converting the lists to texts was their recommended option to maintain formatting when using live text.

Votes

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
Community Expert ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

I'll take your word for it, then. But also note that, with considerable experience in this specific area, I can't really make sense of the problem or your methods towards a solution. All due respect and politeness, you may have gone down a rabbit hole on this one and may need to start over from a simpler approach (that leverages In5's considerable power and support features). Put another way, the real causes of the problem may be outside this small scope.


╟ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) ╢

Votes

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
Participant ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

I agree that this is definetely a bizarre solution to a niche problem. ill keep looking into other methods in the future to better optimize my lists, but the problem of appliedParagraphsStyles affecting the paragraphs above the intended target when withing the same script file but not when in seperate files still remains.

Votes

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
Community Expert ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

As the problems lie entirely with In5's processes, and you haven't been able to get a satisfactory answer from that side, I am not sure one will be found here. There are some very advanced users (and mavens) here, but I'd wager they're already on In5 community resources. I am not sure even very advanced ID knowledge alone will add much.

 

But good luck. Been there, done this, have the forehead scars to prove it. 🙂


╟ Word & InDesign to Kindle & EPUB: a Guide to Pro Results (Amazon) ╢

Votes

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
Participant ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

Just in case it wasn't clear, the problem is entirely an InDesign scripting problem. The in5 part was just context to try to explain why i'm doing this bizarre thing with numbered lists. 

Votes

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
Participant ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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

Votes

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
Community Expert ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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

Votes

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
Community Expert ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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

Votes

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
Participant ,
Jun 05, 2023 Jun 05, 2023

Copy link to clipboard

Copied

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!

Votes

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
Community Expert ,
Jun 06, 2023 Jun 06, 2023

Copy link to clipboard

Copied

No worries! Was just my first glance observation. 🙂

Votes

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
Guide ,
Jun 02, 2023 Jun 02, 2023

Copy link to clipboard

Copied

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

Votes

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
Participant ,
Jun 05, 2023 Jun 05, 2023

Copy link to clipboard

Copied

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

Votes

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
Community Expert ,
Jun 06, 2023 Jun 06, 2023

Copy link to clipboard

Copied

This is cool

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

Thanks

Votes

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
Participant ,
Jun 07, 2023 Jun 07, 2023

Copy link to clipboard

Copied

LATEST

that is the full script in the post, but switching out the forward looping with the reverse looping suggested by Marc

Votes

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