Table resize (script) - match column width but proportionally
Originally I have this script which works on an entire frame width
So if you have multiple text frames in columns it's fine - and resizes it proportionally enough for me.
// Check if a document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
// Check if a table is selected
if (app.selection.length > 0 && app.selection[0] instanceof Table) {
var table = app.selection[0];
var frame = table.parent;
// Get the width of the text frame
var frameWidth = frame.geometricBounds[3] - frame.geometricBounds[1];
// Consider text frame insets
var frameInset = frame.textFramePreferences.insetSpacing;
// Adjust frameWidth for insets
frameWidth -= frameInset[1] + frameInset[3];
// Get the number of columns
var numColumns = table.columns.length;
// Calculate the total width of all columns
var totalColumnWidth = 0;
for (var i = 0; i < numColumns; i++) {
totalColumnWidth += table.columns[i].width;
}
// Calculate the ratio to maintain the proportional relationship
var ratio = frameWidth / totalColumnWidth;
// Resize each column proportionally
for (var i = 0; i < numColumns; i++) {
table.columns[i].width *= ratio;
}
// Resize the table to the width of the text frame (considering insets)
table.width = frameWidth;
alert("Table resized to the width of the text frame while maintaining column proportions!");
} else {
alert("Please select a table before running the script.");
}
} else {
alert("Open a document before running the script.");
}
But now I am encountering situations where there is a single text frame set to columns.
But I can't seem to get the table cells to be proportional (I don't want them all the same width)
---THIS IS THE SCRIPT I'M WORKING ON ----
// Check if a document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
// Check if a table is selected
if (app.selection.length > 0 && app.selection[0] instanceof Table) {
var table = app.selection[0];
var frame = table.parent;
// Get the selected text frame
var myTextFrame = frame;
// Get the number of columns and gutter width
var myColAmount = myTextFrame.textFramePreferences.textColumnCount;
var myGutterWidth = myTextFrame.textFramePreferences.textColumnGutter;
// Calculate the total width of the text frame and available width for columns
var myTextFrameBounds = myTextFrame.geometricBounds;
var myTotalWidth = myTextFrameBounds[3] - myTextFrameBounds[1];
var myAvailableWidth = myTotalWidth - (myGutterWidth * (myColAmount - 1));
// Calculate the width of each column
var myColumnWidth = myAvailableWidth / myColAmount;
// Set the table width to match the calculated column width
table.width = myColumnWidth;
alert("Table width set to match the column width of the text frame!");
} else {
alert("Please select a table before running the script.");
}
} else {
alert("Open a document before running the script.");
}

