Skip to main content
Inspiring
May 30, 2025
Answered

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

  • May 30, 2025
  • 3 replies
  • 571 views

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.
Correct answer Robert at ID-Tasker

@Thomas_Calvin

 

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

 

3 replies

rob day
Community Expert
Community Expert
May 31, 2025

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?

 

 

 

rob day
Community Expert
Community Expert
May 31, 2025

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)

 

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
May 30, 2025

@Thomas_Calvin

 

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

 

Robert at ID-Tasker
Legend
May 30, 2025

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