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

how to resize a text frame to a given value

Community Beginner ,
Feb 15, 2025 Feb 15, 2025

Copy link to clipboard

Copied

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

Views

209
Translate

Report

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

Community Beginner , 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
...

Votes

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
...

Votes

Translate
Community Beginner ,
Feb 15, 2025 Feb 15, 2025

Copy link to clipboard

Copied

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.

 

Votes

Translate

Report

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 Beginner ,
Feb 15, 2025 Feb 15, 2025

Copy link to clipboard

Copied

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],

);
}

 

Votes

Translate

Report

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

Copy link to clipboard

Copied

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],
    ];

};

Votes

Translate

Report

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

Copy link to clipboard

Copied

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.gifexpand image

 

 

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

 

Best,

Marc

Votes

Translate

Report

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

Copy link to clipboard

Copied

@Marc Autret, what fun! Beautiful!

- Mark

Votes

Translate

Report

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

Copy link to clipboard

Copied

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 â†“

 

Votes

Translate

Report

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

Copy link to clipboard

Copied

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

 

Votes

Translate

Report

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