Copy link to clipboard
Copied
I found this thread: https://community.adobe.com/t5/illustrator-discussions/auto-close-alert-message/td-p/9398258
Images here: https://github.com/Marshall-Brooks/Sandbox/issues/2
See also:
I'm somewhat spoiled by the Enhanced Message Box add-on for Microsoft Access which shows a standard Msgbox with a countdown of seconds until the box closes.
I used @Peter Kahrel's Scipt UI guide to modify the first linked script as follows:
function AutocloseAlert(message, delaySeconds, title){
title = title || 'Alert';
var alertWindow = new Window('palette', title);
// var control_text = alertWindow.add('edittext', [0, 0, 500, 200], message, {multiline: true});
// var control_text = alertWindow.add('edittext', , message, {multiline: true});
var control_text = alertWindow.add('edittext', undefined, message, {multline:true})
// if(delaySeconds === 0){
var control_close = alertWindow.add('button', undefined, 'OK');
control_close.onClick = function(){
if(alertWindow){
alertWindow.hide();
alertWindow = null;
}
};
// }
alertWindow.show();
alertWindow.update();
if(delaySeconds > 0){
$.sleep(delaySeconds * 1000);
alertWindow.hide();
alertWindow = null;
}
}I'm having the following issues:
var startTime = new Date().getTime();
while ( new Date().getTime() - startTime < delaySeconds * 1000 ) {alertWindow.update()} But it didn't change how it worked - i.e. the window was still grayed out, and clicking the OK button did not close the window, but it did disappear after the delaySeconds interval.
Thank you in advance!!!
Eh that didn't work - is this what you're thinking of ?
This works for me
function CountdownAlert(message, delaySeconds, title) {
title = title || "Countdown Alert";
var win = new Window("palette", title);
win.orientation = "column";
win.alignChildren = "center";
var msg = win.add("statictext", undefined, message);
var btn = win.add("button", undefined, "OK (" + delaySeconds + ")");
var cancelled = false;
// Function for button click
btn.onClick = functio...
Updated the scripts to auto-size and or wrap correctly with the help of this thread: https://community.adobe.com/t5/indesign-discussions/script-ui-dynamically-autosizing-statictext/td-p/15275567#M621971
EDIT: Had to make a minor change to the "if" statement in AutocloseAlert(). The previous version would fail (only display the first line) if message length was < 80 and you have manual line feeds, e.g.:
AutocloseAlert("Line 1\rLine2");EDIT2: Had to correct the "if" statement, not found returns -1
...Recommend making a minor tweak to the CustomConfirm() code.
The above works fine if your button text is short (Yes/No/Retry).
If the button text is longer, the window expands to fit the button text (which is nice and I didn't really expect), but the message still wraps beforre it needs to.
Recommend changing the line:
var w_width = 450;to:
var w_width = Math.max(450, (Btn1Text.length+Btn2Text.length+Btn3Text.length+Btn4Text.length)*9);Basically, just empirically derived, but it seems to work well in
...CustomPrompt Code:
var result;
result = CustomPrompt("test","input","title");
alert (result);
result = prompt("test","input","title");
alert (result);
function CustomPrompt(message, defaultinput, title)
{
var usercancelled = false;
title = title || "Script Alert";
defaultinput = defaultinput || "";
var win = new Window("dialog", title, undefined, {closeButton:false});
win.orientation = "row"; // not deprecated and required here, despite what VS Code says.
win.alignChildre...
Copy link to clipboard
Copied
I'm back again. I decided to try to make my own script to replace the prompt command for three reasons:
I actually have the window working the way I want, but my code returns "undefined" whether you click Ok or Cancel instead of the inputbox text or null, and I don't know why.
Code:
var result;
result = CustomPrompt("test","input","title");
alert (result);
result = prompt("test","input","title");
alert (result);
function CustomPrompt(message, defaultinput, title)
{
title = title || "Script Alert";
defaultinput = defaultinput || "";
var win = new Window("dialog", title, undefined, {closeButton:false});
win.orientation = "row"; // not deprecated and required here, despite what VS Code says.
win.alignChildren = "top";
win.margins = 10;
var myInputGroup = win.add ("group");
myInputGroup.orientation = "column";
myInputGroup.alignChildren = "left";
// Dialog Width To Be Defined:
var w_width = 250;
// 150 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 (message.length)
if (message.length < 15 && message.indexOf("\r")===-1 && message.indexOf("\n")===-1)
{
// single line
var msg = myInputGroup.add('statictext', undefined, message);
}
else
{
// multiline
var msg = myInputGroup.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.
msg.graphics.font="Tahoma:14";
var X_width = msg.preferredSize[0];
msg.preferredSize = [-1,-1];
msg.characters = ~~(w_width/X_width);
msg.preferredSize[1] = -1;
msg.text = message;
}
var InputBox = myInputGroup.add ("edittext", undefined, defaultinput);
InputBox.characters = 30;
InputBox.active = true;
var myButtonGroup = win.add ("group");
myButtonGroup.orientation = "column";
var btnOk = myButtonGroup.add ("button", undefined, "OK", {name: "ok"});
var btnCancel = myButtonGroup.add ("button", undefined, "Cancel", {name: "cancel"});
btnOk.onClick = function ()
{
alert (InputBox.text);
var answer = InputBox.text;
alert (answer);
win.close();
alert (answer);
return answer;
}
btnCancel.onClick = function ()
{
win.close();
var answer = null;
return answer;
}
set_font (win, "Tahoma:14");
win.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_fontThank you in advance!
Copy link to clipboard
Copied
I think I see the problem, but not the solution.
"answer" is being returned by the .onClick of either button, but nothing is being returned by CustomPrompt(), but I don't see how to pass "answer" to CustomPrompt() so that it can then be passed to the calling function.
Solved - I'll update the code above in a minute ...
Copy link to clipboard
Copied
CustomPrompt Code:
var result;
result = CustomPrompt("test","input","title");
alert (result);
result = prompt("test","input","title");
alert (result);
function CustomPrompt(message, defaultinput, title)
{
var usercancelled = false;
title = title || "Script Alert";
defaultinput = defaultinput || "";
var win = new Window("dialog", title, undefined, {closeButton:false});
win.orientation = "row"; // not deprecated and required here, despite what VS Code says.
win.alignChildren = "top";
win.margins = 10;
var myInputGroup = win.add ("group");
myInputGroup.orientation = "column";
myInputGroup.alignChildren = "left";
// Dialog Width To Be Defined:
var w_width = 275;
// 50 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 (message.length)
if (message.length < 50 && message.indexOf("\r")===-1 && message.indexOf("\n")===-1)
{
// single line
var msg = myInputGroup.add('statictext', undefined, message);
}
else
{
// multiline
var msg = myInputGroup.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.
msg.graphics.font="Tahoma:14";
var X_width = msg.preferredSize[0];
msg.preferredSize = [-1,-1];
msg.characters = ~~(w_width/X_width);
msg.preferredSize[1] = -1;
msg.text = message;
}
var InputBox = myInputGroup.add ("edittext", undefined, defaultinput);
InputBox.characters = 30;
InputBox.active = true;
var myButtonGroup = win.add ("group");
myButtonGroup.orientation = "column";
var btnOk = myButtonGroup.add ("button", undefined, "OK", {name: "ok"});
var btnCancel = myButtonGroup.add ("button", undefined, "Cancel", {name: "cancel"});
btnOk.onClick = function ()
{
win.close();
}
btnCancel.onClick = function ()
{
win.close();
usercancelled = true;
}
set_font (win, "Tahoma:14");
win.show ();
if (usercancelled ===true)
{
return null;
}
else
{
return InputBox.text
}
} // end CustomPrompt
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_fontMinor edits above. I had "if message.length < 15" instead of "if message.length < 50". Also I changed w_width to 275. Script was truncating after 6 lines of text with 250, but I don't know why and I am not sure why 275 works better.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more