Skip to main content
Inspiring
March 17, 2016
Answered

Deleting all hidden text frames unexpectedly deletes anchored (but visible) text frames. Why?

  • March 17, 2016
  • 3 replies
  • 484 views

I'm trying to use the code below to delete all hidden text frames from a document. It's works pretty well BUT for some reason, deletes visible anchored text frames.

(the currentTextFrame.length argument is to prevent it deleting hidden text frames if the content is threaded, in case the text then reflows into visible text frames. I'll need to work out a solution to that, but it's tomorrows problem.)

I have a similar function for allGraphics and that does not delete anchored frames.

Can someone point me in the right direction? I can add a check to see if the text frame is anchored, but I'm concerned I might miss the bigger problem.

var myDocument = app.activeDocument;

var myTextFrames = myDocument.stories.everyItem().textContainers.reverse();

var myTextFramesLength = myTextFrames.length;

for ( i = 0; i < myTextFramesLength; i++ ){

     var currentTextFrame = myTextFrames;

     if ((!currentTextFrame.visable) && (currentTextFrame.length ===1)){

          currentTextFrame[0].remove();

     }

}

Edit: This is happening in CC 2014.0 and 2015.1, OSX 10.10

Message was edited by: Naomi Kennedy

This topic has been closed for replies.
Correct answer Laubender

Hi Naomi,

and there is an array of arrays with:

myDocument.stories.everyItem().textContainers

Test this:

var a = app.documents[0].stories.everyItem().textContainers;

for(var n=0;n<a.length;n++)

{

    $.writeln(a.constructor.name);

}

I would suggest the following, that should work:

var doc = app.documents[0];

var myStoriesArray = doc.stories.everyItem().getElements();

var storiesLength = myStoriesArray.length;

var textContainersToRemoveIDs = [];

for(var n=0;n<storiesLength;n++)

{

   

    // A counter:

    var removeAllContainersOfStory = 0;

    for(var c=0;c<myStoriesArray.textContainers.length;c++)

    {

        if(!myStoriesArray.textContainers.visible)

        {

            removeAllContainersOfStory++

        };

       

    }

   

    if(removeAllContainersOfStory == myStoriesArray.textContainers.length)

    for(var c=0;c<myStoriesArray.textContainers.length;c++)

    {

        textContainersToRemoveIDs[textContainersToRemoveIDs.length++] = myStoriesArray.textContainers.id;

    }

   

};

// You can test the waters here:

for(var n=0;n<textContainersToRemoveIDs.length;n++)

{

    doc.pageItems.itemByID(textContainersToRemoveIDs).remove();

}

Uwe

3 replies

Inspiring
March 17, 2016

Looks like this combo of both replies works to solve the problem (visible typo AND currrentTextFrame[0] -- although I should probably update my variable name to currentStory for clarity).

if ((!currentTextFrame[0].visible) && (currentTextFrame.length ===1)){

However in practice, getting all the ids then deleting them could avoid many other potential problems, so I'll probably go ahead and update my code so it's structured like that.

Thanks for your help!

LaubenderCommunity ExpertCorrect answer
Community Expert
March 17, 2016

Hi Naomi,

and there is an array of arrays with:

myDocument.stories.everyItem().textContainers

Test this:

var a = app.documents[0].stories.everyItem().textContainers;

for(var n=0;n<a.length;n++)

{

    $.writeln(a.constructor.name);

}

I would suggest the following, that should work:

var doc = app.documents[0];

var myStoriesArray = doc.stories.everyItem().getElements();

var storiesLength = myStoriesArray.length;

var textContainersToRemoveIDs = [];

for(var n=0;n<storiesLength;n++)

{

   

    // A counter:

    var removeAllContainersOfStory = 0;

    for(var c=0;c<myStoriesArray.textContainers.length;c++)

    {

        if(!myStoriesArray.textContainers.visible)

        {

            removeAllContainersOfStory++

        };

       

    }

   

    if(removeAllContainersOfStory == myStoriesArray.textContainers.length)

    for(var c=0;c<myStoriesArray.textContainers.length;c++)

    {

        textContainersToRemoveIDs[textContainersToRemoveIDs.length++] = myStoriesArray.textContainers.id;

    }

   

};

// You can test the waters here:

for(var n=0;n<textContainersToRemoveIDs.length;n++)

{

    doc.pageItems.itemByID(textContainersToRemoveIDs).remove();

}

Uwe

Peter Kahrel
Community Expert
Community Expert
March 17, 2016

It's visible, not visable.

Peter