Skip to main content
smithcgl9043167
Inspiring
July 31, 2022
Answered

Retrieve elements from an array

  • July 31, 2022
  • 2 replies
  • 761 views

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

 

This topic has been closed for replies.
Correct answer c.pfaffenbichler

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

2 replies

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
July 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 = "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) {};
    };
smithcgl9043167
Inspiring
July 31, 2022

@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

smithcgl9043167
Inspiring
July 31, 2022

@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) {};
    };

 

c.pfaffenbichler
Community Expert
Community Expert
July 31, 2022

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

 

smithcgl9043167
Inspiring
July 31, 2022

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

}

 

 

c.pfaffenbichler
Community Expert
Community Expert
July 31, 2022

What is the Script supposed to do?