Copy link to clipboard
Copied
//Hi, I have a simple panel which save my field(txt1) into a txt file. I added a new field(txt2) but i dont now how to includ this var to the script. . How to save both form, txt1 and txt2 to data.txt? Thanks
var panel = new Window("dialog", "Form");
var t1 = getData();
var txt1 = panel.add("edittext", undefined, t1);
var t2 = getData();
var txt2 = panel.add("edittext", undefined, t2);
txt1.onChange = function () {
saveData(txt1.text);
}
panel.show();
function getData() {
var _data = '';
var _file = File(Folder.myDocuments + "/data.txt");
if (_file.exists) {
_file.open("r");
_data = _file.read();
}
return _data;
}
function saveData(text) {
var _file = File(Folder.myDocuments + "/data.txt");
_file.open("w");
_file.write(text);
_file.close();
}
Saving both text values as comma separated string is not more elegant but it is simpler. Also, you should consider invoking the saveData function before dismissing the window, there's no point in saving the data each time one of the two text boxes changes.
var panel = new Window("dialog", "Form");
var t = getData().split(",");
var txt1 = panel.add("edittext", undefined, t[0]);
var txt2 = panel.add("edittext", undefined, t[1]);
txt1.onChange = txt2.onChange = function () {
saveData([txt1
...
Copy link to clipboard
Copied
Hi,
There are many ways to do it.
1. You can add event listneer to the second textbox and save the data in the same way as you are doing for first textbox.
2. You can create a single object that will have values for all editboxes and call that method on close or on change of textinput.
Copy link to clipboard
Copied
This is surely an unnecessarily complicated way of doing it, but here you go.
var _file;
_file = File("~/Desktop/text1.txt");
_file.open("e");
var panel = new Window("dialog", "Form");
var t1 = getData().match(/txt1.+/g) || 0;
if (t1) {t1 = t1[t1.length - 1].slice(4);}
var txt1 = panel.add("edittext", undefined, t1);
txt1.name = "txt1";
txt1.onChange = function () {saveData(txt1);}
var t2 = getData().match(/txt2.+/g) || 0;
if (t2) {t2 = t2[t2.length - 1].slice(4);}
var txt2 = panel.add("edittext", undefined, t2);
txt2.name = "txt2";
txt2.onChange = function () {saveData(txt2);}
btn = panel.add("button", undefined, "ok");
panel.show();
function getData() {
_file.seek(0);
return _file.read();
}
function saveData(text) {
_file.seek(0, 2);
_file.writeln(text.name + text.text);
}
_file.close();
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You are not doing anything wrong. If I'm not mistaken, the OP's goal is not to print output, but to store the last inputs between script runs. "txt1" and "txt2" are tags used to retrieve the stored data.
Copy link to clipboard
Copied
This is surely an unnecessarily complicated way of doing it, but here you go.
thank you for yout time
I see it's complicated..for me..
but which is the correct way for dooing that?
after all I just want to memorize the 2 fields.. similar whit rectangle tool from illustrator
Copy link to clipboard
Copied
I would also be interested in seeing a more elegant way of doing it.
Copy link to clipboard
Copied
Saving both text values as comma separated string is not more elegant but it is simpler. Also, you should consider invoking the saveData function before dismissing the window, there's no point in saving the data each time one of the two text boxes changes.
var panel = new Window("dialog", "Form");
var t = getData().split(",");
var txt1 = panel.add("edittext", undefined, t[0]);
var txt2 = panel.add("edittext", undefined, t[1]);
txt1.onChange = txt2.onChange = function () {
saveData([txt1.text, txt2.text].join(","));
}
panel.show();
function getData() {
var _data = '';
var _file = File(Folder.myDocuments + "/data.txt");
if (_file.exists) {
_file.open("r");
_data = _file.read();
}
return _data;
}
function saveData(text) {
var _file = File(Folder.myDocuments + "/data.txt");
_file.open("w");
_file.write(text);
_file.close();
}
Copy link to clipboard
Copied
Simpler is more elegant.