Skip to main content
Known Participant
February 15, 2025
Answered

how to resize a text frame to a given value

  • February 15, 2025
  • 3 replies
  • 747 views

Hello,

 

I want to change the height of  a text frame to a fixed value in mm, anchoring the text frame from top center.

No changes in width of text frame.

 

Please suggest a code.

Thanks and regards.

Correct answer m1b

Hi @Moiz5FB2, well done on getting your script to work!

 

Just for your learning here is your same script but I've adjusted a few things to how I like them, and also used Robert's idea of using the `geometricBounds` to set the size. Using the `resize` method is just fine, too—this is just showing another way.

- Mark

const mm = 2.834645;

function main() {

    var item = app.selection[0];
    var lineCount = prompt("Please set type a line number (1 to 4)", "1");

    if (!lineCount)
        return;

    setHeightByLineCount(item, lineCount);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Frame Height');


function setHeightByLineCount(item, lineCount) {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    lineCount = Number(lineCount);

    if (isNaN(lineCount))
        return;

    // get height value, in mm
    // depending on line count
    var height = [
        0,          // 0 lines
        206.6,      // 1 lines
        213.0,      // 2 lines
        219.5,      // 3 lines
        225.7,      // 4 lines
        231.9,      // 5 lines
    ][lineCount] * mm;

    if (!height)
        return;

    // change height only using `geometricBounds` property:
    item.geometricBounds = [
        item.geometricBounds[0],
        item.geometricBounds[1],
        item.geometricBounds[0] + height,
        item.geometricBounds[3],
    ];

};

3 replies

Robert at ID-Tasker
Legend
February 15, 2025

Or you can read geometricBounds and modify 3rd value - [2] in JS notation - to [0]+height. 

 

Moiz5FB2Author
Known Participant
February 15, 2025
This code works for me.
 

 

function main() {
var l = prompt("Please set type a line number (1 to 4)", "1");
resizeHeight(l);
}

main();

function resizeHeight(line) {
// app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
// app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

var sel = app.selection[0];

var w = 0;

switch (line) {
case "1":
w = 585.637;
break;
case "2":
w = 603.78;
break;
case "3":
w = 622.204;
break;
case "4":
w = 639.921;
break;
case "5":
w = 657.354;
break;
}
// alert(w);
sel.resize(
CoordinateSpaces.INNER_COORDINATES,

AnchorPoint.TOP_CENTER_ANCHOR,

ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,

[325.984, w],

);
}

 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
February 16, 2025

Hi @Moiz5FB2, well done on getting your script to work!

 

Just for your learning here is your same script but I've adjusted a few things to how I like them, and also used Robert's idea of using the `geometricBounds` to set the size. Using the `resize` method is just fine, too—this is just showing another way.

- Mark

const mm = 2.834645;

function main() {

    var item = app.selection[0];
    var lineCount = prompt("Please set type a line number (1 to 4)", "1");

    if (!lineCount)
        return;

    setHeightByLineCount(item, lineCount);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Frame Height');


function setHeightByLineCount(item, lineCount) {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    lineCount = Number(lineCount);

    if (isNaN(lineCount))
        return;

    // get height value, in mm
    // depending on line count
    var height = [
        0,          // 0 lines
        206.6,      // 1 lines
        213.0,      // 2 lines
        219.5,      // 3 lines
        225.7,      // 4 lines
        231.9,      // 5 lines
    ][lineCount] * mm;

    if (!height)
        return;

    // change height only using `geometricBounds` property:
    item.geometricBounds = [
        item.geometricBounds[0],
        item.geometricBounds[1],
        item.geometricBounds[0] + height,
        item.geometricBounds[3],
    ];

};
Marc Autret
Legend
February 17, 2025

Hi @m1b 

 

Amusing challenge!

 

My two pennies: maybe we could make the whole setHeightByLineCount routine 100% agnostic. Here is a first draft that seems promising…

 

https://gist.github.com/indiscripts/742c3760fe08beea37496e0db4fba4d2

 

Best,

Marc

Moiz5FB2Author
Known Participant
February 15, 2025

I want to change the height of selected a text frame to a fixed value in mm, anchoring the text frame from top center, via a script.

No changes in width of text frame.