Copy link to clipboard
Copied
Hello friends, how are you? Here in my work I'm trying to develop a small system for registering frame profiles just for my internal control where I can manage them, but I intend to run it in Photoshop itself through a dialog box. Everything seemed to be going well when I detected a terrible error that completely compromises the project. I will have to register hundreds of profiles in a drop-down list where I will have to locate and edit their data, but I lose all my data when the list is updated when using the search system. Guys this is very important for my work, if anyone can share a solution or other more confident method.
Thanks. Attached files:
https://drive.google.com/drive/folders/1YtD72yq4gTqSR6YNMtwFjNgGo_ogH9hU?usp=sharing
dlg = new Window("dialog"); dlg.text = "Dialog";
dlg.orientation = "column"; dlg.alignChildren = ["center","top"];
et1 = dlg.add('edittext {properties: {name: "et1"}}'); et1.text = "Search";
et1.preferredSize.width = 200;
et1.onChanging = function (){
var temp = this.text;
listMD.removeAll ();
for (var i = 0; i < listA_carregar.length; i++) {
if (listA_carregar[i].toLowerCase().indexOf (temp) == 0) {
listMD.add ('item', listA_carregar[i]);
}
}
listMD.selection = 0;
}
var txtlistA = "~/desktop/mylist.txt";
var txtlistA = File(txtlistA);
txtlistA.open('r'); var strtext_A =txtlistA.read();
var listA_carregar = strtext_A.split("\n");
listA_carregar.pop();
listMD = dlg.add("dropdownlist", undefined, undefined, {name: "listMD", items: listA_carregar});
listMD.selection = 0; listMD.preferredSize.width = 200;
g1 = dlg.add("group", undefined, {name: "g1"});
g1.orientation = "row"; g1.alignChildren = ["left","center"];
b1 = g1.add("button", undefined, undefined, {name: "b1"}); b1.text = "+";
b2 = g1.add("button", undefined, undefined, {name: "b2"}); b2.text = "-";
b3 = g1.add("button", undefined, undefined, {name: "b3"}); b3.text = "Edit";
b1.onClick = function(){f=1; presets()}
b2.onClick = function(){remover()}
b3.onClick = function(){f=null; presets()}
dlg.show();
function presets(){
var deg = new Window("dialog"); deg.text = "Prests";
deg.orientation = "row";
var ed1 = deg.add('edittext {properties: {name: "ed1"}}'); ed1.preferredSize.width = 150;
ed1.active=true; if(f==null){ed1.text = listMD.selection.text }
var b4 = deg.add("button", undefined, undefined, {name: "b4"}); b4.text = "Save";
b4.onClick = function(){
deg.close();
if(f==null){listMD.selection.text = ed1.text}
else{listMD.add("item", ed1.text ); listMD.selection = listMD.items.length-1;}
salvar(listA_carregar, listMD.items, txtlistA);
}
deg.show();
}
function remover(){
var sel = listMD.selection.index;
listMD.remove(listMD.selection);
if(sel > listMD.items.length-1) listMD.selection = listMD.items.length-1;
else listMD.selection = sel;
salvar(listA_carregar, listMD.items, txtlistA);
}
function salvar(ma, mo, f){
var itensAtuais = ma; if(itensAtuais == null) return;
var fileList= mo; if(fileList.length>0){
var out = new File(f); out.open("w"); out.encoding="UTF8";
for(var a = 0; a < fileList.length; a++){ out.writeln(fileList[a]);} out.close();
}
};
For your code it looks something like this:
dlg = new Window("dialog"); dlg.text = "Dialog";
dlg.orientation = "column"; dlg.alignChildren = ["center", "top"];
et1 = dlg.add('edittext {properties: {name: "et1"}}'); et1.text = "Search";
et1.preferredSize.width = 200;
et1.onChanging = function () {
var temp = this.text;
listMD.removeAll();
filteredItems = [];
for (var i = 0; i < listA_carregar.length; i++) {
if (listA_carregar[i].toLowerCase().indexOf(temp) == 0) {
...
Copy link to clipboard
Copied
You write to the file not the original array with data, but the elements of the drop-down list. Not surprisingly, if as a result of the search you have 1 element left, then only it will be saved when writing to the file ¯\_(ツ)_/¯
You need to keep the dropdown and data array in sync: when you add or remove an element, you must not only add and remove it from the list, but also perform the same operations on the original array of strings. Accordingly, when writing to a file, you must also pass not the elements of the drop-down list, but the original array - this will allow you not to refer to the result of filtering the list, but to save the full data set.
Copy link to clipboard
Copied
Hi @jazz-y thanks for replying:
How to keep dataset and initiality in sync?
I'm still a beginner and I confess to you that I'm certainly not going to be able to do this alone so I turned to the community, I've already researched and I swear I've been trying to fix this for days. Would it be inconvenient of me to ask you to show me how to do this in practice? It would be fundamental, grateful for your attention.
Copy link to clipboard
Copied
For your code it looks something like this:
dlg = new Window("dialog"); dlg.text = "Dialog";
dlg.orientation = "column"; dlg.alignChildren = ["center", "top"];
et1 = dlg.add('edittext {properties: {name: "et1"}}'); et1.text = "Search";
et1.preferredSize.width = 200;
et1.onChanging = function () {
var temp = this.text;
listMD.removeAll();
filteredItems = [];
for (var i = 0; i < listA_carregar.length; i++) {
if (listA_carregar[i].toLowerCase().indexOf(temp) == 0) {
filteredItems.push(i)
listMD.add('item', listA_carregar[i]);
}
}
listMD.selection = 0;
}
var txtlistA = "~/desktop/mylist.txt";
var txtlistA = File(txtlistA);
txtlistA.open('r'); var strtext_A = txtlistA.read();
var listA_carregar = strtext_A.split("\n");
var filteredItems = [];
listA_carregar.pop();
listMD = dlg.add("dropdownlist", undefined, undefined, { name: "listMD", items: listA_carregar });
listMD.selection = 0; listMD.preferredSize.width = 200;
g1 = dlg.add("group", undefined, { name: "g1" });
g1.orientation = "row"; g1.alignChildren = ["left", "center"];
b1 = g1.add("button", undefined, undefined, { name: "b1" }); b1.text = "+";
b2 = g1.add("button", undefined, undefined, { name: "b2" }); b2.text = "-";
b3 = g1.add("button", undefined, undefined, { name: "b3" }); b3.text = "Edit";
b1.onClick = function () { f = 1; presets() }
b2.onClick = function () { remover() }
b3.onClick = function () { f = null; presets() }
dlg.show();
function presets() {
var deg = new Window("dialog"); deg.text = "Prests";
deg.orientation = "row";
var ed1 = deg.add('edittext {properties: {name: "ed1"}}'); ed1.preferredSize.width = 150;
ed1.active = true; if (f == null) { ed1.text = listMD.selection.text }
var b4 = deg.add("button", undefined, undefined, { name: "b4" }); b4.text = "Save";
b4.onClick = function () {
deg.close();
if (f == null) {
listMD.selection.text = ed1.text
listA_carregar[filteredItems.length ? filteredItems[listMD.selection.index] : listMD.selection.index] = ed1.text
}
else {
listMD.add("item", ed1.text);
listMD.selection = listMD.items.length - 1;
if (filteredItems.length) filteredItems.push(listA_carregar.length)
listA_carregar.push(ed1.text);
}
salvar(listA_carregar, listMD.items, txtlistA);
}
deg.show();
}
function remover() {
var sel = listMD.selection.index;
listA_carregar.splice(filteredItems.length ? filteredItems[listMD.selection.index] : listMD.selection.index, 1);
if (filteredItems.length) filteredItems.splice(listMD.selection.index, 1)
listMD.remove(listMD.selection);
if (sel > listMD.items.length - 1) listMD.selection = listMD.items.length - 1;
else listMD.selection = sel;
salvar(listA_carregar, listMD.items, txtlistA);
}
function salvar(ma, mo, f) {
var itensAtuais = ma; if (itensAtuais == null) return;
var out = new File(f); out.open("w"); out.encoding = "UTF8";
for (var a = 0; a < itensAtuais.length; a++) { out.writeln(itensAtuais[a]); } out.close();
};
Copy link to clipboard
Copied
Perfect! @jazz-y , man you are a nice and important guy, saved my project. I have a lot to learn! I would never have achieved this feat alone. Immensely grateful to you my brother. Thanks