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

InDesign scripting: sections

Participant ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Hi all! Thank you in advance if you can help. I work with sections and have noticed such an absurdity.
There must be at least one page in InDesign document and it already belongs to the section.
I am trying to determine the range of pages belonging to a section by creating a method like this :

 

 

Section.prototype.getPageRange = function(){
     var sectionsArray = this.getElements();
     var ret;
     for (var i = 0; i < sectionsArray.length; i++){
          var curSection = sectionsArray[i],
              //0-based:
              sectionStartPageIndex = curSection.pageNumberStart - 1, 
              //also 0-based:
              sectionEndPageIndex = sectionStart + curSection.length - 2;  
              //alert('section length: ' + curSection.length);
              ret.push(
                   app.activeDocument.pages.itemByRange(
                   sectionStartPageIndex,
                   sectionEndPageIndex)
              );
     }
     return ret.length == 1 ? ret[0] : ret;
}

 

 

 

The method must return an array (or an array of arrays when processing a section collection) of pages belonging to the section.

 

 

//For one section - array of pages
var sectionPages = app.activeDocument.sections[0].getPageRange();

//For collection of sections - array of arrays of pages
var pagesOfEverySection = app.activeDocument.sections.everyItem().getPageRange();

 

 

 But sporadically, the length of the section (curSection.length) has zero value, which should not be, and this impossible zero breaks logic.

Stuck with this but want to clear things up.

TOPICS
Scripting

Views

282

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

Community Expert , Sep 12, 2021 Sep 12, 2021

You should probably define ret as an array -- i.e.

var ret = [];

 

Votes

Translate

Translate
Community Expert ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

You should probably define ret as an array -- i.e.

var ret = [];

 

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 ,
Sep 13, 2021 Sep 13, 2021

Copy link to clipboard

Copied

Peter Kahrel, thank you very much for your attention to this post.

Yes, indeed, I made a mistake by not initializing the ret variable, but this is a new error after the one described earlier. I wrote the code from my memory and I was inattentive... I noticed that in a loop when i = 0, the section length = 0 (always!) and this iteration should be skipped.

Also, if I skip the iteration when length = 0, all sections will be processed without loss!

 

Thus, the this.getElements() array has an excess element, whose length property is incorrect since  all pages within document must belong to some section, and the document must consist of at least one page.

 

 

Section.prototype.getPageRange = function(){
     var sectionsArray = this.getElements(),
     ret = [];
     for (var i = 0; i < sectionsArray.length; i++){
          var curSection = sectionsArray[i],
          //0-based:
          sectionStartPageIndex = curSection.pageNumberStart - 1,
          //also 0-based:
          sectionEndPageIndex = sectionStart + curSection.length - 2;
          //alert('section length: ' + curSection.length);


          //this  check was needed here
          if (curSection.length == 0) continue;


          ret.push(
               app.activeDocument.pages.itemByRange(
               sectionStartPageIndex,
               sectionEndPageIndex)
          );
     }
     return ret.length == 1 ? ret[0] : (ret.length > 1 ? ret : undefined);
}

 

 

 

 

 

 

 

 

 

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 ,
Sep 13, 2021 Sep 13, 2021

Copy link to clipboard

Copied

The problem is that for the first page of a section for some reason you look at the last page of the previous section:

sectionStartPageIndex = curSection.pageNumberStart - 1,

Why is that? It's easier to look at the documentOffset of a section's first page and determine the the section's last page from that. If pageNumberStart is set to a value that does not correspond with the page's documentOffset you always have a problem. Like this:

Section.prototype.getPageRange = function(){
  var sectionsArray = this.getElements();
	// Declare variables just once
  var curSection, sectionStartPageIndex, sectionEndPageIndex;
  var ret = [];
  for (var i = 0; i < sectionsArray.length; i++){
    curSection = sectionsArray[i];
    //0-based:
    sectionStartPageIndex = curSection.pageStart.documentOffset;
    //also 0-based:
    sectionEndPageIndex = sectionStartPageIndex + curSection.length - 1;
    //alert('section length: ' + curSection.length);
    ret.push(
      app.activeDocument.pages.itemByRange(
      sectionStartPageIndex,
      sectionEndPageIndex)
    );
  }
  return ret.length == 1 ? ret[0] : ret;
}

You won't have any of the problems you mentioned and the code can be simplified.

P.

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 ,
Sep 13, 2021 Sep 13, 2021

Copy link to clipboard

Copied

LATEST

Thank you very much for an exhaustive explanation!
Sometimes it is difficult to understand the purpose of certain properties and their behavior.

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