Skip to content

Instantly share code, notes, and snippets.

@indiscripts
Created February 17, 2025 04:21
Show Gist options
  • Select an option

  • Save indiscripts/742c3760fe08beea37496e0db4fba4d2 to your computer and use it in GitHub Desktop.

Select an option

Save indiscripts/742c3760fe08beea37496e0db4fba4d2 to your computer and use it in GitHub Desktop.
Set Frame Height by Line Count (InDesign script - Draft)
(function setFrameHeightsByLineCount( ask,NL,tf,pf,dup,mul,a,t,n,k,L)
//--------------------------------------------------------------------
// Select one or more textframes (or have the insertion pt in a frame),
// then enter the desired line number.
{
const LN_MIN = 1;
const LN_MAX = 32;
const SZOF = +AutoSizingTypeEnum.OFF;
const FITC = +FitOptions.FRAME_TO_CONTENT;
// ---
const CS_IN = +CoordinateSpaces.INNER_COORDINATES;
const AP_TOP = +AnchorPoint.TOP_CENTER_ANCHOR;
const RM_MUL = +ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY;
const WARN = '\u26A0';
const __ = $.global.localize;
a = app.selection||0;
if( !a ) return;
// Inner text -> parent frame.
1==a.length
&& (t=a[0]).hasOwnProperty('horizontalOffset')
&& (a=t.parentTextFrames||[]);
ask = __("Desired Line Number?\r(%1 to %2)", LN_MIN, LN_MAX);
for( NL=1/0 ; tf=a.pop() ; )
{
if( !(tf||0).isValid ) continue;
if( tf.constructor.name.slice(-9) != 'TextFrame' ) continue;
pf = tf.textFramePreferences.properties;
if( +pf.autoSizingType != SZOF ) continue;
if( pf.textColumnCount != 1 ) continue;
n = tf.lines.length;
while( ask )
{
NL = prompt(ask, ''+n, "Set Height by Line Count");
if( null===NL ) return;
NL = parseInt(NL, 10);
isFinite(NL) && NL >= LN_MIN && NL <= LN_MAX
? ( ask=false )
: ( -1==ask.indexOf(WARN) && ask=ask.split('\r').join('\r'+WARN+' ') );
}
(dup = tf.duplicate()).properties = { ignoreWrap:true };
// Make sure we have enough 'potential' lines in the story.
dup.parentStory.texts[0].contents += Array(2+NL).join('\r.')
for
(
mult=1, t=-1, L=dup.lines ;
t != (n=L.length) && NL != n ;
dup.resize( CS_IN, AP_TOP, RM_MUL, [1,k=(NL/(t=n))] ),
mult *= k
);
dup.remove();
1 != mult && tf.resize( CS_IN, AP_TOP, RM_MUL, [1,mult] );
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment