Repeating ScriptUI section doesn't work
I'm fairly well-versed in Javascript and ExtendScript by now, including ScriptUI. However, there's one situation that eludes me, and that is when there is a repeating section in a ScriptUI dialog and how to handle dynamic controls. Here's a self-contained example that should run just fine in the ExtendScript Toolkit for InDesign CC 2015:
/**
* Label is an object that represents a single label for a product.
*
*/
function Label(lSize, lQuantity, lInstructions, lContentType, lContents) {
this.lSize = lSize; // String: The size of the label ("Brother 3x1", "Zebra 3x1", or "Zebra 6x4").
this.lQuantity = lQuantity; // Number: Actual quantity of this label.
this.lInstructions = lInstructions; // String: The instructions for this label.
this.lContentType = lContentType; // String: "Text" = text-only label, "File" = file-based label (using pre-made artwork).
this.lContents = lContents; // String or File: Depending on previous property, can be a String for a text-only label,
// or a File object for a file-based label.
}
// Label groups, as an object.
function labelGroup (labelPanel, quantityInstGroup, quantityInstStatics, quantityInstFields, quantity,
instructions, labelSizeGroup,
rbBrother3x1, rbZebra3x1, rbZebra6x4,
labelTypeGroup, labelTypeRadios, labelTypeFields,
typeFileGroup, rbTypeText, rbTypeFile,
textfield, filenameField, browsebutton, fileselected) {
this.labelPanel = labelPanel;
this.quantityInstGroup = quantityInstGroup;
this.quantityInstStatics = quantityInstStatics;
this.quantityInstFields = quantityInstFields;
this.quantity = quantity;
this.instructions = instructions;
this.labelSizeGroup = labelSizeGroup;
this.rbBrother3x1 = rbBrother3x1;
this.rbZebra3x1 = rbZebra3x1;
this.rbZebra6x4 = rbZebra6x4;
this.labelTypeGroup = labelTypeGroup;
this.labelTypeRadios = labelTypeRadios;
this.labelTypeFields = labelTypeFields;
this.typeFileGroup = typeFileGroup;
this.rbTypeText = rbTypeText;
this.rbTypeFile = rbTypeFile;
this.textfield = textfield;
this.filenameField = filenameField;
this.browsebutton = browsebutton;
this.fileselected = fileselected;
}
// Label dialog.
function getLabelInfo (labelArray, lastButton) {
var totalNumLabels = 3;
var wLabelTitle = "Label";
wLabelTitle += totalNumLabels > 1 ? "s " : " ";
wLabelTitle += "for item: " + "<Test Name>" + ".";
var lastButtonText = lastButton ? "Begin Proofing" : "Next Product";
var wLabel = new Window("dialog", wLabelTitle);
var productNameText = wLabel.add("statictext", undefined, "<Test Name>");
// productNameText.graphics.font = ScriptUI.newFont("Myriad Pro", "Bold", 20); // Font handling no longer works in CC+.
var labelGroups = [];
for (var index = 0; index < totalNumLabels; index++) {
// For each label for this item, create a labelGroup object and add it to the array.
var thisGroup = new labelGroup();
thisGroup.labelPanel = wLabel.add("panel", /*[0, 0, 300, 75]*/ undefined, "Label " + (index + 1));
thisGroup.labelPanel.orientation = "row";
thisGroup.quantityInstGroup = thisGroup.labelPanel.add("group");
thisGroup.quantityInstGroup.alignChildren = "top";
thisGroup.quantityInstStatics = thisGroup.quantityInstGroup.add("group");
thisGroup.quantityInstStatics.orientation = "column";
thisGroup.quantityInstStatics.alignChildren = "right";
// thisGroup.margins = [0, 3, 0, 0]; // [left, top, right, bottom].
thisGroup.quantityInstStatics.margins.top = 3;
thisGroup.quantityInstStatics.spacing = 15;
thisGroup.quantityInstStatics.add("statictext", undefined, "Quantity: ");
thisGroup.quantityInstStatics.add("statictext", undefined, "Instructions: ");
thisGroup.quantityInstFields = thisGroup.quantityInstGroup.add("group");
thisGroup.quantityInstFields.orientation = "column";
thisGroup.quantityInstFields.alignChildren = "left";
thisGroup.quantity = thisGroup.quantityInstFields.add("edittext");
thisGroup.quantity.characters = 6;
thisGroup.instructions = thisGroup.quantityInstFields.add("edittext", [0, 0, 200, 55], "",
{multiline: true, scrolling: true, wantReturn: true});
thisGroup.labelPanel.add("panel", [0, 0, 2, 100]); // Vertical divider line.
thisGroup.labelSizeGroup = thisGroup.labelPanel.add("group");
thisGroup.labelSizeGroup.orientation = "column";
thisGroup.labelSizeGroup.alignChildren = "left";
thisGroup.rbBrother3x1 = thisGroup.labelSizeGroup.add("radiobutton", undefined, "Brother 3x1");
thisGroup.rbZebra3x1 = thisGroup.labelSizeGroup.add("radiobutton", undefined, "Zebra 3x1");
thisGroup.rbZebra6x4 = thisGroup.labelSizeGroup.add("radiobutton", undefined, "Zebra 6x4");
thisGroup.rbBrother3x1.value = true;
thisGroup.labelPanel.add("panel", [0, 0, 2, 100]); // Vertical divider line.
thisGroup.labelTypeGroup = thisGroup.labelPanel.add("group");
thisGroup.labelTypeGroup.alignChildren = "top";
thisGroup.labelTypeRadios = thisGroup.labelTypeGroup.add("group");
thisGroup.labelTypeRadios.orientation = "column";
thisGroup.labelTypeRadios.alignChildren = "left";
thisGroup.labelTypeRadios.margins.top = 6;
thisGroup.labelTypeRadios.spacing = 13;
thisGroup.rbTypeFile = thisGroup.labelTypeRadios.add("radiobutton", undefined, "File");
thisGroup.rbTypeText = thisGroup.labelTypeRadios.add("radiobutton", undefined, "Text");
thisGroup.rbTypeText.value = true;
thisGroup.labelTypeFields = thisGroup.labelTypeGroup.add("group");
thisGroup.labelTypeFields.orientation = "column";
thisGroup.labelTypeFields.alignChildren = "left";
thisGroup.typeFileGroup = thisGroup.labelTypeFields.add("group");
thisGroup.filenameField = thisGroup.typeFileGroup.add("edittext", undefined, "---", {readonly: true});
thisGroup.filenameField.characters = 20;
thisGroup.browsebutton = thisGroup.typeFileGroup.add("button", undefined, "Browse");
thisGroup.browsebutton.onClick = function () {
thisGroup.fileselected = File.openDialog();
thisGroup.filenameField.text = thisGroup.fileselected.name;
};
thisGroup.textfield = thisGroup.labelTypeFields.add("edittext", [0, 0, 200, 55], "",
{multiline: true, scrolling: true, wantReturn: true});
labelGroups.push(thisGroup);
}
var buttonGroup = wLabel.add("group");
buttonGroup.alignment = "right";
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
var okButton = buttonGroup.add("button", undefined, lastButtonText);
function getLabelSize (rButtonGroup) {
for (k = 0; k < rButtonGroup.children.length; k++) {
if (rButtonGroup.children
.value === true) { return rButtonGroup.children
.text; }
}
}
function getContents (currentGroup) {
if (currentGroup.rbTypeText.value) {
return currentGroup.textfield.text;
}
return currentGroup.fileselected;
}
if (wLabel.show() == 1) {
// Assign data gathered from fields to product's object.
var thisLabel;
for (j = 0; j < labelGroups.length; j++) {
thisLabel = new Label(
getLabelSize(labelGroups
.labelSizeGroup), parseInt(labelGroups
.quantity.text.replaceAll(/\,/g, ''), 10), labelGroups
.instructions.text, labelGroups
.textradiobutton.value ? "Text" : "File", getContents(labelGroups
) );
labelArray.push(thisLabel);
}
} else {
$.writeln("[getLabelInfo] Canceling the Label window.");
return 7;
}
return 0;
}
var returnedValue = 0;
var labelArray = [];
returnedValue = getLabelInfo(labelArray, true);
This one has the repeating section appear 3 times, but in my main program, it could appear once or even 10 times. I won't know ahead of time, but I need the information to be stored with each section. The problem lies in selecting a file for a section using the "Browse" button. I'd like the filename to appear in the same section's edittext field next to the button, but that's not happening. What can I do make sure that *does* happen?