Skip to main content
Inspiring
January 9, 2017
Answered

Can't delete all section

  • January 9, 2017
  • 2 replies
  • 1489 views

Hi guys,

Why does ID not allow you to delete all sections.

for ( i = allSections.length; i > 0; i-- ) {

    allSections.remove();

    }

This says undefined is not an object but I have 2 sections in my document.

allSections = doc.sections.everyItem().getElements();

This topic has been closed for replies.
Correct answer Peter Kahrel

A document must have at least one section with at least one page.

P.

2 replies

Jongware
Community Expert
Community Expert
January 9, 2017

While Peter is right (naturally), it's not the reason why you get that error.

If you have 2 sections, the count is '2' but since counting starts at 0, their indexes are 0 and 1.

You try to remove a section with index 2, which does not exist. So you get that error.

Peter Kahrel
Community Expert
Community Expert
January 9, 2017

> You try to remove a section with index 2, which does not exist. So you get that error.

I read the first sentence and thought that I had seen enough. Clearly not!

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
January 9, 2017

A document must have at least one section with at least one page.

P.

Obi-wan Kenobi
Legend
January 9, 2017

Hi,

Are these 3 codes equivalent?

Thanks in advance!

app.activeDocument.sections.itemByRange(1, app.activeDocument.sections.length-1).remove();

allSections = app.activeDocument.sections.everyItem().getElements();

S = allSections.length;

for ( var s = 1 ; s < S ; s++)  allSections.remove();

allSections = app.activeDocument.sections.everyItem().getElements();

for ( s = allSections.length-1 ; s > 0 ; s-- )  allSections.remove();

(^/)

Inspiring
January 10, 2017

app.activeDocument.sections.itemByRange(1, app.activeDocument.sections.length-1).remove();

will throw an error if there's only one section.

ItemByRange is generally (considerably) quicker


Thank you all for your help and useful comments!

I made this function which does what I needed.