I've got a couple million little tables in a linguistics book, where two
different-language sentences are compared one above the other. Each word
in the top sentence has to align with its counterpart in the bottom
sentence. The author did this with lots of little tables, each word in
one cell.
This works okay, except that the manuscript was prepared in 12 pt type
and the book has to be done in 10 pt type, so every column in every
table has to be resized smaller. Framemaker has a "resize column to fit
text" command. It would be great if I could just swipe the table and
make it resize all the columns to fit. Does ID have something similar
buried somewhere, or is there a plugin or script somewhere that does this?
--
Kenneth Benson
Pegasus Type, Inc.
www.pegtype.com
Peter
Peter
#target indesign;
snap_all_columns (get_table(), 3)
function snap_all_columns (tbl, space)
{
for( var i = 0; i < tbl.columns.length; i++ )
{
tbl.columns[i].width = '5cm'
var longest = 0;
var rightmargin = tbl.columns[i].cells.everyItem ().insertionPoints[-1].horizontalOffset;
for ( j = 0; j < rightmargin.length ; j++ )
longest = Math.max (rightmargin[j], longest);
var lm = tbl.columns[i].cells[0].insertionPoints[0].horizontalOffset
tbl.columns[i].width = ((longest - lm) + space)
}
}
function get_table()
{
if (app.selection.length > 0)
if (app.selection[0].parent instanceof Table)
return app.selection[0].parent;
else if (app.selection[0].parent.parent instanceof Table)
return app.selection[0].parent.parent;
alert ('Cursor not in a table\ror illegal selection.');
exit ();
}
That looks pretty cool. Would it be difficult to make this traverse the document looking for tables? Ken said he had "a couple million" of them to do. :)
Peter S.
#target indesign;
tables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
for (i = 0; i < tables.length; i++ )
snap_all_columns (tables[i], 3)
function snap_all_columns( tbl, space )
{
for( var i = 0; i < tbl.columns.length; i++ )
{
tbl.columns[i].width = '5cm'
var longest = 0;
rightmargin = tbl.columns[i].cells.everyItem ().insertionPoints[-1].horizontalOffset;
for ( var j = 0; j < rightmargin.length ; j++ )
longest = Math.max (rightmargin[j], longest);
var lm = tbl.columns[i].cells[0].insertionPoints[0].horizontalOffset
tbl.columns[i].width = ((longest - lm) + space)
}
}
Peter
In that case, under the last line in the script add a new line, so that it looks as follows:
tbl.columns[i].width = ((longest - lm) + space);
tbl.columns[0].width = 24
}
}
Why it should set the first columns to 363 points I don't know, doesn't happen to me. Anyway, by adding that new line the problem is irrelevant.
Peter
It does something funky with the first column (sets it to 363 pts),
which usually has just a number in parens like (12), but that doesn't
really affect things, because I have to consistently size that column at
24 pts regardless of the contents.
Very nice.
This produces an error about too many braces, so I took the last one
out, and it works, but it doesn't resize the first column to 24 pts.
The "last line" that I should place this after, that's the exit line?
Peter
#target indesign;
snap_all_columns (get_table(), 3)
function snap_all_columns (tbl, space)
{
for( var i = 0; i < tbl.columns.length; i++ )
{
tbl.columns[i].width = '5cm'
var longest = 0;
var rightmargin = tbl.columns[i].cells.everyItem ().insertionPoints[-1].horizontalOffset;
for ( j = 0; j < rightmargin.length ; j++ )
longest = Math.max (rightmargin[j], longest);
var lm = tbl.columns[i].cells[0].insertionPoints[0].horizontalOffset;
tbl.columns[i].width = ((longest - lm) + space);
tbl.columns[0].width = 24;
Sounds like you know this work.
Since these are tables, and since the tables came from Word, I need a
way to fix the insets. I'm using cell styles (to fix the insets) and a
table style (to adjust space above and below to keep the whole thing on
grid). If I converted all the tables to text and then back to tables, I
could avoid the cell and table styles, but it seems like just as much work.
I'm also unmerging cells, deleting extra columns, and moving the parts
of the sentence that run off the page onto 2nd lines.
Sounds like you know this work.
Yep, I do a lot of linguistic setting.
Since these are tables, and since the tables came from Word, I need a
way to fix the insets. I'm using cell styles (to fix the insets) and a
table style (to adjust space above and below to keep the whole thing on
grid).
It's probably a matter of taste, but as the format of all those tables isn't going to change, instead of using all those styles I find it easier to clean up the table and apply just a paragraph style to all cells. This is the kind of script I use for that:
#target indesign
t = get_table();
clean_table (t);
function clean_table (tbl)
{
var pStyles = app.activeDocument.paragraphStyles;
if( pStyles.item ('example') != null)
tbl.cells.everyItem().texts[0].applyParagraphStyle (
pStyles.item ('example'), false);
with (tbl.cells.everyItem ())
{
//delete all rules
topEdgeStrokeWeight = 0;
bottomEdgeStrokeWeight = 0;
leftEdgeStrokeWeight = 0;
rightEdgeStrokeWeight = 0;
//set insets to zero
topInset = 0;
rightInset = 0;
bottomInset = 0;
leftInset = 0;
}
tbl.rows.everyItem().firstBaselineOffset = FirstBaseline.leadingOffset
tbl.spaceBefore = 0;
tbl.spaceAfter = 0;
}
function get_table()
{
if (app.selection.length > 0)
if (app.selection[0].parent instanceof Table)
return app.selection[0].parent;
else if (app.selection[0].parent.parent instanceof Table)
return app.selection[0].parent.parent;
alert ('Cursor not in a table\ror illegal selection.');
exit ();
}
The script applies the paragraph style 'example' to all cells if that style is present, sets all insets to zero, removes all rules, sets the baseline offset of each cell to leading (to keep them on the grid), and sets the space before and after the table.
I'm also unmerging cells, deleting extra columns, and moving the parts
of the sentence that run off the page onto 2nd lines.
There may not be much to script here as there is probably not much system in the way the cells have been merged.
Peter
That's exactly what I'm doing when I apply a cell style and a table
style. Another thing I'm doing is removing row height settings.
This doesn't seem to be true for me. I changed the table style so that
it's based on No Table Style, and I changed the cell style so that it's
based on None, just in case that was the problem. When I apply (or
ALT-apply) the table style, it doesn't change the cell style. And when I
run your final script (which applies a table style) it also does not
change the cell style (or the paragraph style).
This is why I've been doing it in 4 steps:
1) Select all in table and apply cell style
2) Apply paragraph style
3) Apply table style
4) Set row height to At Least 3 pts.
The order is important. If cell style is set for None, it applies No
Paragraph Style (so I follow this by ALT-applying the correct paragraph
style).
Including the paragraph style in the cell style means that steps 1 and 2
could be just one step, but if I use a different paragraph style, then
the cell that contains that different paragraph will show an exception
to the style. In addition, applying the paragraph style through the cell
style doesn't remove paragraph style overrides. Maybe it would through
the script; I can't tell.
I changed the table style so that it's based on No Table Style, and I
changed the cell style so that it's based on None, just in case that was
the problem. When I apply (or ALT-apply) the table style, it doesn't change
the cell style.
But it can work only if the cell style is specified at Body Rows in the table style panel. Though some fooling around shows that it works only well if you define a table and cell style with their embedded styles and then leave them alone. When you change embedded styles it all becomes much less straightforward, which lands you in the situation that you now appear to be in.
But anyway, the four steps you mentioned can be added to the script. Just replace the beginning with these lines:
#target indesign;
tbl = get_table();
tbl.cells.everyItem().texts.everyItem().applyParagraphStyle(
app.activeDocument.paragraphStyles.item ('example_par'),false);
tbl.appliedTableStyle = app.activeDocument.tableStyles.item ('example');
tbl.rows.everyItem().autoGrow = true;
tbl.rows.everyItem().height = 3;
snap_all_columns (tbl, 3);
The rest stays the same. (Your first step, assigning cell styles, can't be scripted as it is destructive. If that's no problem, then a line can be added to do that, too.)
Peter
something that removed overrides would be highly welcome
In that case, use this for the beginning of the script (again, the rest remains the same).
Peter
#target indesign;
tbl = get_table();
tbl.cells.everyItem().appliedCellStyle = 'example';
tbl.cells.everyItem().texts.everyItem().appliedParagraphStyle = 'example';