JavaScript对话框返回

时间:2010-03-01 21:26:58

标签: javascript function return-value return extendscript

我有一个绘制对话框的JavaScript函数。我想让它返回用户指定的值。问题是,当用户点击两个分配了onClick个事件的按钮时,对话框将关闭。我知道获取这些事件的唯一方法是为它们分配函数,这意味着返回会导致指定的函数返回,而不是我的inputDialog函数。我确定我只是以愚蠢的方式做这件事。

如果您想知道,此脚本使用Adobe的ExtendScript API来扩展After Effects。

以下是代码:

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();

}

2 个答案:

答案 0 :(得分:0)

我想出了一个解决方案。这真的很难看,但现在它会让我感到震惊......任何人都有更好的解决方案吗?

    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;

答案 1 :(得分:0)

使用特定的整数关闭对话框可能会更好。然后,您可以检查show()的返回值并返回适当的值。请参阅jongware对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;