Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

how to resize a text frame to a given value

Explorer ,
Feb 15, 2025 Feb 15, 2025

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.

TOPICS
Scripting
623
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Explorer , Feb 15, 2025 Feb 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;
bre
...
Translate
Community Expert , Feb 15, 2025 Feb 15, 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)
        retu
...
Translate
Explorer ,
Feb 15, 2025 Feb 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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2025 Feb 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],

);
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2025 Feb 15, 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],
    ];

};
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 16, 2025 Feb 16, 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…

setHeightByLines.gif

 

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

 

Best,

Marc

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 17, 2025 Feb 17, 2025

@Marc Autret, what fun! Beautiful!

- Mark

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 17, 2025 Feb 17, 2025
LATEST

An improved version (undoable, bugfix, etc.) is now available in the IdGoodies repo:

→ https://github.com/indiscripts/IdGoodies/blob/master/full/SelSetFrameHeightByLineCount.jsx

 

Note that it also supports threaded frames ↓

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 15, 2025 Feb 15, 2025

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines