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

UI save form to txt

Explorer ,
Oct 18, 2021 Oct 18, 2021

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

[3]10-18-2021.jpg

 

 

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

 

 

 

TOPICS
Scripting

Views

383

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

Community Expert , Oct 19, 2021 Oct 19, 2021

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
...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 18, 2021 Oct 18, 2021

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.

Best regards

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 ,
Oct 18, 2021 Oct 18, 2021

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

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
Participant ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

@femkeblanco  your script returns

GerssonDelgado_0-1634580086146.png

GerssonDelgado_1-1634580112175.png

when it should returns

GerssonDelgado_2-1634580221608.png

 

let me know if Im doing something wrong! 

 

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 ,
Oct 18, 2021 Oct 18, 2021

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. 

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
Explorer ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

quote

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

Urraco37_0-1634633379667.png

 

 

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 ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

I would also be interested in seeing a more elegant way of doing it. 

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
Community Expert ,
Oct 19, 2021 Oct 19, 2021

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

 

 

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 ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

LATEST

Simpler is more elegant. 

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