Skip to main content
Inspiring
May 30, 2025 10:00 PM UTC
Answered

How do you find the table closest to the insertion point?

All the examples I can find show how to find a table when you're already in one. But I need to be able to find the next table before or after the insertion point, even if it's sitting in regular text.

 

Presumably I need to find the absolute index of the start (and end, maybe) of the table in the story and compare it to that of the insertion point, but something like the following doesn't seem to give an index in the entire story:

 

    var tables = story.tables;
    var nextTable;
    for (currTable = 0; currTable < tables.length; currTable++)
    {
        alert("currTable index: " + tables[currTable].cells[0].insertionPoints[0].index);
        if (tables[currTable].cells[0].insertionPoints[0].index > app.selection[0].index)
        {
            nextTable = tables[currTable];
            alert("Table found.");
        }
    }
 
The index returned is 0 in all cases.
 
BTW, the "code sample" formatting button here doesn't do anything.
Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
May 30, 2025 10:07 PM UTC

@Thomas_Calvin

 

You need to check Tablet's StoryOffset to get it's InsertionPoint.

 

6 replies

rob day
Community Expert
Community Expert
May 31, 2025 1:52 PM UTC

BTW, the "code sample" formatting button here doesn't do anything.

 

Just to clarify, when you click the Code icon (with no text selected) you don’t get an Insert/Edit code dialog?

 

 

 

Translate
rob day
Community Expert
Community Expert
May 31, 2025 1:42 PM UTC

Hi @Thomas_Calvin , Are you looking for the table that is closest to the current selection in space or by index? This selection’s index is closer to Table 1 but its XY postion is closer to Table 2

 

 

This gets a selected text’s X,Y position

 

var ip = app.activeDocument.selection[0].insertionPoints[0]
alert("Selection Position\rX: " + ip.horizontalOffset + "\rY: " + ip.baseline)

 

Translate
Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
May 30, 2025 10:07 PM UTC

@Thomas_Calvin

 

You need to check Tablet's StoryOffset to get it's InsertionPoint.

 

Translate
Robert at ID-Tasker
Legend
May 30, 2025 10:08 PM UTC

Should've been Table's - not Tablet's. 

 

Translate