Table's Position & Scripting
Hi,
I've got a table within a text frame.
I'd like to my script to figure out the table's position (its x & y coordinates).
How do I do that?
Thanks!
Hi,
I've got a table within a text frame.
I'd like to my script to figure out the table's position (its x & y coordinates).
How do I do that?
Thanks!
Hi @m1b , I’m not sure if this would always work, but a simpler approach might be to work off of the insertion point before the table (the table’s .storyOffset property). Its baseline minus the table height minus the top border stroke width would get Y, and its horizontalOffset plus 1 point would get X. With some limited testing this seems to work:
var et = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
var pos = getTableXY(et[0])
alert("X: " + pos[0] + " Y: " + pos[1])
/**
* Get a table’s X,Y position
* t.storyOffset is the insertion point before the table
* get its baseline - the table h - the top border for Y
* the insertion point’s horizontal position gets X
*
* @ param the table
* @ return x,y as an array
*
*/
function getTableXY(t){
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var tr = t.rows[0].cells.everyItem().getElements()
var ti = t.topBorderStrokeWeight;
for (var i = 0; i < tr.length; i++){
if (tr[i].topEdgeStrokeWeight > ti) {
ti = tr[i].topEdgeStrokeWeight
}
};
var ty = t.storyOffset.baseline - t.height - ti;
var tx = t.storyOffset.horizontalOffset+1
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
return [tx,ty]
}
Margin guides at 1". The table is pushed to the right of the text frame via a 1" Left Indent, and pushed down via Space After in the paragraph above:

EDIT had X,Y reversed—the returned array is [X,Y]
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.