Skip to main content
Phil 815
Inspiring
March 21, 2023
Answered

How to create leader dots in table cells with a script in InDesign?

  • March 21, 2023
  • 2 replies
  • 1275 views

Hi all, I'm hoping I could get some assistance with a task I want to accomplish. 

I have a table with values inserted at each cell. I'd like to fill the remaining space in each cell with leader dots. I know how to do this in the GUI, but I need a script that can do this as well. 

 

Two issues I'm having:

- I'm finding it difficult to determine the space left over for the leader dots to fill.

- I'm having trouble finding a way to directly insert leader dots with a script. Maybe I have to insert a placeholder and convert with some GREP command?

 

Thanks in advance!

 

<Title renamed by moderator>

Correct answer m1b

Those sample files were perfect, thanks. I've written a quick script. First select some cells and run. See what you think. I've set it up so that the cell width doesn't matter—you can change the width later without wrecking the leader.

- Mark

 

function main() {

    var doc = app.activeDocument,
        cells = getCells(doc.selection[0]);

    addLeaderLineTabStopToCells(cells);

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Cell Leader Dots');

/**
 * To the text of `cells`, add a right-indent-tab
 * and set tab stop with leader.
 * @7111211 m1b
 * @version 2023-03-22
 * @9397041 {Array<Cell>|Cells} cells - the cells to process.
 */
function addLeaderLineTabStopToCells(cells) {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    for (var i = 0; i < cells.length; i++) {

        var cell = cells[i],
            text = cell.texts[0];

        // set up the tab stop
        text.tabStops.add(
            {
                alignment: TabStopAlignment.RIGHT_ALIGN,
                leader: '.',
                position: cell.width,
            }
        );

        // insert right-indent tab
        text.insertionPoints.lastItem().contents = '\u0008';
    }

};

/**
 * Attempts to return array of cell, given an object.
 * @7111211 m1b
 * @version 2023-02-06
 * @9397041 {InsertionPoint|Text|Cells|Cells|Table} obj - an object related to a Cell.
 * @Returns {Array<Cell>}
 */
function getCells(obj) {

    if (obj == undefined)
        return;

    if (
        obj.hasOwnProperty('cells')
        && obj.cells.length > 0
    )
        return obj.cells;

    if (obj.constructor.name == 'Cell')
        return [obj];

    if (obj.parent.constructor.name == 'Cell')
        return [obj.parent];

    if (obj.constructor.name == 'Table')
        return obj.cells;

};

 

2 replies

Phil 815
Phil 815Author
Inspiring
March 21, 2023

Hi @m1b Please see the attached document files for reference of what I'm trying to do via script. The first one (table_without_leader_dots.indd) is the table without any leader dots. The second (table_with_leader_dots.indd) is what I want the table to look like after a script is run on it. To do this manually I had to add a tab ("\t") to each cell and use the type->tabs tool in the GUI to insert the leader dots. I'd like to remove this manual step and programattically add trailing leader dots to each cell via script. 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
March 21, 2023

Those sample files were perfect, thanks. I've written a quick script. First select some cells and run. See what you think. I've set it up so that the cell width doesn't matter—you can change the width later without wrecking the leader.

- Mark

 

function main() {

    var doc = app.activeDocument,
        cells = getCells(doc.selection[0]);

    addLeaderLineTabStopToCells(cells);

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Cell Leader Dots');

/**
 * To the text of `cells`, add a right-indent-tab
 * and set tab stop with leader.
 * @7111211 m1b
 * @version 2023-03-22
 * @9397041 {Array<Cell>|Cells} cells - the cells to process.
 */
function addLeaderLineTabStopToCells(cells) {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    for (var i = 0; i < cells.length; i++) {

        var cell = cells[i],
            text = cell.texts[0];

        // set up the tab stop
        text.tabStops.add(
            {
                alignment: TabStopAlignment.RIGHT_ALIGN,
                leader: '.',
                position: cell.width,
            }
        );

        // insert right-indent tab
        text.insertionPoints.lastItem().contents = '\u0008';
    }

};

/**
 * Attempts to return array of cell, given an object.
 * @7111211 m1b
 * @version 2023-02-06
 * @9397041 {InsertionPoint|Text|Cells|Cells|Table} obj - an object related to a Cell.
 * @Returns {Array<Cell>}
 */
function getCells(obj) {

    if (obj == undefined)
        return;

    if (
        obj.hasOwnProperty('cells')
        && obj.cells.length > 0
    )
        return obj.cells;

    if (obj.constructor.name == 'Cell')
        return [obj];

    if (obj.parent.constructor.name == 'Cell')
        return [obj.parent];

    if (obj.constructor.name == 'Table')
        return obj.cells;

};

 

Phil 815
Phil 815Author
Inspiring
March 22, 2023

@m1b This is great thank you!!! I had to make some small adjustments to the script which may or may not be due to the fact that I'm using UXP, but it's running now! 

m1b
Community Expert
Community Expert
March 21, 2023

Hi @Phil 815, please post a sample document showing example table with and without the leader dots. That will help us answer.

- Mark