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

Retrieve elements from an array

Engaged ,
Jul 30, 2022 Jul 30, 2022

Copy link to clipboard

Copied

Hello friends, I'm new to scripting and I'm studying arrays and I don't know how to export all the items in an array to an external .txt file so that I can reload them again whenever I open the dialog box and add new items.

 

dialog = new Window("dialog"); 
dialog.text = "Dialog"; 
dialog.preferredSize.width = 300; 

array=[];
st1 = dialog.add("statictext", undefined, undefined, {name: "st1"}); 
st1.text = "text"; 
st1.preferredSize.width = 100; 
st1.justify = "center"; 

et1 = dialog.add('edittext {properties: {name: "et1"}}');  et1.text = ""; 
et1.preferredSize.width = 60;
bt = dialog.add("button", undefined, undefined, {name: "bt"}); bt.text = "add"; 

bt.onClick = function(){
    n = et1.text;
    array.push([n]);
    st1.text = array;
    et1.text = "";
};  

dialog.show();

 

TOPICS
Actions and scripting

Views

413

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 , Jul 31, 2022 Jul 31, 2022

Are you trying to achieve something like this? 

var theFile = String($.fileName);
var thePath = theFile.slice(0, theFile.lastIndexOf(".")) + "_pref.txt";
if (File(thePath).exists == true) {var theArray = readPref (thePath)}
else {var theArray = new Array};

dialog = new Window("dialog"); 
dialog.text = "Dialog"; 
dialog.preferredSize.width = 300; 

array=[];
st1 = dialog.add("statictext", undefined, undefined, {name: "st1"}); 
st1.text = "text"; 
st1.preferredSize.width = 100; 
st1.justify = "ce
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 31, 2022 Jul 31, 2022

Copy link to clipboard

Copied

You can save this code as a jsx and on running it should create a txt-file beside the jsx and »read it back« to you.  

I hope that demonstrates the process. 

var theFile = String($.fileName);
var thePath = theFile.slice(0, theFile.lastIndexOf(".")) + "_pref.txt";
var theArray = ["aaa", "bbb", "ccc", "111", "222", "333"];
writePref(theArray, thePath);
var theArray2 = readPref (thePath);
alert (theArray2.join("\n"));
////// read prefs file //////
function readPref (thePath) {
  if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = file.read();
    file.close();
    return String(theText).split(",")
    }
  };
////// function to write a preference-file storing a text //////
function writePref (theArray, thePath) {
  try {
    var thePrefFile = new File(thePath);
    thePrefFile.open("w");
    thePrefFile.write(theArray.join(","));
    thePrefFile.close()
    }
  catch (e) {};
  };

 

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
Engaged ,
Jul 31, 2022 Jul 31, 2022

Copy link to clipboard

Copied

Hi @c.pfaffenbichler , thanks for sharing your knowledge, your script is very interesting, but I am not able to add it to my script. What am I doing wrong? Thanks.

 

dialog = new Window("dialog"); 
dialog.text = "Dialog"; 
dialog.preferredSize.width = 300; 

array=[];
st1 = dialog.add("statictext", undefined, undefined, {name: "st1"}); 
st1.text = "text"; 
st1.preferredSize.width = 100; 
st1.justify = "center"; 

et1 = dialog.add('edittext {properties: {name: "et1"}}');  et1.text = ""; 
et1.preferredSize.width = 60;
bt = dialog.add("button", undefined, undefined, {name: "bt"}); bt.text = "add"; 

bt.onClick = function(){
   theArray();
};  

dialog.show();

function teste(){
    var theFile = String($.fileName);
    var thePath = theFile.slice(0, theFile.lastIndexOf(".")) + "_pref.txt";
    var theArray = [/*"aaa", "bbb", "ccc", "111", "222", "333"*/];
    writePref(theArray, thePath);
    var theArray2 = readPref (thePath);
    alert (theArray2.join("\n"));

    ////// read prefs file //////
    function readPref (thePath) {
      if (File(thePath).exists == true) {
        var file = File(thePath);
        file.open("r");
        file.encoding= 'BINARY';
        var theText = file.read();
        file.close();
        return String(theText).split(",")
        }
      };
  
    ns = et1.text;
    theArray.push([ns]);
    st1.text = theArray;
    et1.text = "";
  
    ////// function to write a preference-file storing a text //////
    function writePref (theArray, thePath) {
      try {
        var thePrefFile = new File(thePath);
        thePrefFile.open("w");
        thePrefFile.write(theArray.join(","));
        thePrefFile.close()
        }
      catch (e) {};
      };

}

 

 

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 ,
Jul 31, 2022 Jul 31, 2022

Copy link to clipboard

Copied

What is the Script supposed to do? 

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
Engaged ,
Jul 31, 2022 Jul 31, 2022

Copy link to clipboard

Copied

The objective is to save all the items in the array in memory, in the case of a .txt file, and I add new ones through the text box, so they are saved and reloaded whenever I open the dialog box again.

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 ,
Jul 31, 2022 Jul 31, 2022

Copy link to clipboard

Copied

Are you trying to achieve something like this? 

var theFile = String($.fileName);
var thePath = theFile.slice(0, theFile.lastIndexOf(".")) + "_pref.txt";
if (File(thePath).exists == true) {var theArray = readPref (thePath)}
else {var theArray = new Array};

dialog = new Window("dialog"); 
dialog.text = "Dialog"; 
dialog.preferredSize.width = 300; 

array=[];
st1 = dialog.add("statictext", undefined, undefined, {name: "st1"}); 
st1.text = "text"; 
st1.preferredSize.width = 100; 
st1.justify = "center";

et1 = dialog.add('edittext {properties: {name: "et1"}}');  et1.text = ""; 
et1.preferredSize.width = 60;
bt = dialog.add("button", undefined, undefined, {name: "bt"}); bt.text = "add"; 

bt.onClick = function(){
   theArray.push(et1.text);
   writePref(theArray, thePath);
};  

dialog.show();

var theArray2 = readPref (thePath);
alert (theArray2.join("\n"));

////// read prefs file //////
function readPref (thePath) {
    if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = file.read();
    file.close();
    return String(theText).split(",")
    }
    };

ns = et1.text;
theArray.push([ns]);
st1.text = theArray;
et1.text = "";

////// function to write a preference-file storing a text //////
function writePref (theArray, thePath) {
    try {
    var thePrefFile = new File(thePath);
    thePrefFile.open("w");
    thePrefFile.write(theArray.join(","));
    thePrefFile.close()
    }
    catch (e) {};
    };

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
Engaged ,
Jul 31, 2022 Jul 31, 2022

Copy link to clipboard

Copied

@c.pfaffenbichler  have you tested it? how do you work? I tested it here and nothing happens when I add the text and click the "add" button

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
Engaged ,
Jul 31, 2022 Jul 31, 2022

Copy link to clipboard

Copied

LATEST

@c.pfaffenbichler  sorry for my mistake! I had tested your script without saving. The text file is getting all the added items, I thought it was not working because the static text in the dialog doesn't update with all the elements. 

Fused very well now! Thanks for your wonderful script!
Here it is just the way I need it.

var theFile = String($.fileName);
var thePath = theFile.slice(0, theFile.lastIndexOf(".")) + "_pref.txt";
if (File(thePath).exists == true) {var theArray = readPref (thePath)}
else {var theArray = new Array};

dialog = new Window("dialog"); 
dialog.text = "Dialog"; 
dialog.preferredSize.width = 600; 

array=[];
st1 = dialog.add("statictext", undefined, undefined, {name: "st1"}); 
st1.text = readPref (thePath);
st1.preferredSize.width = 300; 
st1.justify = "center";

et1 = dialog.add('edittext {properties: {name: "et1"}}');  et1.text = ""; 
et1.preferredSize.width = 100;
bt = dialog.add("button", undefined, undefined, {name: "bt"}); bt.text = "add"; 

bt.onClick = function(){
   theArray.push(et1.text);
   writePref(theArray, thePath);
   st1.text = readPref (thePath)
};  

dialog.show();

var theArray2 = readPref (thePath);
alert (theArray2.join("\n"));

////// read prefs file //////
function readPref (thePath) {
    if (File(thePath).exists == true) {
    var file = File(thePath);
    file.open("r");
    file.encoding= 'BINARY';
    var theText = file.read();
    file.close();
    return String(theText).split(",")
    }
    };

////// function to write a preference-file storing a text //////
function writePref (theArray, thePath) {
    try {
    var thePrefFile = new File(thePath);
    thePrefFile.open("w");
    thePrefFile.write(theArray.join(","));
    thePrefFile.close()
    }
    catch (e) {};
    };

 

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