Answered
UI Close button delivers saved parameters
The script UI remembers the last settings and passes the UI parameters to the main function. When the user cancels the UI the script still delivers the saved parameters to the main function.
Is it possible to omit the saved parameters when using the UI Close button so that no data is delivered to the main function?
#target photoshop
app.bringToFront();
function showDialog () {
// DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Add Selected File Layers";
dialog.orientation = "row";
dialog.alignChildren = ["left","top"];
dialog.spacing = 10;
dialog.margins = 16;
// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"});
group1.orientation = "column";
group1.alignChildren = ["fill","top"];
group1.spacing = 10;
group1.margins = 0;
group1.alignment = ["left","fill"];
// PANEL1
// ======
var panel1 = group1.add("panel", undefined, undefined, {name: "panel1"});
panel1.text = "File Type";
panel1.orientation = "column";
panel1.alignChildren = ["left","top"];
panel1.spacing = 10;
panel1.margins = 10;
panel1.alignment = ["fill","top"];
dialog.radiobutton1 = panel1.add("radiobutton", undefined, undefined, {name: "radiobutton1"});
dialog.radiobutton1.text = "PSD (8 Bit)";
dialog.radiobutton1.value = radiobutton1;
dialog.radiobutton2 = panel1.add("radiobutton", undefined, undefined, {name: "radiobutton2"});
dialog.radiobutton2.text = "TIFF (8 Bit)";
dialog.radiobutton2.value = radiobutton2;
// PANEL2
// ======
var panel2 = group1.add("panel", undefined, undefined, {name: "panel2"});
panel2.text = "Color Profile";
panel2.orientation = "column";
panel2.alignChildren = ["left","top"];
panel2.spacing = 10;
panel2.margins = 10;
panel2.alignment = ["fill","top"];
dialog.radiobutton3 = panel2.add("radiobutton", undefined, undefined, {name: "radiobutton3"});
dialog.radiobutton3.text = "Adobe RGB";
dialog.radiobutton3.value = radiobutton3;
dialog.radiobutton4 = panel2.add("radiobutton", undefined, undefined, {name: "radiobutton4"});
dialog.radiobutton4.text = "Adobe sRGB";
dialog.radiobutton4.value = radiobutton4;
// PANEL3
// ======
var panel3 = group1.add("panel", undefined, undefined, {name: "panel3"});
panel3.text = "Background Layer";
panel3.orientation = "column";
panel3.alignChildren = ["left","top"];
panel3.spacing = 10;
panel3.margins = 10;
panel3.alignment = ["fill","top"];
dialog.checkbox1 = panel3.add("checkbox", undefined, undefined, {name: "checkbox1"});
dialog.checkbox1.text = "Background Copy";
dialog.checkbox1.value = checkbox1;
var statictext1 = panel3.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = "Rename Layer";
dialog.edittext1 = panel3.add('edittext {properties: {name: "edittext1"}}');
dialog.edittext1.text = "Background copy";
dialog.edittext1.alignment = ["fill","top"];
dialog.edittext1.onChange = function()
{
if (dialog.edittext1.text == "")
{
alert("The text field can not be empty.");
}
else
{
win.hide();
}
dialog.edittext1.text = "Background copy"
}
// GROUP4
// ======
var group4 = dialog.add("group", undefined, {name: "group4"});
group4.orientation = "column";
group4.alignChildren = ["fill","top"];
group4.spacing = 10;
group4.margins = 0;
group4.alignment = ["left","fill"];
var ok = group4.add("button", undefined, undefined, {name: "ok"});
ok.text = "OK";
var cancel = group4.add("button", undefined, undefined, {name: "cancel"});
cancel.text = "Cancel";
//update the params and save them off on close
function updateParams() {
radiobutton1 = dialog.radiobutton1.value;
radiobutton2 = dialog.radiobutton2.value;
radiobutton3 = dialog.radiobutton3.value;
radiobutton4 = dialog.radiobutton4.value;
checkbox1 = dialog.checkbox1.value;
};
ok.onClick = function () {
updateParams();
saveSettings();
dialog.close();
};
cancel.onClick = function () {
dialog.close();
};
dialog.center();
dialog.show();
}
///////////////////////////////////////////////////////
// UI Dialog Save Settings
///////////////////////////////////////////////////////
// Settings
var radiobutton1 = false;
var radiobutton2 = false;
var radiobutton3 = false;
var radiobutton4 = false;
var checkbox1 = false;
var checkbox20 = false;
//IDs for custom option saving / loading
const settingsID = "exportOptions";
const radiobutton1ID = stringIDToTypeID("radiobutton1");
const radiobutton2ID = stringIDToTypeID("radiobutton2");
const radiobutton3ID = stringIDToTypeID("radiobutton3");
const radiobutton4ID = stringIDToTypeID("radiobutton4");
const checkbox1ID = stringIDToTypeID("checkbox1");
//try and load previous settings
var testDoc = app.activeDocument;
var exportSettings;
try {
exportSettings = app.getCustomOptions(settingsID);
} catch (e) {
saveSettings();
}
if(typeof exportSettings == "undefined") {
saveSettings();
}
///////////////////////////////////////////////////////
// MAIN
///////////////////////////////////////////////////////
main();
function main() {
loadSettings();
showDialog();
if(radiobutton1) {
alert('Save PSD file')
}
if(radiobutton2) {
alert('Save TIFF file')
}
if(radiobutton3) {
alert('Set profile Adobe RGB')
}
if(radiobutton4) {
alert('Set profile Adobe sRGB')
}
if(checkbox1) {
alert('Background copy')
}
}
///////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////
function loadSettings() {
// check for previously saved dialog options
// exclude layer group name and feather value
exportSettings = app.getCustomOptions(settingsID);
if(exportSettings.hasKey (radiobutton1ID))
radiobutton1 = exportSettings.getBoolean (radiobutton1ID);
if(exportSettings.hasKey (radiobutton2ID))
radiobutton2 = exportSettings.getBoolean (radiobutton2ID);
if(exportSettings.hasKey (radiobutton3ID))
radiobutton3 = exportSettings.getBoolean (radiobutton3ID);
if(exportSettings.hasKey (radiobutton4ID))
radiobutton4 = exportSettings.getBoolean (radiobutton4ID);
if(exportSettings.hasKey (checkbox1ID))
checkbox1 = exportSettings.getBoolean (checkbox1ID);
}
function saveSettings() {
//save defaults
var newExportSettings = new ActionDescriptor();
newExportSettings.putBoolean (radiobutton1ID, radiobutton1);
newExportSettings.putBoolean (radiobutton2ID, radiobutton2);
newExportSettings.putBoolean (radiobutton3ID, radiobutton3);
newExportSettings.putBoolean (radiobutton4ID, radiobutton4);
newExportSettings.putBoolean (checkbox1ID, checkbox1);
app.putCustomOptions(settingsID,newExportSettings,true);
}
