Skip to main content
Participant
February 2, 2011
Answered

dynamic resizing of too wide columns (javascript, CS5)

  • February 2, 2011
  • 1 reply
  • 1342 views

Hey. I'm just getting started at scripting for InDesign (and am realizing how much time I've been wasting these last too many years). So far, I've managed to solve minor problems myself, but this one I just don't know enough to get round. I appreciate that what's needed for the solution is likely a quite large piece of code, so I'm grateful for any advice you can give, be it just hints or nudges in the right direction.

What I have to start with is this little piece of code which just applies a cell style to all tables in the selected text frame:

var myTextFrame = app.selection[0];

var myTables = myTextFrame.parentStory.tables.everyItem();
var myTableCells = myTextFrame.parentStory.tables.everyItem().cells.everyItem();

myTableCells.appliedCellStyle = "NameOfCellStyle";

What I need to do from here is to search for any of the tables in myTables that:

1) are wider than myTextFrame (that is, wider than the text frame they're in)

2) have exactly two columns

3) have a first column wider than the second column

In these tables, the first column needs to be resized so that the table fits exactly inside myTextFrame.

The best solution would be a dynamic resizing that isn't dependent on specific numbers, because the size of myTextFrame may vary, but right now I'll settle for anything, even if it means creating several scripts for different text frame widths.

So there. After way too many failed attempts, which largely spring from me having very little clue what I'm doing, I'm about to give up. Any help or hint is greatly appreciated.

Thanks

This topic has been closed for replies.
Correct answer _Jongware_-9BC6tI

Let's go over it one step at a time.

for (s=0; s<app.activeDocument.stories.length; s++)

     for (t=0; t<app.activeDocument.stories.tables.length; t++)

          check (app.activeDocument.stories.tables);

function check (aTable)

{

//     We have a table. So, test for

//     "1) are wider than myTextFrame (that is, wider than the text frame they're in)"

     var myTextFrame = aTable.cells[0].insertionPoints[-1].parentTextFrames[0];

     if (aTable.width < myTextFrame.geometricBounds[3] - myTextFrame.geometricBounds[1])

          return;

//     "2) have exactly two columns"

     if (aTable.columns.length != 2)

          return;

//     "3) have a first column wider than the second column"

     if (aTable.columns[0].width <= aTable.columns[1].width)

          return;

//     A-ha!

//     "The first column needs to be resized so that the table fits exactly inside myTextFrame."

     aTable.columns[0].width = (myTextFrame.geometricBounds[3] - myTextFrame.geometricBounds[1]) - aTable.columns[1].width;

}

If your tables have strokes at the outer edge, zoom in! The right hand side of resized tables are inside the text frame, but its stroke is outside. If that's a problem, you probably have to subtract the stroke width from the available width as well. (I usually don't have that problem, because mostly, my tables are unstroked.)

1 reply

_Jongware_-9BC6tICorrect answer
Inspiring
February 2, 2011

Let's go over it one step at a time.

for (s=0; s<app.activeDocument.stories.length; s++)

     for (t=0; t<app.activeDocument.stories.tables.length; t++)

          check (app.activeDocument.stories.tables);

function check (aTable)

{

//     We have a table. So, test for

//     "1) are wider than myTextFrame (that is, wider than the text frame they're in)"

     var myTextFrame = aTable.cells[0].insertionPoints[-1].parentTextFrames[0];

     if (aTable.width < myTextFrame.geometricBounds[3] - myTextFrame.geometricBounds[1])

          return;

//     "2) have exactly two columns"

     if (aTable.columns.length != 2)

          return;

//     "3) have a first column wider than the second column"

     if (aTable.columns[0].width <= aTable.columns[1].width)

          return;

//     A-ha!

//     "The first column needs to be resized so that the table fits exactly inside myTextFrame."

     aTable.columns[0].width = (myTextFrame.geometricBounds[3] - myTextFrame.geometricBounds[1]) - aTable.columns[1].width;

}

If your tables have strokes at the outer edge, zoom in! The right hand side of resized tables are inside the text frame, but its stroke is outside. If that's a problem, you probably have to subtract the stroke width from the available width as well. (I usually don't have that problem, because mostly, my tables are unstroked.)

Inspiring
February 2, 2011

Harbs, if you are listening in:

Why didn't this work? Shorter, sweeter, only one Undo ... overall much nicer. If it had only worked.

Table.prototype.checkMe = function () { check(this) };

app.activeDocument.stories.everyItem().tables.everyItem().checkMe();

(-- I got the idea from your "don't prototype native objects" discussion in the Illy forum, but hey, what the heck, Boundaries are for Pushing.)

(And it din't work, anyway.)

Harbs.
Legend
February 2, 2011

[Jongware] wrote:


(-- I got the idea from your "don't prototype native objects" discussion in the Illy forum, but hey, what the heck, Boundaries are for Pushing.)

(And it din't work, anyway.)

Kind of underscores my point don'tcha think?

Trying to prototype collection range item objects is really pushing it! You didn't really expect the javascript parser to run your function as an array did you?

Harbs