JavaScript dialog return
I've got a JavaScript function that draws a dialog. I'd like to have it return the value the user specifies. The problem is, the dialog is closed when a user clicks on of two buttons, which have onClick
events assigned to them. The only way I know to grab these events is by assigning functions to them, which means that a return causes the assigned function to return, and not my inputDialog function. I'm sure I'm just doing this in a stupid way.
In case you're wondering, this script is using Adobe's ExtendScript API for extending After Effects.
Here's the code:
function inputDialog (queryString, title){
// Create a window of type dialog.
var dia = new Window("dialog", title, [100,100,330,200]); // bounds = [left, top, right, bottom]
this.windowRef = dia;
// Add the components, a label, two buttons and input
dia.label = dia.add("statictext", [20, 10, 210, 30]);
dia.label.text = queryString;
dia.input = dia.add("edittext", [20, 30, 210, 50]);
dia.input.textselection = "New Selection";
dia.input.active = true;
dia.okBtn = dia.add("button", [20,65,105,85], "OK");
dia.cancelBtn = dia.add("button", [120, 65, 210, 85], "Cancel");
// Register event listeners that define the button behavior
//user clicked OK
dia.okBtn.onClick = function() {
if(dia.input.text != "") { //check that the text input wasn't empty
var result = dia.input.text;
dia.close(); //close the window
if(debug) alert(result);
return result;
} else { //the text box is blank
alert("Please enter a value."); //don't close the window, ask the user to enter something
}
};
//user clicked Cancel
dia.cancelBtn.onClick = function() {
dia.close();
if(debug)开发者_运维技巧 alert("dialog cancelled");
return false;
};
// Display the window
dia.show();
}
I've come up with a solution. It's really ugly, but it'll tide me over for now... Anyone have a better solution?
var ret = null;
// Register event listeners that define the button behavior
dia.okBtn.onClick = function() {
if(dia.input.text != "") { //check that the text input wasn't empty
var result = dia.input.text;
dia.close(); //close the window
if(debug) alert(result);
ret = result;
return result;
} else { //the text box is blank
alert("Please enter a value."); //don't close the window, ask the user to enter something
}
};
dia.cancelBtn.onClick = function() { //user cancelled action
dia.close();
ret = false;
return false;
};
// Display the window
dia.show();
while(ret == null){};
return ret;
It might be slightly better to close the dialog with a specific integer. You can then check the return value from show()
and return the appropriate value. See jongware's reference to ExtendScript ScriptUI Window's close method.
// Register event listeners that define the button behavior
dia.okBtn.onClick = function() {
if(dia.input.text != "") { //check that the text input wasn't empty
dia.close(1); //close the window
} else { //the text box is blank
alert("Please enter a value."); //don't close the window, ask the user to enter something
}
};
dia.cancelBtn.onClick = function() { //user cancelled action
dia.close(2);
};
var buttonCloseValue = dia.show(); //blocks until user interaction
if(dia.show() == 1) {
if(debug) alert(dia.input.text);
return dia.input.text;
}
return false;
精彩评论