Site logo

Text frame

Auto-grow text frame

This script makes the 1st text frame in the active document to grow vertically: from top to bottom.

var myDoc = app.activeDocument;
var myTF = myDoc.textFrames[0];
var myViewPr = myDoc.viewPreferences;
var myHM = myViewPr.horizontalMeasurementUnits;
var myVM = myViewPr.verticalMeasurementUnits;
myViewPr.horizontalMeasurementUnits = MeasurementUnits.millimeters;
myViewPr.verticalMeasurementUnits = MeasurementUnits.millimeters;

while (myTF.overflows){
  var myBounds = myTF.geometricBounds;
  myBounds[2] += 1;
  myTF.geometricBounds = myBounds;
}

myViewPr.horizontalMeasurementUnits = myHM;
myViewPr.verticalMeasurementUnits = myVM;

Changing myBounds[2]+= ; to myBounds[3]+= 1; makes it grow horizontally from left to right. BTW, unlike fit() method, this approach works for multicolumn text frames.

See also: Fit Text Frame to Content (supporting multicolumn).

Get text column number

sel = app.selection[0];
frame = sel.parentTextFrames[0];
$.writeln (frame.insertionPoints.itemByRange (0, sel.index).textColumns.length-1);