Skip to main content
mikefwd
Known Participant
November 9, 2018
Question

Delete empty graphics frames inside table

  • November 9, 2018
  • 1 reply
  • 1311 views

Hi all

I need a script to delete all empty graphics frames inside all tables of a multi page document.

The following script deletes text frames in the document but does not delete them if they are inside a table.

var myGraphicFrames = app.activeDocument.rectangles; 

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

    if (myGraphicFrames.graphics.length < 1) 

        myGraphicFrames.remove(); 

}

Anyone know how to rewrite this script so that it deletes graphic frames from within all tables as well?

I'm a total beginner at scripting so any help would be much appreciated.

Cheers

Mike

This topic has been closed for replies.

1 reply

Community Expert
November 9, 2018

Something like below should work, you need to iterate all the tables in the document and then each cell of all these tables

var myGraphicFrames = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().pageItems

for (i = 0; i < myGraphicFrames.length; i++)

{

    if (myGraphicFrames.graphics.length == 0)

        myGraphicFrames.remove();

}

-Manan

-Manan
Community Expert
November 9, 2018

I just noticed that the code i gave would not work on inner tables i.e. tables within a table cell and so forth. But that can be added to it, you just need to check each cell to check for tables within it and then the process repeats itself to iterate each cell. A recursive algorithm fits the bill in this case.

-Manan

-Manan
Peter Kahrel
Community Expert
Community Expert
November 9, 2018

Ah. Found a solution and changed the for loop where I remove the object to that:

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

{

    // Test for allGraphics array of individual object:

    if( resultsArray.allGraphics.length > 0 ){ continue };

    try{ resultsArray.remove() }catch(e){ /* $.writeln( n +"\t"+ e.message ) */};

};

Regards,
Uwe


Interesting, different approaches.

To find empty graphic frames of any embedded depth, keep testing its parent's constructor until it's Cell or Document. If it's Document the page item is not in a cell. Pasted graphics aren't deleted, by the way.

function inCell (frame) {

  var p = frame.parent;

  while (!(p instanceof Document || p instanceof Cell)) {

    p = p.parent;

  }

  return p instanceof Cell;

}

r = app.documents[0].allPageItems;

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

  if (r.hasOwnProperty('graphics') && inCell (r) && r.graphics.length == 0) {

    r.remove();

  }

}

Because of JavaScript's short-circuit evaluation, you can speed up the script (any script) by rearranging the three coordinated tests. (Short-circuit evaluation means that if the first test fails, following tests aren't done.) If very few page items are in cells, then it makes sense first to test whether any page item is in a cell: if a frame is not in a cell, the two other tests are done. And if very few frames are empty, then first test whether any frame has a graphic.

P.