Skip to main content
Inspiring
April 17, 2025
Answered

Script UI - Dynamically autosizing statictext

  • April 17, 2025
  • 6 replies
  • 1851 views

@Peter Kahrel , @Eugene Tyson , @Marc Autret 

 

I'm sure this has been asked before, but I could not find a solution.

 

I've never been able to post images to this forum. See https://github.com/Marshall-Brooks/Sandbox/issues/2 for images.

 

I created (with help) a custom confimation script here: https://community.adobe.com/t5/indesign-discussions/autoclose-alert-message-and-custom-confirm-message/td-p/15255213

 

I needed the script to dynamically change size. The displayed text might be one line or 3 or 4 paragraphs when the script is called. I added the {multiline=true} parameter. See the link above for the results. If the link vanishes:

 

var msg = win.add("statictext", undefined, message, {multiline: true});

Probably works and autosizes, but the text is wrapping before it gets to half the width of the window.

var msg = win.add("statictext",  [5, 5, 500, 200], message, {multiline: true});

Works fine, but I think the script will not display if it goes beyond the borders, and the message takes up half the screen for a 1-line message.

var msg = win.add("statictext", [5, undefined, 500, undefined], message, {multiline: true});

Didn't display any text at all.

I think the characters property is supposed to allow this, but:

var msg = win.add("statictext",  undefined, message, {characters: 300, multiline: true});

Didn't seem to do anything?

I hope it isn't going to turn into something like having to count the number of characters and line feeds and size the window based on the results.

 

Thanks in advance!!!

 

Correct answer Marshall_Brooks

@Peter Kahrel - Thanks, I'm used to the inconsistencies, although not being able to change it would drive me nuts.
@Eugene Tyson made an auto-close message box for me and clicking OK works fine in ESTK. Does nothing in FrameMaker ...
I'll report results after I play some more with the font sizing.
Question, while I've got your attention - is there any easy way to calculate the width of W_statictext?

What I want is  the box to wrap if the text is more than one line and auto-shrink if the box is less.
I'm thinking code will be something like:

var w = new Window('dialog', 'Autosize Test');
    w.margins = 20;

    // Dialog Width To Be Defined:
    var w_width = 400;

    // Stactic Text To Be Defined:
    w_statictext = 'This is a dynamically sized message. It can be short or it can be very long. \nIt should wrap nicely and the box should grow vertically to fit the text without cutting off or overflowing weirdly. \n\nThis Solution is not mine but written by Marc Autret from Indiscripts in July 2013.';
// If less than one line, is there a better way to calculate this?
if (w_statictext.length < 31) {
// single line
     var st = w.add('statictext', undefined, 'X');
}
else{
// is this the correct way to set the font?
    var st = w.add('statictext', undefined, 'X', {multiline: true, .graphics.font=Tahoma:14});
    var X_width = st.preferredSize[0];
    with(st){preferredSize = [-1,-1]; characters = ~~(w_width/X_width); preferredSize[1] = -1;};
    st.text = w_statictext;
}
    // "Close" Button:
    w.add('button', undefined, 'Close', {name: 'OK'});

w.show();

The length<31 is just a guess, but I'm thinking I can set length<1 which will force it to multiline, screen shot that, and then increase the length value until the window is wider than the screenshot.

 

Unless you know a better solution?

 

 


Fixed. Works with non-default font sizes (if allowed). Shows single-line if short enough, or wraps otherwise.

The value in the "if" statement will need to change if you use a wider window or a different font face or size:


EDIT: Had to make a minor change to the "if" statement, the original script would fail (display only one line) if w_statictext was less than 80 characters and had manual line feeds, e.g.:

w_statictext = "Line 1\rLine2";

EDIT2 - Had to update the if statement - not found is -1, not 0. 

Final (???) Script:

var w = new Window('dialog', 'Autosize Test');
    w.margins = 10;

    // Dialog Width To Be Defined:
    var w_width = 450;

    // Stactic Text To Be Defined:
    w_statictext = 'This is a dynamically sized message. It can be short or it can be very long. \nIt should wrap nicely and the box should grow vertically to fit the text without cutting off or overflowing weirdly. \n\nThis Solution is not mine but written by Marc Autret from Indiscripts in July 2013.';
//  80 below is somewhat arbitrary. Run the script and see where the first line ends. Adjust w_static text to a bit longer than that. Uncomment the line below to get the text length, Use that in the "if" statement.
//alert (w_statictext.length)
if (w_statictext.length < 80 && w_statictext.indexOf("\r")===-1 && w_statictext.indexOf("\n")===-1) {
// single line
     var st = w.add('statictext', undefined, w_statictext);
}
else{
// multiline    
    var st = w.add('statictext', undefined, 'X', {multiline: true});
    // if not using standard font size, Font must be defined below, even if using set_font, or the message will truncate unpredictably.
    st.graphics.font="Tahoma:14";
    var X_width = st.preferredSize[0];
    with(st){preferredSize = [-1,-1]; characters = ~~(w_width/X_width); preferredSize[1] = -1;};
    st.text = w_statictext;
}
    // "Close" Button:
    w.add('button', undefined, 'Close', {name: 'OK'});
set_font (w, "Tahoma:14");
w.show();

function set_font (control, font) {
    for (var i = 0; i < control.children.length; i++) {
    if ("GroupPanel".indexOf (control.children[i].constructor.name) > -1)
    set_font (control.children[i], font);
    else
    control.children[i].graphics.font = font;
    }
}//--end set_font;

 

6 replies

FRIdNGE
April 18, 2025
var w = new Window('dialog', 'Autosize Test');
    w.margins = 20;

    // Dialog Width To Be Defined:
    var w_width = 400;

    // Stactic Text To Be Defined:
    w_statictext = 'This is a dynamically sized message. It can be short or it can be very long. \nIt should wrap nicely and the box should grow vertically to fit the text without cutting off or overflowing weirdly. \n\nThis Solution is not mine but written by Marc Autret from Indiscripts in July 2013.';

    var st = w.add('statictext', undefined, 'X', {multiline: true});
    var X_width = st.preferredSize[0];
    with(st){preferredSize = [-1,-1]; characters = ~~(w_width/X_width); preferredSize[1] = -1;};
    st.text = w_statictext;

    // "Close" Button:
    w.add('button', undefined, 'Close', {name: 'OK'});

w.show();

 

(^/)  The Jedi

Community Expert
April 18, 2025
Peter Kahrel
Community Expert
Community Expert
April 18, 2025

@Marshall_Brooks 

 

A resizable window doesn't see when it gets overset. You'll have to set a minimum size.

Community Expert
April 18, 2025

Seems to work ok for me

var message = "This is a long message that should scroll if it overflows. This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows.This is a long message that should scroll if it overflows."

var win = new Window("dialog", "Scrollable Message Box");

// Group to contain the scrollable text
var group = win.add("group");
group.orientation = "column";
group.alignChildren = ["fill", "top"];
group.margins = 10;

// Scrollable EditText instead of statictext
var msg = group.add("edittext", undefined, message, {
    multiline: true,
    readonly: true,
    scrolling: true
});
msg.preferredSize = [400, 200]; // Set dimensions
msg.alignment = ["fill", "top"];

var ok = group.add("button", undefined, "OK", {name: "ok"});

win.defaultElement = ok;
ok.onClick = function () {
    win.close();
};

win.center();
win.show();
Community Expert
April 18, 2025

This also works - but when the message is very long it eats up the screen real estate

 

var message = "This is a dynamically sized message. It can be short or it can be very long.\n\nIt should wrap nicely and the box should grow vertically to fit the text without cutting off or overflowing weirdly. ";

var win = new Window("dialog", "Autosize Test");
var group = win.add("group");
group.orientation = "column";
group.alignChildren = ["fill", "top"];
group.margins = 10;

// Create a statictext that fills width and grows with content
var msg = group.add("statictext", undefined, message, {multiline: true});
msg.maximumSize.width = 400;
msg.minimumSize.width = 400;
msg.alignment = ["fill", "top"];

win.layout.layout(true);
win.center();
win.show();
leo.r
Community Expert
Community Expert
April 18, 2025

I don't know if you're aware of this tool - maybe it can help:

https://scriptui.joonas.me/

m1b
Community Expert
Community Expert
April 18, 2025

Hi @Marshall_Brooks, one approach that often works well is to set the edittext's

alignment: ['fill', 'fill']

That should fill the parent container. If it doesn't work, check if you parent container is also set to fill it's container.

 

If that's no good, then it might be worth posting the full code for your simple example dialog here.

- Mark 

Robert at ID-Tasker
Legend
April 17, 2025

Multiline means that text will be wrapped - but it's up to you to define width and height of the area = you'll have to do the math yourself.