@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;