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

Can't delete all section

Engaged ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

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();

TOPICS
Scripting

Views

1.1K

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 , Jan 09, 2017 Jan 09, 2017

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

P.

Votes

Translate

Translate
Community Expert ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

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

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
LEGEND ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

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();

(^/)

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 ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

Well, Michel, they have the same result so in that sense they're equivalent. The approaches are slightly different. So it depends on what you understand by 'equivalent'.

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
LEGEND ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

Peter,

Can I use them indifferently? … or could one of them be risky to be used in certain situations?

(^/) 

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 ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

I think they're equally safe. There may be performance differences, I don't know. Probably depends on the size of the document, the number of sections, the number of pages per section, etc. You could do a comparison of the three methods, on a smallish document and on a big one and compare the results.

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
Guru ,
Jan 10, 2017 Jan 10, 2017

Copy link to clipboard

Copied

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

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
Engaged ,
Jan 10, 2017 Jan 10, 2017

Copy link to clipboard

Copied

LATEST

Thank you all for your help and useful comments!

I made this function which does what I needed.

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 ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

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.

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 ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

> 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!

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