Skip to main content
dublove
Legend
October 17, 2025
Answered

How to determine the number of columns in a text box and obtain the width of one column?

  • October 17, 2025
  • 2 replies
  • 254 views

I want to get the width of the parent(textFrame) of the current table.
If the text box has only one column, the width is the entire text box width.
If the text box has multiple columns, I want to get the width of one column.

 

Suppose I have now selected the table and want to obtain myWidth.

 

Correct answer m1b

Hi @dublove try this:

/**
 * @file Get Column Width of Table.js
 * 
 * Example showing how to get a table from the selection,
 * and then the parent text frame's column width.
 * 
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-determine-the-number-of-columns-in-a-text-box-and-obtain-the-width-of-one-column/m-p/15550852
 */
function main() {

    var doc = app.activeDocument;
    var table = getParent(doc.selection[0], 'Table');

    if (!table)
        return alert('Please put cursor into a table and try again.');

    var textFrame = table.storyOffset.parentTextFrames[0];
    var columnCount = textFrame.textFramePreferences.textColumnCount;
    var columnGutter = textFrame.textFramePreferences.textColumnGutter;
    var b = textFrame.geometricBounds;
    var frameWidth = b[3] - b[1];
    var inset = textFrame.textFramePreferences.insetSpacing;
    var columnWidth = (frameWidth - inset[1] - inset[3] - (columnGutter * (columnCount - 1))) / columnCount;

    alert('Column width = ' + columnWidth);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Get Column Width of Table');

/**
 * Return an item with the matching constructor name, if a parent of `item`.
 * @author m1b
 * @version 2025-10-18
 * @param {PageItem|*} item - an Indesign DOM item.
 * @param {String} constructorName - the constructor name to match.
 * @returns {*?}
 */
function getParent(item, constructorName) {

    try {

        while (item.hasOwnProperty('parent')) {

            if (constructorName === item.constructor.name)
                return item;

            item = item.parent;

        }

    } catch (error) { }

};

2 replies

Dave Creamer of IDEAS
Community Expert
Community Expert
October 17, 2025

If you just want some quick info...

For the column width, use the Text Frame Options:

 

For a table column width, use the Cell Options (select column, fully or partially):

 

David Creamer: Community Expert (ACI and ACE 1995-2023)
dublove
dubloveAuthor
Legend
October 18, 2025

Hi Dave Creamer of IDEAS.

I want to use scripts to control

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
October 17, 2025

Hi @dublove try this:

/**
 * @file Get Column Width of Table.js
 * 
 * Example showing how to get a table from the selection,
 * and then the parent text frame's column width.
 * 
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-determine-the-number-of-columns-in-a-text-box-and-obtain-the-width-of-one-column/m-p/15550852
 */
function main() {

    var doc = app.activeDocument;
    var table = getParent(doc.selection[0], 'Table');

    if (!table)
        return alert('Please put cursor into a table and try again.');

    var textFrame = table.storyOffset.parentTextFrames[0];
    var columnCount = textFrame.textFramePreferences.textColumnCount;
    var columnGutter = textFrame.textFramePreferences.textColumnGutter;
    var b = textFrame.geometricBounds;
    var frameWidth = b[3] - b[1];
    var inset = textFrame.textFramePreferences.insetSpacing;
    var columnWidth = (frameWidth - inset[1] - inset[3] - (columnGutter * (columnCount - 1))) / columnCount;

    alert('Column width = ' + columnWidth);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Get Column Width of Table');

/**
 * Return an item with the matching constructor name, if a parent of `item`.
 * @author m1b
 * @version 2025-10-18
 * @param {PageItem|*} item - an Indesign DOM item.
 * @param {String} constructorName - the constructor name to match.
 * @returns {*?}
 */
function getParent(item, constructorName) {

    try {

        while (item.hasOwnProperty('parent')) {

            if (constructorName === item.constructor.name)
                return item;

            item = item.parent;

        }

    } catch (error) { }

};
dublove
dubloveAuthor
Legend
October 17, 2025

Hi @m1b 

Seems like there's an issue—did it get stuck in an infinite loop?
I tried three times with no results. After five seconds, the ID software shut down.