• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

UI Close button delivers saved parameters

Engaged ,
Mar 13, 2022 Mar 13, 2022

Copy link to clipboard

Copied

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

 

 

TOPICS
Actions and scripting

Views

312

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Mar 13, 2022 Mar 13, 2022

 

#target photoshop
app.bringToFront();
function showDialog() {
    var dialog = new Window("dialog");
    dialog.text = "Add Selected File Layers";
    dialog.orientation = "row";
    dialog.alignChildren = ["left", "top"];
    dialog.spacing = 10;
    dialog.margins = 16;
    var group1 = dialog.add("group", undefined, { name: "group1" });
    group1.orientation = "column";
    group1.alignChildren = ["fill", "top"];
    group1.spacing = 10;
    group1.margins = 0;
    group1.alignment = ["lef
...

Votes

Translate

Translate
Adobe
Guide ,
Mar 13, 2022 Mar 13, 2022

Copy link to clipboard

Copied

 

#target photoshop
app.bringToFront();
function showDialog() {
    var dialog = new Window("dialog");
    dialog.text = "Add Selected File Layers";
    dialog.orientation = "row";
    dialog.alignChildren = ["left", "top"];
    dialog.spacing = 10;
    dialog.margins = 16;
    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"];
    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;
    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;
    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"
    }
    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";
    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(1);
    };
    dialog.center();
    return dialog.show();
}
var radiobutton1 = false;
var radiobutton2 = false;
var radiobutton3 = false;
var radiobutton4 = false;
var checkbox1 = false;
var checkbox20 = false;
const settingsID = "exportOptions";
const radiobutton1ID = stringIDToTypeID("radiobutton1");
const radiobutton2ID = stringIDToTypeID("radiobutton2");
const radiobutton3ID = stringIDToTypeID("radiobutton3");
const radiobutton4ID = stringIDToTypeID("radiobutton4");
const checkbox1ID = stringIDToTypeID("checkbox1");
var testDoc = app.activeDocument;
var exportSettings;
try {
    exportSettings = app.getCustomOptions(settingsID);
} catch (e) {
    saveSettings();
}
if (typeof exportSettings == "undefined") {
    saveSettings();
}
main();
function main() {
    loadSettings();
    var result = showDialog();
    if (result != 2) {
        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')
        }
    }
}
function loadSettings() {
    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() {
    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);
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 13, 2022 Mar 13, 2022

Copy link to clipboard

Copied

Thank you Jazzy for your awesome help! 

As far as I can tell the save setting works when the UI element returns a true or false boolean.

Can the save setting work with an UI element that returns a string? 

For example, is it possible to remember the string from the dynamic text field when the value is not  Background copy?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 13, 2022 Mar 13, 2022

Copy link to clipboard

Copied

In the example above, we return the result of .show() method. It can assign only numeric values from .close() function. You can take this feature into account and encode string by stringIDToTypeID, then decode by typeIDToStringID when processing the result.

However, we can also return the result of the entire showDialog() function by writing the appropriate statement after dialog.show(), as is done in the example below. In this case, the result of the function execution is limited only by your imagination (to store the result, it is advisable to use a variable in the showDialog () scope that changes the value when a particular button is pressed)

 

#target photoshop
app.bringToFront();
function showDialog() {
    var dialog = new Window("dialog");
    dialog.text = "Add Selected File Layers";
    dialog.orientation = "row";
    dialog.alignChildren = ["left", "top"];
    dialog.spacing = 10;
    dialog.margins = 16;
    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"];
    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;
    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;
    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"];
    
    var result = ''
    dialog.edittext1.onChange = function () {
        if (dialog.edittext1.text == "") {
            alert("The text field can not be empty.");
        }
        else {
            win.hide();
        }
        dialog.edittext1.text = "Background copy"
    }
    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";
    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();
        result = 'Wow! You press OK!'
        dialog.close();
    };
    cancel.onClick = function () {
        result = 'Caaancelling!'
        dialog.close();
    };
    dialog.center();
    dialog.show();
    return result
}
var radiobutton1 = false;
var radiobutton2 = false;
var radiobutton3 = false;
var radiobutton4 = false;
var checkbox1 = false;
var checkbox20 = false;
const settingsID = "exportOptions";
const radiobutton1ID = stringIDToTypeID("radiobutton1");
const radiobutton2ID = stringIDToTypeID("radiobutton2");
const radiobutton3ID = stringIDToTypeID("radiobutton3");
const radiobutton4ID = stringIDToTypeID("radiobutton4");
const checkbox1ID = stringIDToTypeID("checkbox1");
var testDoc = app.activeDocument;
var exportSettings;
try {
    exportSettings = app.getCustomOptions(settingsID);
} catch (e) {
    saveSettings();
}
if (typeof exportSettings == "undefined") {
    saveSettings();
}
main();
function main() {
    loadSettings();
    var result = showDialog();
    alert (result)
}
function loadSettings() {
    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() {
    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);
}

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 13, 2022 Mar 13, 2022

Copy link to clipboard

Copied

LATEST

Thank you for your explanation. Now I have a general idea of what needs to happen to remember the input in the text field.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines