3 Replies Latest reply on Oct 17, 2014 7:23 AM by creativejoan0425

    About Arrary

    creativejoan0425 Level 1

      Hi expert,

       

      if I alert(i), the result is myArrayloop1 , myArrayloop1 , myArrayloop1 , but I want the result is A, B, C

       

      below is my Script:

       

      var myArray1 = ["A", "B", "C"];

      var myArray2 = ["1", "2", "3"];

       

      var myArray1Split = myArray1.toString().split(",");

      var myArray2Split = myArray2.toString().split(",");

       

      for (j = 0; j < myArray1Split.length; j++) {

      var myArrayloop1 = myArray1Split[j];

      var myArrayloop2 = myArray2Split[j];

       

       

      var myText = {myArrayloop1 : myArrayloop2};

      for (var i in myText){

      alert(i);

      alert(myText[i]);

      }

      }

        • 1. Re: About Arrary
          Jump_Over Level 5

          Hi,

           

          Exam my description, pls:

          var myArray1 = ["A", "B", "C"];
          var myArray2 = ["1", "2", "3"];
          
          // here script goes from array to string (i.e. "A,B,C") and back to array (i.e. ["A", "B", "C"] )
          var myArray1Split = myArray1.toString().split(",");
          var myArray2Split = myArray2.toString().split(",");
          
          for (j = 0; j < myArray1Split.length; j++) {
          // here particular element of array1 (myArray1Split) is assigned to a variable "myArrayloop1"
          var myArrayloop1 = myArray1Split[j];
          // here particular element of array2 (myArray2Split) is assigned to a variable "myArrayloop2"
          var myArrayloop2 = myArray2Split[j];
          
          // here a new object is created with property named "myArrayloop1"  (casual coincidence?) and value set to current value of  variable "myArrayloop2"
          var myText = {myArrayloop1 : myArrayloop2};
          for (var i in myText){ // here starts a loop for each property of object "Text" ==> there is only one
          alert(i); // here the name of property is alerted
          alert(myText[i]); // here the value of property is alerted
          }
          }
          
          
          

           

          What is a goal of creating an object "Text"?

          What result are you going to reach?

          For now it looks like the code is running the redundant circles.

           

          Jarek

          • 2. Re: About Arrary
            pixxxel schubser Level 5

            creativejoan0425,

            why so complicated?

            var myArray1 = ["A", "B", "C"];
            var myArray2 = ["1", "2", "3"];
            
            for (j = 0; j < myArray1.length; j++) {
                var myArrayloop1 = myArray1[j];
                var myArrayloop2 = myArray2[j];
              
                var myText = {myArrayloop1 : myArrayloop2};
                for (var i in myText){
                    // here is the name of nested Array - gives you at first "A", second "B" and third "C"
                    alert(myArrayloop1);
                    // here is the value of nested Array - gives you at first "1", second "2" and third "3"
                    alert(myText[i]);
                    }
                }
            
            
            

             

            But one note: IMHO this way is not the best way. Because of, outside the loop your variable myText has only stored the last values "C" and "3".

             

            Have fun

             

            • 3. Re: About Arrary
              creativejoan0425 Level 1

              Hi Jarek,

               

              I have 10 indd files called 1.indd, 2.indd, 3.indd, 4.indd, 5.indd, 6.idd, 7.indd, 8.indd, 9.indd, 10.indd

              and each file have different pages 1.indd (1 page), 2.indd (2 pages), 3.indd (3 pages), 4.indd (4 pages), 5.indd (5 pages), 6.idd (6 pages), 7.indd (7 pages), 8.indd (8 pages), 9.indd (9 pages), 10.indd (10 pages)

               

              and I open those 10 files and run the script

               

              For Row1 editcontent, I change 9 to 20 and Row 2 change from 8 to 21

              Only Row 1, 3, 5 Checkbox value is true

               

              so If I alert all editcontent it should be 20, 21, 7, 6, 5, 4, 3, 2, 10, 1

              but the result is 9, 8, 7, 6, 5, 4, 3, 2, 10,1

               

              and for the Checkbox value, the result should be true, false, false, true, true, false, false, false, false, false

              but now the result true, false, false, true, true


              And I found that when I change the row 1,  9 to 20 and then I use the scrollbar to scroll down and then scroll up , 20 will change back to 9, can make it when I scroll back the result is 20?


              var DocArray = app.documents.everyItem().name;

              var myPrintFiles =[];

              var myPagesLength =[];

               

               

              for (i = 0; i < DocArray.length; i++){

                                              myPrintFiles.push(app.documents.itemByName(DocArray[i]).name);

                                              myPagesLength.push(app.documents.itemByName(DocArray[i]).pages.length);

                                      }

              var call_codes = {myPrintFiles: myPagesLength};

              var rows = 5, number_of_codes = DocArray.length;

              var w = new Window ("dialog");

              var group = w.add ("group {alignChildren: 'fill'}");

              var panel = group.add ("panel {orientation: 'row', alignChildren: 'top'}");

              var sbar = group.add ("scrollbar {preferredSize: [20, undefined], maxvalue: " + String (number_of_codes-rows) + "}");

              var col1 = panel.add ("group {orientation: 'column', margins: 3}");

              var col2 = panel.add ("group {orientation: 'column', alignChildren: 'fill', preferredSize: [150, undefined]}");

              var fields = {}, start = 0, stop = rows, n = -1, myCheck = {};

               

               

                       var myPrintFilesJoin =  myPrintFiles.join("\\r");

                                  var myPrintFilesSplit =  myPrintFilesJoin.split("\\r");

                                        for (j = 0; j < myPrintFilesSplit.length; j++) {

                                                 myEachEditContent = myPrintFilesSplit[j];

               

               

              for (var i in call_codes){

              ++n;

              if (n >= start && n < stop){

              myCheck[n]  =col1.add ("checkbox", [0, 0, 300, 22], myPrintFilesSplit[j]);

              fields[n] = col2.add ("edittext", undefined, myPagesLength[j].toString().split(","));

              fields[n].label = i; // Add a label property that contains the letter

              fields[n].onChange = function () {call_codes[this.label] = this.text}

              } // When the edittext is changed, update the call_codes object

              }

              }

              sbar.onChanging = function()

              {

              var start = Math.round (this.value); // Round the sbar's value

              var stop = start+rows, n = 0, r = -1;

              var myPrintFilesSplit =  myPrintFilesJoin.split("\\r");

              for (j = 0; j < myPrintFilesSplit.length; j++) {

               

               

              //~ for (var i in call_codes)

              //~ {

              n++;

               

               

              if (n > start && n <= stop)

              {

              ++r;

              col1.children[r].text = myPrintFilesSplit[j]; // Replace the contents of the statictext

              fields[r].text = myPagesLength[j].toString().split(","); // and the edittext controls

              fields[r].label = i; // and update each edittext's label

               

               

              }

              }

              }

               

               

              w.show();

              for (j = 0; j < 10; j++) {

              alert(myPagesLength[j].toString().split(","));

              }

              for (j = 0; j < 10; j++) {

              alert(col1.children[j].value);

              }

               

              In fact the upper script is base on below sample to change

               

               

              var call_codes = {"A": "Alpha", "B": "Bravo", "C": "Charlie", "D": "Delta", "E": "Echo", "F": "Foxtrot", "G": "Golf", "H": "Hotel", "I": "India",

              "J": "Juliet", "K": "Kilo", "L": "Lima", "M": "Mike", "N": "November", "O": "Oscar", "P": "Papa", "Q": "Quebec", "R": "Romeo", "S": "Sierra", "T":

              "Tango", "U": "Uniform", "V": "Victor", "W": "Whiskey", "X": "X-ray", "Y": "Yankee", "Z": "Zulu"};

              var rows = 14, number_of_codes = 0;

              for (var i in call_codes) number_of_codes++; // We need to know how many elements the object contains

              var w = new Window ("dialog");

              var group = w.add ("group {alignChildren: 'fill'}");

              var panel = group.add ("panel {orientation: 'row', alignChildren: 'top'}");

              var sbar = group.add ("scrollbar {preferredSize: [20, undefined], maxvalue: " + String (number_of_codes-rows) + "}");

              var col1 = panel.add ("group {orientation: 'column', margins: 3}");

              var col2 = panel.add ("group {orientation: 'column', alignChildren: 'fill', preferredSize: [150, undefined]}");

              var fields = {}, start = 0, stop = rows, n = -1;

              for (var i in call_codes){

              ++n;

              if (n >= start && n < stop){

              col1.add ("statictext", [0, 0, 20, 20], i);

              fields[n] = col2.add ("edittext", undefined, call_codes[i]);

              fields[n].label = i; // Add a label property that contains the letter

              fields[n].onChange = function () {call_codes[this.label] = this.text}

              } // When the edittext is changed, update the call_codes object

              }

              sbar.onChanging = function()

              {

              var start = Math.round (this.value); // Round the sbar's value

              var stop = start+rows, n = 0, r = -1;

              for (var i in call_codes)

              {

              n++;

              if (n > start && n <= stop)

              {

              ++r;

              col1.children[r].text = i; // Replace the contents of the statictext

              fields[r].text = call_codes[i]; // and the edittext controls

              fields[r].label = i; // and update each edittext's label

              }

              }

              }

              w.show();