Text frames already have an Auto-Sizing feature for this, so I’d probably use that rather than trying to reproduce the double-click behaviour with fit().
It’s under Object > Text Frame Options > Auto-Size. You can set it to Height Only, Width Only, or Height and Width, choose the reference point it should resize from, and enable No Line Breaks.
Those same options are exposed to scripting through textFramePreferences. Adobe specifically provides autoSizingType, autoSizingReferencePoint and useNoLineBreaksForAutoSizing.
So your two-step idea is reasonable temporarily enable auto-sizing, let InDesign calculate the required dimensions, then turn auto-sizing off again if you don’t want the frame to remain dynamic.
For example
var tf = sel[i];
var prefs = tf.textFramePreferences;
prefs.useNoLineBreaksForAutoSizing = true;
prefs.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_LEFT_POINT;
prefs.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH;
// Once InDesign has resized the frame:
prefs.autoSizingType = AutoSizingTypeEnum.OFF;I’d set the reference point and useNoLineBreaksForAutoSizing before setting autoSizingType but it seems that Adobe recommends setting the sizing type after the other auto-sizing properties.
fit(FitOptions.FRAME_TO_CONTENT) does exist and is documented as resizing the frame to its content, but with text it is still working with the text as currently composed/wrapped. It doesn’t necessarily mean “find the natural unbroken width of this text and resize to that”, which is why you can see the height change without getting the width you expect.
Auto-Size + No Line Breaks + switch Auto-Size off afterwards is probably the cleanest of the approaches for what you describe.
Sign up
Already have an account? Login
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inSign in to Adobe Community
No account yet? Create an account
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.

