Skip to main content
dublove
Legend
Nov 18, 2026 11:18 AM UTC
Answered

How to make 'delete page' continue when there are two connected text boxes on a page?

I want to keep the first text box of the current article and delete other text boxes, including the page.
There is a problem now, if there are two text boxes on a page, an error will be reported.

How to continue deleting pages?

//please select a textFrame
var item = d.selection[0]
var frames = item.parentStory.textContainers;
for (var i = frames.length - 1; i >= 1; i--) {
var page = frames[i].parentPage;
frames[i].remove();
try {
page.remove();
} catch (e) {
}
}

 

    Peter Kahrel
    Peter KahrelCorrect answer
    Community Expert
    Nov 18, 2026 2:31 PM UTC

    > I feel that frames [i]. parentPage refers to a cross page, not divided into left and right pages?

    No, that has nothing to do with it.

    The problem is that when you delete a page, all frames on the page are removed, which messes up the counter in the for-loop. That’s a general problem with deleting (or adding) objects in a for-loop.

     

    A more straightforward approach, which gets around that problem, is to remove frames and record which page they’re on, making sure that you record each frame just once. When all frames are removed, remove the recorded pages in a second loop.

    doc = app.activeDocument;
    frames = app.selection[0].parentStory.textContainers;
    offsets = [];
    known = [];
    for (i = frames.length-1; i >= 1; i--) {
    page = frames[i].parentPage;
    if (!known[page.documentOffset]) { // Avoid duplicates
    offsets.push (page.documentOffset);
    known[page.documentOffset] = true;
    }
    frames[i].remove();
    }

    for (i = 0; i < offsets.length-1; i++) {
    doc.pages[offsets[i]].remove();
    }

     

    15 replies

    rob day
    Community Expert
    Community Expert
    Nov 18, 2026 1:15 PM UTC

    Hi ​@dublove , Try changing

     (var i = frames.length - 1; i >= 1; i--) 

     

    To

     (var i = frames.length - 2; i >= 1; i--) 

     

    Translate
    dublove
    dubloveAuthor
    Legend
    Nov 18, 2026 1:42 PM UTC

    HI ​@rob day 

    It has nothing to do with this, right.
    It is because two text boxes share the same page,

    for example: reciprocal frame6 and frame7 share the same page. If frame7 is deleted, the corresponding page7 is also deleted, and page6 is no longer saved. It will make mistakes.

     

    Now there is a question:
    I feel that frames [i]. parentPage refers to a cross page, not divided into left and right pages?

    Translate
    Peter Kahrel
    Community Expert
    Peter KahrelCommunity ExpertCorrect answer
    Community Expert
    Nov 18, 2026 2:31 PM UTC

    > I feel that frames [i]. parentPage refers to a cross page, not divided into left and right pages?

    No, that has nothing to do with it.

    The problem is that when you delete a page, all frames on the page are removed, which messes up the counter in the for-loop. That’s a general problem with deleting (or adding) objects in a for-loop.

     

    A more straightforward approach, which gets around that problem, is to remove frames and record which page they’re on, making sure that you record each frame just once. When all frames are removed, remove the recorded pages in a second loop.

    doc = app.activeDocument;
    frames = app.selection[0].parentStory.textContainers;
    offsets = [];
    known = [];
    for (i = frames.length-1; i >= 1; i--) {
    page = frames[i].parentPage;
    if (!known[page.documentOffset]) { // Avoid duplicates
    offsets.push (page.documentOffset);
    known[page.documentOffset] = true;
    }
    frames[i].remove();
    }

    for (i = 0; i < offsets.length-1; i++) {
    doc.pages[offsets[i]].remove();
    }

     

    Translate