重复显示脚本 UI 窗口

Repeated showings of a ScriptUI window

本文关键字:UI 窗口 脚本 显示      更新时间:2023-09-26

遇到了另一个"应该容易"的问题。 我有一个 ScriptUI 窗口,我需要在其中检查以确保所有字段都已填充。 这个想法是,当用户单击"确定"按钮时,脚本会检查以确保字段已填写。 如果没有,那么它应该提醒用户这样做并再次显示相同的窗口,直到他们全部填写。 这是我到目前为止所拥有的:

var windowTitle = "Output Script v1.00 by Chris McGee";
var curDate = new Date();
var curDay = curDate.getDate();
var curMonth = curDate.getMonth() + 1;
var curYear = curDate.getYear() - 100;
var dateFormat = /^(0?[1-9]|1[012])[- '/.](0?[1-9]|[12][0-9]|3[01])[- '/.]'d'd$/;
var outputData = new Window("dialog", windowTitle);
    outputData.preferredSize = [337, 286];
    outputData.orientation = "stack";
    outputData.margins = 0;
    var stackGroup = outputData.add("group");
        stackGroup.margins = [0, 55, 0, 0];
        stackGroup.alignment = ["", "top"];
        stackGroup.alignChildren = ["", "top"];
        var textPanel = stackGroup.add("panel");
            textPanel.orientation = "row";
            textPanel.alignChildren = ["", "bottom"];
            var staticGroup = textPanel.add("group");
                staticGroup.orientation = "column";
                staticGroup.alignChildren = "right";
                staticGroup.spacing = 16;
                staticGroup.add("statictext", undefined, "&Order Number:");
                staticGroup.add("statictext", undefined, "&Ship Date:");
                staticGroup.add("statictext", undefined, "&Initials:");
                staticGroup.add("statictext", undefined, "Ove&rruns?");
            var editGroup = textPanel.add("group");
                editGroup.orientation = "column";
                editGroup.alignChildren = "left";
                var orderNumText = editGroup.add("edittext");
                    orderNumText.characters = 8;
                    orderNumText.active = true;
                    orderNumText.onChange = function ()
                    {
                        if (!/'b'd{6}'b/.test(orderNumText.text))
                        {
                            // If the entered text does not contain exactly 6 digits.
                            alert("The order number does not appear to conform to a standard order number.'n Please fix and try again.");
                        }
                    }
                var shipDateText = editGroup.add("edittext");
                    shipDateText.characters = 8;
                    shipDateText.onChange = function ()
                    {
                        if (dateFormat.test(shipDateText.text))
                        {
                            var enteredDate = shipDateText.text.match(/'b'd*'b/g);
                            var entMonth = parseInt(enteredDate[0]);
                            var entDay = parseInt(enteredDate[2]);
                            var entYear = parseInt(enteredDate[4]);
                            if (entYear < curYear || entYear > curYear + 1)
                            {
                                // If the entered year is older than the current year, or
                                // if the entered year is two or more years in the future.
                                alert("Invalid date entered.  Please fix.");
                            }
                            else if ((entYear > curYear && curMonth < 12) || entMonth > curMonth + 1)
                            {
                                // If the entered month is more than one month ahead, just verify that it is correct.
                                alert("Please verify that the entered date is correct before proceeding.");
                            }
                            else if ((entMonth < curMonth && curMonth < 12) || (entMonth == curMonth && entDay < curDay))
                            {
                                // If the entered date is within the same month, but on an earlier day, alert the user.
                                alert("Invalid date entered.  Please fix.");
                            }
                        }
                        else
                        {
                            alert("Date format not recognized.  Please try again.'nDate must be in the form of MM/DD/YY.");
                            shipDateText.text = "";
                        }
                    }
                var initialsText = editGroup.add("edittext");
                    initialsText.characters = 3;
                var overRunsBox = editGroup.add("checkbox");
        var buttonGroup = stackGroup.add("group");
            buttonGroup.orientation = "column";
            var okButton = buttonGroup.add("button", undefined, "OK", {name: "ok"});
            buttonGroup.add("button", undefined, "Cancel", {name: "cancel"});
var allFieldsFilled = false;
while (!allFieldsFilled)
{
    // If the user clicked "OK", then the contents get put into variables and the work begins on the document(s).
    if (outputData.show() == 1) {
        if (orderNumText.text == "" || shipDateText.text == "" || initialsText.text == "")
        {
            alert("Please fill out ALL fields.");
        }
        else
        {
            allFieldsFilled = true;
            var orderNum = orderNumText.text;
            var shipDate = shipDateText.text;
            var outputInitials = initialsText.text;
            var overRuns = overRunsBox.value;
        }
    } else {
        // If the user clicked "Cancel".
        exit();
    }
}

问题是该窗口在警报消息后不会重新出现。 为什么不呢,我该如何解决这个问题?

编辑09/23/2013:我添加了代码,以便它可以运行。 我还注意到,即使用户单击"取消",也会出现有关输入文本的警报窗口。

编辑 09/24/2013:我尝试实现@ariestav的想法,所以我的代码现在看起来像这样:

var outputData = new Window("dialog", windowTitle);
    outputData.preferredSize = [337, 286];
    outputData.orientation = "stack";
    outputData.margins = 0;
    var stackGroup = outputData.add("group");
        stackGroup.margins = [0, 55, 0, 0];
        stackGroup.alignment = ["", "top"];
        stackGroup.alignChildren = ["", "top"];
        var textPanel = stackGroup.add("panel");
            textPanel.orientation = "row";
            textPanel.alignChildren = ["", "bottom"];
            var staticGroup = textPanel.add("group");
                staticGroup.orientation = "column";
                staticGroup.alignChildren = "right";
                staticGroup.spacing = 16;
                staticGroup.graphics.foregroundColor = staticGroup.graphics.newPen(outputData.graphics.PenType.SOLID_COLOR, [1, 1, 1], 1);
                staticGroup.add("statictext", undefined, "&Order Number:");
                staticGroup.add("statictext", undefined, "&Ship Date:");
                staticGroup.add("statictext", undefined, "&Initials:");
                staticGroup.add("statictext", undefined, "Ove&rruns?");
            var editGroup = textPanel.add("group");
                editGroup.orientation = "column";
                editGroup.alignChildren = "left";
                var orderNumText = editGroup.add("edittext");
                    orderNumText.characters = 8;
                    orderNumText.active = true;
                    orderNumText.onChange = function ()
                    {
                        if (!/'b'd{6}'b/.test(orderNumText.text))
                        {
                            // If the entered text does not contain exactly 6 digits.
                            alert("The order number does not appear to conform to a standard order number.'n Please fix and try again.");
                        }
                    }
                var shipDateText = editGroup.add("edittext");
                    shipDateText.characters = 8;
                    shipDateText.onChange = function ()
                    {
                        if (dateFormat.test(shipDateText.text))
                        {
                            var enteredDate = shipDateText.text.match(/'b'd*'b/g);
                            var entMonth = parseInt(enteredDate[0]);
                            var entDay = parseInt(enteredDate[2]);
                            var entYear = parseInt(enteredDate[4]);
                            if (entYear < curYear || entYear > curYear + 1)
                            {
                                // If the entered year is older than the current year, or
                                // if the entered year is two or more years in the future.
                                alert("Invalid date entered.  Please fix.");
                            }
                            else if ((entYear > curYear && curMonth < 12) || entMonth > curMonth + 1)
                            {
                                // If the entered month is more than one month ahead, just verify that it is correct.
                                alert("Please verify that the entered date is correct before proceeding.");
                            }
                            else if ((entMonth < curMonth && curMonth < 12) || (entMonth == curMonth && entDay < curDay))
                            {
                                // If the entered date is within the same month, but on an earlier day, alert the user.
                                alert("Invalid date entered.  Please fix.");
                            }
                        }
                        else
                        {
                            alert("Date format not recognized.  Please try again.'nDate must be in the form of MM/DD/YY.");
                            shipDateText.text = "";
                        }
                    }
                var initialsText = editGroup.add("edittext");
                    initialsText.characters = 3;
                var overRunsBox = editGroup.add("checkbox");
        var buttonGroup = stackGroup.add("group");
            buttonGroup.orientation = "column";
            var okButton = buttonGroup.add("button", undefined, "OK", {name: "ok"});
                okButton.onClick = function ()
                {
                    if (orderNumText.text == "" || shipDateText.text == "" || initialsText.text == "")
                    {
                        alert("Please fill out ALL fields.");
                    }
                    else
                    {
                        outputData.close();
                    }
                }
            var cancelButton = buttonGroup.add("button", undefined, "Cancel", {name: "cancel"});
                cancelButton.onClick = function () {$.writeln("Canceling."); outputData.close(); exit();}
outputData.show();
var orderNum = orderNumText.text;
var shipDate = shipDateText.text;
var outputInitials = initialsText.text;
var overRuns = overRunsBox.value;

但是,"取消"按钮不会取消。它只是继续代码。 我以为exit();指令会让它完全退出脚本。 我做错了什么?

删除onClick内的exit(),错误消失:

cancelButton.onClick = function () {$.writeln("Canceling."); outputData.close();}

因此,解决这个问题的最简单方法是几乎没有记录的解决方案。 事实证明,.enabled属性可以用于(几乎)任何 ScriptUI 对象。 因此,我可以简单地禁用"确定"按钮,直到所有字段都通过验证。 为此,我需要一个单独的函数,但它完美运行:

function ifFieldsFilled ()
{
    if (orderNumText.text != "" && shipDateText.text != "" && initialsText.text != "" && dateValid)
    {
        return true;
    }
    else
    {
        return false;
    }
}
var curDate = new Date();
var curDay = curDate.getDate();
var curMonth = curDate.getMonth() + 1;
var curYear = curDate.getYear() - 100;
var dateFormat = /^(0?[1-9]|1[012])[- '/.](0?[1-9]|[12][0-9]|3[01])[- '/.]'d'd$/;
var dateValid = false;
var outputData = new Window("dialog", windowTitle);
    outputData.preferredSize = [337, 300];
    outputData.orientation = "stack";
    outputData.margins = 0;
    var stackGroup = outputData.add("group");
        stackGroup.margins = [0, 48, 0, 0];
        stackGroup.alignment = ["", "top"];
        stackGroup.alignChildren = ["", "top"];
        var textPanel = stackGroup.add("panel");
            textPanel.orientation = "row";
            textPanel.alignChildren = ["", "bottom"];
            var staticGroup = textPanel.add("group");
                staticGroup.orientation = "column";
                staticGroup.alignChildren = "right";
                staticGroup.spacing = 16;
                staticGroup.graphics.foregroundColor = staticGroup.graphics.newPen(outputData.graphics.PenType.SOLID_COLOR, [1, 1, 1], 1);
                staticGroup.add("statictext", undefined, "&Order Number:");
                staticGroup.add("statictext", undefined, "&Ship Date:");
                staticGroup.add("statictext", undefined, "&Initials:");
                staticGroup.add("statictext", undefined, "Ove&rruns?");
                staticGroup.add("statictext", undefined, "Digital &Print?");
            var editGroup = textPanel.add("group");
                editGroup.orientation = "column";
                editGroup.alignChildren = "left";
                var orderNumText = editGroup.add("edittext");
                    orderNumText.characters = 8;
                    orderNumText.active = true;
                    orderNumText.onChange = function ()
                    {
                        if (!/'b'd{6}'b/.test(orderNumText.text) && orderNumText.text != "")
                        {
                            // If the entered text does not contain exactly 6 digits.
                            alert("The order number does not appear to conform to a standard order number.'n Please fix and try again.");
                        }
                        okButton.enabled = ifFieldsFilled();
                    }
                var shipDateText = editGroup.add("edittext");
                    shipDateText.characters = 8;
                    shipDateText.onChange = function ()
                    {
                        if (dateFormat.test(shipDateText.text))
                        {
                            var enteredDate = shipDateText.text.match(/'b'd*'b/g);
                            var entMonth = parseInt(enteredDate[0]);
                            var entDay = parseInt(enteredDate[2]);
                            var entYear = parseInt(enteredDate[4]);
                            if (entYear < curYear || entYear > curYear + 1)
                            {
                                // If the entered year is older than the current year, or
                                // if the entered year is two or more years in the future.
                                alert("Invalid date entered.  Please fix.");
                                dateValid = false;
                            }
                            else if ((entYear > curYear && curMonth < 12) || entMonth > curMonth + 1)
                            {
                                // If the entered month is more than one month ahead, just verify that it is correct.
                                alert("Please verify that the entered date is correct before proceeding.");
                            }
                            else if ((entMonth < curMonth && curMonth < 12) || (entMonth == curMonth && entDay < curDay))
                            {
                                // If the entered date is within the same month, but on an earlier day, alert the user.
                                alert("Invalid date entered.  Please fix.");
                                dateValid = false;
                            }
                            else
                            {
                                // The date is completely valid.
                                dateValid = true;
                            }
                        }
                        else
                        {
                            alert("Date format not recognized.  Please try again.'nDate must be in the form of MM/DD/YY.");
                            shipDateText.text = "";
                        }
                        okButton.enabled = ifFieldsFilled();
                    }
                var initialsText = editGroup.add("edittext");
                    initialsText.characters = 3;
                    initialsText.onChanging = function ()
                    {
                        okButton.enabled = ifFieldsFilled();
                    }
                var overRunsBox = editGroup.add("checkbox");
                var digitalPrintBox = editGroup.add("checkbox");
        var buttonGroup = stackGroup.add("group");
            buttonGroup.orientation = "column";
            var okButton = buttonGroup.add("button", undefined, "OK", {name: "ok"});
                okButton.enabled = false;
            var cancelButton = buttonGroup.add("button", undefined, "Cancel", {name: "cancel"});
// If the user clicked "OK", then the contents get put into variables and the work begins on the document(s).
if (outputData.show() == 1) {
    var orderNum = orderNumText.text;
    var shipDate = shipDateText.text;
    var outputInitials = initialsText.text;
    var overRuns = overRunsBox.value;
    var digitalPrint = digitalPrintBox.value;
} else {
    // If the user clicked "Cancel".
    exit();
}

代码从那里继续。

因此,okButton开始时被禁用,然后在每次用户填写字段时进行检查,或者在initialsText字段的情况下,当用户键入内容时,它会进行检查。 此外,添加到orderNumText .onChange()功能的额外条件&& orderNumText.text != ""仅用于防止在用户点击 [ESC] 或在填写该文本框之前单击"取消"按钮时显示烦人的警报对话框。

所以,那个鬼鬼祟祟的小.enabled属性是帮助我解决这个问题的关键。 回想起来,这是一个显而易见的答案。 无论如何,我希望这对遇到类似问题的任何其他人都有帮助。 当心!