Skip to main content
Inspiring
May 8, 2020
Answered

Photoshop UI save user input options

  • May 8, 2020
  • 2 replies
  • 5614 views

I am working with a small script UI dialog with checkbox and radio buttons. Ideally, the user would set their own checkbox/radio button combination which the script retains. The idea is that after closing and restating Photoshop the script recalls the last used checkbox/radio button combination. 

The options to save user input data include writing to internal storage xml/txt file; Using the app.getCustomOptions(), app.saveCustumOptions, app.eraseCustomOptions() functions and perhaps saving the user input through an external dialog UI. Where can I find examples that use the getCustomOptions(), saveCustomOptions() & ersaeCustomOptions() functions? or perhaps using a separate UI to save the settings of the main UI.

This topic has been closed for replies.
Correct answer r-bin

I was able to Frankenstein a test script that retains the user input. Now I am having difficulty passing the UI parameters to the main() script function. Online 120 the putCustomOptins is set to true, the script does not retain the user input after restating Photoshop. Can someone have a look a the script and suggest how to workaround passing the UI parameters to the main() please.

 



#target photoshop
app.bringToFront();

function showDialog () {
 

// DIALOG
// ======
var dialog = new Window("dialog", "Save User Settings");
dialog.orientation = "row";
dialog.alignChildren = ["center","top"];
dialog.spacing = 10;
dialog.margins = 16;

// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"});
group1.orientation = "column";
group1.alignChildren = ["left","center"];
group1.spacing = 10;
group1.margins = 0;

dialog.checkbox1 = group1.add("checkbox", undefined, undefined, {name: "checkbox1"});
dialog.checkbox1.text = "Group Layers";
dialog.checkbox1.value = checkbox1;
dialog.checkbox1.alignment = "left";

dialog.radiobutton1 = group1.add("radiobutton", undefined, undefined, {name: "radiobutton1"});
dialog.radiobutton1.text = "Add Mask";
dialog.radiobutton1.value = radiobutton1;
dialog.radiobutton1.alignment = "left";

dialog.radiobutton2 = group1.add("radiobutton", undefined, undefined, {name: "radiobutton2"});
dialog.radiobutton2.text = "Invert Mask";
dialog.radiobutton2.value = radiobutton2;
dialog.radiobutton2.alignment = "left";

dialog.radiobutton3 = group1.add("radiobutton", undefined, undefined, {name: "radiobutton3"});
dialog.radiobutton3.text = "Omit Mask";
dialog.radiobutton3.value = radiobutton3;
dialog.radiobutton3.alignment = "left";

 
// GROUP2 OK/CANCEL
// ======
var group2 = dialog.add("group", undefined, {name: "group2"});
group2.orientation = "column";
group2.alignChildren = ["left","center"];
group2.spacing = 10;
group2.margins = 0;

var button1 = group2.add("button", undefined, undefined, {name: "button1"});
button1.text = "OK";
button1.alignment = ["fill","center"];
// button1.onClick = function(){

// main(checkbox1,radiobutton1,radiobutton2,radiobutton3);

// };

 
var button2 = group2.add("button", undefined, undefined, {name: "button2"});
button2.text = "Cancel";
button2.alignment = ["fill","center"];

//update the params and save them off on close
function updateParams() {
checkbox1 = dialog.checkbox1.value;
radiobutton1 = dialog.radiobutton1.value;
radiobutton2 = dialog.radiobutton2.value;
radiobutton3 = dialog.radiobutton3.value;
};
dialog.onClose = function() {
updateParams();
saveSettings();
};

button2.onClick = function () { this.parent.close(0); return; };
button1.onClick = function () {
updateParams();
//main(checkbox1,radiobutton1,radiobutton2,radiobutton3);
this.parent.close(0);
};


dialog.center();
dialog.show();

}
// END DIALOG
// ====

function loadSettings() {
//check for previously saved dialog options
exportSettings = app.getCustomOptions(settingsID);
 
if(exportSettings.hasKey (checkbox1ID))
checkbox1 = exportSettings.getBoolean (checkbox1ID);
if(exportSettings.hasKey (radiobutton1ID))
radiobutton1 = exportSettings.getBoolean (radiobutton1ID);
if(exportSettings.hasKey (radiobutton2ID))
radiobutton2 = exportSettings.getBoolean (radiobutton2ID);
if(exportSettings.hasKey (radiobutton3ID))
radiobutton3 = exportSettings.getBoolean (radiobutton3ID);
}

function saveSettings() {
//save defaults
var newExportSettings = new ActionDescriptor();

newExportSettings.putBoolean (checkbox1ID, checkbox1);
newExportSettings.putBoolean (radiobutton1ID, radiobutton1);
newExportSettings.putBoolean (radiobutton2ID, radiobutton2);
newExportSettings.putBoolean (radiobutton3ID, radiobutton3);

app.putCustomOptions(settingsID,newExportSettings,true);
}


// Settings.
var checkbox1 = true;
var radiobutton1 = false;
var radiobutton2 = false;
var radiobutton3 = false;


//IDs for custom option saving / loading
const settingsID = stringIDToTypeID("exportOptions");

const checkbox1ID = stringIDToTypeID("checkbox1");
const radiobutton1ID = stringIDToTypeID("radiobutton1");
const radiobutton2ID = stringIDToTypeID("radiobutton2");
const radiobutton3ID = stringIDToTypeID("radiobutton3");


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

// alert(checkbox1.value);
// alert(radiobutton1.value);
// alert(radiobutton2.value);
// alert(radiobutton3.value);

}

Do not use

const settingsID = stringIDToTypeID("exportOptions");

use

const settingsID = "exportOptions";

 

NOTE:

In the first case, settingsID will be a random number.
 

2 replies

Legend
May 8, 2020

app.putCustomOptions (key: string, customObject: ActionDescriptor) allows you to save the settings as an ActionDescriptor object that you can create yourself (see photoshop javaScript reference). You can use any string variable as a key, but in order to avoid errors (as JJMack correctly noted) it is better to use a unique key of the GUID (UUID) type (which you can generate by yourself, or using some service like uuidgenerator.net). By default, the settings are stored permanently (i.e. you can get them after restarting Photoshop).

 

When saving the settings, you create a new ActionDescriptor object into which you place the values ​​you need (for example, the state of the controls) in the format "key" -> "value". The key is the name of the variable in a form convenient for you (at the same time, it is accepted in the number format, i.e. it must first be converted using app.stringIDToTypeID ()).

That is, to save some arbitrary set of variables, we must first do the following:

 

s2t = stringIDToTypeID;

var myKey1 = 100,
 myKey2 = 'sample string',
 myKey3 = false;
 
 var settingsObj = new ActionDescriptor
 
 settingsObj.putInteger (s2t('key1'), myKey1)
 settingsObj.putString (s2t('key2'), myKey2)
 settingsObj.putBoolean (s2t('key3'), myKey3)

 

and then:

 

 var GUID = 'a15f44e3-7170-4110-a626-ee3d33fd4d06'
 app.putCustomOptions (GUID, settingsObj)

 

When receiving the settings, we need to do everything in the reverse order. First get the ActionDescriptor of our settings:

 

var GUID = 'a15f44e3-7170-4110-a626-ee3d33fd4d06'
 try {settingsObj = app.getCustomOptions (GUID)} catch (e) {}

 

(try ... catch is needed so that the script does not give an error if the settings were not saved earlier (this is usually the first script run)).

Then put values from that ​​ActionDescriptor to the variables:

 

s2t = stringIDToTypeID;

if (settingsObj != undefined) {
 var myKey1 = settingsObj.getInteger(s2t('key1')),
 myKey2 = settingsObj.getString (s2t('key2')),
 myKey3 =settingsObj.getBoolean(s2t('key3'));
 }

 

This is the easiest way.

 

You can find more complex options using objects for storing settings in the scripts that come with Photoshop: Fit Image.jsx, which JJMack used, is a good choice.

SuperMerlin
Inspiring
May 8, 2020

app.putCustomOpions (GUID, settingsObj );

This will only retain settings whilst Photoshop is open, if you want it to retain the details if Photoshop is closed then opened you would need:-

app.putCustomOpions (GUID, settingsObj, true );

 

Legend
May 8, 2020

It is true by default.

JJMack
Community Expert
Community Expert
May 8, 2020

Mostly Get and Put rarely see erase.  I only hack at Photoshop Scripting.  Like I stole Adobe's   Fit Image code and change its Dialog and function to my dialogs and functions to create two Plug-in for my use in actions.  The Plug-in code is all Adobe's modified for my use.  By the way I did not know what a UUID was  when  I stole the code I just knew I needed to change what was coded needed to be changed to be unique for my scripts.

 

Adobe Fit Image Dialog Looks like this.

My function needed more parameters.  Needed two number several check boxes some radio buttons etc.  Still the scripts overall design is all Fit image. My Dialog looks like this.

 

JJMack
Inspiring
May 8, 2020

To clarify do the getCustomOptions() and outCustomOptions() retain the user input from a UI dialog after Photoshop quits? In other words, the next time script runs after Photoshop opens, will the user data appear in the dialog?