Illustrator Scripting: How to Accurately Position/Size Text Based on Glyph Bounds, Not Bounding Box?
I'm developing an Illustrator script (JSX) to automate text placement on various artboards. My goal is to position text precisely based on its actual visual size (glyph bounds), not the text frame's bounding box, which includes inconsistent padding.
My script is constantly misaligned because:
TextFrame.position and geometricBounds always reference the outer bounding box of the text frame.
If I change the text content (TextFrame.contents), the bounding box height changes, but the text is still misaligned relative to the bounding box's top edge.
I've tried calculating the required scale factor using a temporary TextFrame.createOutline() object to find the glyph height, and that works for scaling the font size. However, I still can't find a reliable way to calculate the precise (x,y) coordinates for the final TextFrame.position (which is the bottom-left corner) so that the visible top of the text aligns with a specific coordinate.
Is there a reliable way in Illustrator scripting to:
Get the Y offset from the top of the Text Frame Bounding Box to the top of the first line's visible glyph?
Or, is there a property that allows positioning based on the visual text content itself, similar to how the Area Type bounding box can be adjusted relative to the visible text?
Any guidance on accurately positioning the live text frame based on the scaled glyph size would be greatly appreciated!"
