Highlighted

Save the ui state of a script

New Here ,
Sep 02, 2020

Copy link to clipboard

Copied

Hi everyone,
I'm looking for an elegant way to store the value changes on a script ui from one use to another.
Here is a simple example, how to get the same value back for checkboxes and editable text.

 

// ======
var dialog = new Window("dialog"); 
    dialog.text = "Dialog"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["center","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

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

var checkboxA = group1.add("checkbox", undefined, undefined, {name: "checkboxA"}); 
    checkboxA.text = "A"; 
    checkboxA.value = true; 

var checkboxB = group1.add("checkbox", undefined, undefined, {name: "checkboxB"}); 
    checkboxB.text = "B"; 
    checkboxB.value = true; 

var checkboxC = group1.add("checkbox", undefined, undefined, {name: "checkboxC"}); 
    checkboxC.text = "C"; 
    checkboxC.value = false; 

var checkboxD = group1.add("checkbox", undefined, undefined, {name: "checkboxD"}); 
    checkboxD.text = "D"; 
    checkboxD.value = false; 

var checkboxE = group1.add("checkbox", undefined, undefined, {name: "checkboxE"}); 
    checkboxE.text = "E"; 
    checkboxE.value = false; 

// DIALOG
// ======
var edittext1 = dialog.add('edittext {properties: {name: "edittext1"}}'); 
    edittext1.text = "my text 1"; 

var launchButton = dialog.add("button", undefined, undefined, {name: "launchButton"}); 
    launchButton.text = "launch"; 
	launchButton.onClick = function(){
		//do stuff
		dialog.close();
	};

dialog.show();

 

  I' m actually doing the job with another .jsx wher i store the values.

 

//version to manage the pref file
prefVersion = 1.00
//check if the pref file is up to date
prefFile = new File(Folder.myDocuments + "/myPref_" + prefVersion + ".jsx");
if (!prefFile.exists) {
	var fileList = new Folder(Folder.myDocuments).getFiles();
	for (var i = 0; i < fileList.length; i++) {
		fileList[i].remove(); //erase the file if it is not up to date
	}
}



// ========================================= UI
var dialog = new Window("dialog"); 
    dialog.text = "Dialog"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["center","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

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

var checkboxA = group1.add("checkbox", undefined, undefined, {name: "checkboxA"}); 
    checkboxA.text = "A"; 
    checkboxA.value = false; 

var checkboxB = group1.add("checkbox", undefined, undefined, {name: "checkboxB"}); 
    checkboxB.text = "B"; 
    checkboxB.value = false; 

var checkboxC = group1.add("checkbox", undefined, undefined, {name: "checkboxC"}); 
    checkboxC.text = "C"; 
    checkboxC.value = false; 

var checkboxD = group1.add("checkbox", undefined, undefined, {name: "checkboxD"}); 
    checkboxD.text = "D"; 
    checkboxD.value = false; 

var checkboxE = group1.add("checkbox", undefined, undefined, {name: "checkboxE"}); 
    checkboxE.text = "E"; 
    checkboxE.value = false; 

// DIALOG
// ======
var edittext1 = dialog.add('edittext {properties: {name: "edittext1"}}'); 
    edittext1.text = "my text 1"; 

var launchButton = dialog.add("button", undefined, undefined, {name: "launchButton"}); 
    launchButton.text = "launch"; 
	launchButton.onClick = function(){
		//do stuff
		dialog.close();
	};


//check if the pref file exist
if (!prefFile.exists) {
	defaultPref(); 
}
// if not a pref file with default value is create
function defaultPref() {
	var defaultValue = "prefVersion = 1.00; checkboxA.value = false; checkboxB.value = false; checkboxC.value = false; checkboxD.value = false; checkboxE.value = false; edittext1.text = \"my text 1\";";
	prefFile.open('w');
	prefFile.writeln(defaultValue);
	prefFile.close();
	return "defaultValueUP"
}

//include pref file
eval('#include' + prefFile.fullName);

dialog.show();


dialog.onClose = savePref();

//on close the pref file is updated
function savePref() {
	var newValue = " checkboxA.value = " + checkboxA.value + "; checkboxB.value = " + checkboxB.value + "; checkboxC.value = " + checkboxC.value + "; checkboxD.value = " + checkboxD.value + "; checkboxE.value = " + checkboxE.value +"; edittext1.text = \"" + edittext1.text + "\" ;";
	prefFile.open('w');
	prefFile.writeln(newValue.toString());
	prefFile.close();
}

 


Any idea ?

thanks  🙂

Teddy

TOPICS
Actions and scripting, How to

Views

55

Likes

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

Save the ui state of a script

New Here ,
Sep 02, 2020

Copy link to clipboard

Copied

Hi everyone,
I'm looking for an elegant way to store the value changes on a script ui from one use to another.
Here is a simple example, how to get the same value back for checkboxes and editable text.

 

// ======
var dialog = new Window("dialog"); 
    dialog.text = "Dialog"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["center","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

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

var checkboxA = group1.add("checkbox", undefined, undefined, {name: "checkboxA"}); 
    checkboxA.text = "A"; 
    checkboxA.value = true; 

var checkboxB = group1.add("checkbox", undefined, undefined, {name: "checkboxB"}); 
    checkboxB.text = "B"; 
    checkboxB.value = true; 

var checkboxC = group1.add("checkbox", undefined, undefined, {name: "checkboxC"}); 
    checkboxC.text = "C"; 
    checkboxC.value = false; 

var checkboxD = group1.add("checkbox", undefined, undefined, {name: "checkboxD"}); 
    checkboxD.text = "D"; 
    checkboxD.value = false; 

var checkboxE = group1.add("checkbox", undefined, undefined, {name: "checkboxE"}); 
    checkboxE.text = "E"; 
    checkboxE.value = false; 

// DIALOG
// ======
var edittext1 = dialog.add('edittext {properties: {name: "edittext1"}}'); 
    edittext1.text = "my text 1"; 

var launchButton = dialog.add("button", undefined, undefined, {name: "launchButton"}); 
    launchButton.text = "launch"; 
	launchButton.onClick = function(){
		//do stuff
		dialog.close();
	};

dialog.show();

 

  I' m actually doing the job with another .jsx wher i store the values.

 

//version to manage the pref file
prefVersion = 1.00
//check if the pref file is up to date
prefFile = new File(Folder.myDocuments + "/myPref_" + prefVersion + ".jsx");
if (!prefFile.exists) {
	var fileList = new Folder(Folder.myDocuments).getFiles();
	for (var i = 0; i < fileList.length; i++) {
		fileList[i].remove(); //erase the file if it is not up to date
	}
}



// ========================================= UI
var dialog = new Window("dialog"); 
    dialog.text = "Dialog"; 
    dialog.orientation = "column"; 
    dialog.alignChildren = ["center","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

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

var checkboxA = group1.add("checkbox", undefined, undefined, {name: "checkboxA"}); 
    checkboxA.text = "A"; 
    checkboxA.value = false; 

var checkboxB = group1.add("checkbox", undefined, undefined, {name: "checkboxB"}); 
    checkboxB.text = "B"; 
    checkboxB.value = false; 

var checkboxC = group1.add("checkbox", undefined, undefined, {name: "checkboxC"}); 
    checkboxC.text = "C"; 
    checkboxC.value = false; 

var checkboxD = group1.add("checkbox", undefined, undefined, {name: "checkboxD"}); 
    checkboxD.text = "D"; 
    checkboxD.value = false; 

var checkboxE = group1.add("checkbox", undefined, undefined, {name: "checkboxE"}); 
    checkboxE.text = "E"; 
    checkboxE.value = false; 

// DIALOG
// ======
var edittext1 = dialog.add('edittext {properties: {name: "edittext1"}}'); 
    edittext1.text = "my text 1"; 

var launchButton = dialog.add("button", undefined, undefined, {name: "launchButton"}); 
    launchButton.text = "launch"; 
	launchButton.onClick = function(){
		//do stuff
		dialog.close();
	};


//check if the pref file exist
if (!prefFile.exists) {
	defaultPref(); 
}
// if not a pref file with default value is create
function defaultPref() {
	var defaultValue = "prefVersion = 1.00; checkboxA.value = false; checkboxB.value = false; checkboxC.value = false; checkboxD.value = false; checkboxE.value = false; edittext1.text = \"my text 1\";";
	prefFile.open('w');
	prefFile.writeln(defaultValue);
	prefFile.close();
	return "defaultValueUP"
}

//include pref file
eval('#include' + prefFile.fullName);

dialog.show();


dialog.onClose = savePref();

//on close the pref file is updated
function savePref() {
	var newValue = " checkboxA.value = " + checkboxA.value + "; checkboxB.value = " + checkboxB.value + "; checkboxC.value = " + checkboxC.value + "; checkboxD.value = " + checkboxD.value + "; checkboxE.value = " + checkboxE.value +"; edittext1.text = \"" + edittext1.text + "\" ;";
	prefFile.open('w');
	prefFile.writeln(newValue.toString());
	prefFile.close();
}

 


Any idea ?

thanks  🙂

Teddy

TOPICS
Actions and scripting, How to

Views

56

Likes

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
Sep 02, 2020 0
LEGEND ,
Sep 02, 2020

Copy link to clipboard

Copied

A txt-file would do as well. 

 

Another option would be using a custom namespace to store the settings, but I couldn’t find a more recent or pertinent thread than this one at current. 

https://community.adobe.com/t5/photoshop/newbie-question-how-to-create-a-namespace-and-record-fileda...

Likes

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
Reply
Loading...
Sep 02, 2020 0
New Here ,
Sep 03, 2020

Copy link to clipboard

Copied

thx,
for sure, i will do it with a text file.
hmmm i don't really anderstand the the thread.
But to store the value in an object sounds like an elegant option. Is that what you are talking about ?

Likes

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
Reply
Loading...
Sep 03, 2020 0
Adobe Community Professional ,
Sep 02, 2020

Copy link to clipboard

Copied

Either a text file or see if you can write to Preferences (which is possible in Bridge.)

 

app.preferences.myNamespace_.myPreference

Likes

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
Reply
Loading...
Sep 02, 2020 1
New Here ,
Sep 03, 2020

Copy link to clipboard

Copied

Thx,
i don't know why i didn't simply create a text file ^^'

it'is not really pref but just UI values i want to store...

Likes

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
Reply
Loading...
Sep 03, 2020 0