5 Replies Latest reply on Jun 29, 2010 10:59 AM by jmw107

    Reading tab delimited data from a text file (JS CS3)

    jmw107 Level 1

      Hi --I am working on a script to read data from a text file and use within a dialog. But I have hit a wall.

       

      I have used a script from a previous post that sets a variable from the text document when the script user chooses it from a dropdown.

       

      var myDialog = app.dialogs.add({name:"Map",canCancel:true});
      with(myDialog){

       

          with(dialogColumns.add()){
              with(borderPanels.add()){
      staticTexts.add({staticLabel:"Choose location:"});
      with(dialogColumns.add()){
         
      var file = File("~/Desktop/myPlacesfile.txt");

       

      file.open("r");
      var str = file.read();
      file.close();
      var myPlaceList = str.split(/[\r\n]+/);  
      var myPlaceMenu = dropdowns.add({stringList:myPlaceList, selectedIndex:0});
      }
      }}}
      var myResult = myDialog.show();
      if(myResult == true){
      if (myPlaceMenu.selectedIndex == 0){
      var myPlace = "- Not Defined -";
      }else{
      var myPlace = myPlaceList[myPlaceMenu.selectedIndex];
      alert(myPlace);
      }

       

      myDialog.destroy();
      }

       

      This is what I need to do now:

       

      The text file is in this format:

       

      value1 [TAB] value2

      value1 [TAB] value2

      value1 [TAB] value2

       

      I need to have the dialog pulldown only show value 1, and after the user selects it, the script returns only value 2. (The alert is just there for testing -- I am doing something else with the variable).

       

      Is there a way to display the first part of a tab delimited line in the pulldown and return the second half as a variable?

       

      Any help would be greatly appreciated.

       

      thanks

        • 1. Re: Reading tab delimited data from a text file (JS CS3)
          Peter Kahrel Adobe Community Professional & MVP

          One of the possibilities is this. Create an array with values left of the tab for the dialog. Then create an object which you use as a look-up table. Roughly as follows:

           

           

          // same as what you have

          file.open("r");
          var str = file.read();
          file.close();
          var array = str.split(/[\r\n]+/);

           

           

          // "pairs" is the look-up table

           

          var pairs = {};

          // as before, "myPlaceList" will be used for the dropdown
          var myPlaceList = [];

           

           

           

          var v;
          for (var i = 0; i < array.length; i++)
            {
            v = array[i].split ("\t");
            pairs [v[0]] = v[1];
            myPlaceList.push (v[0]);
            }

           

           

          // add the dropdown as before:


          var myPlaceMenu = dropdowns.add({stringList:myPlaceList, selectedIndex:0});

           

           

          // the look-up table "pairs" is used as follows: pairs ["value1"] returns "value2"

          // so in your script that would be:

           

           

          var myPlace = pairs [myPlaceList[myPlaceMenu.selectedIndex]];

           

          Peter

          • 2. Re: Reading tab delimited data from a text file (JS CS3)
            Harbs. Level 6

            Very nice!

             

            I didn't know you can use a RegExp as an argument for split()!

             

            Very good to know!

             

            Harbs

            • 3. Re: Reading tab delimited data from a text file (JS CS3)
              jmw107 Level 1

              Thanks Peter -- that worked great.

               

              Terrific solution.

               

              Jim

              • 4. Re: Reading tab delimited data from a text file (JS CS3)
                jmw107 Level 1

                I was hoping I could prevail on you for one more issue:

                 

                I am now adapting this script into scriptUI.

                 

                Here's a snippet:

                 

                //This correctly displays my drop down list.

                 

                myDialog.myPlaceMenu = myPanel.add('dropdownlist', [230,58,630,78], myPlaceList);
                myDialog.myPlaceMenu.items[0].selected = true;

                 

                //This is where I run into a problem. The 'if' seems to be doing what is is supposed to but I cannot figure out the correct wording in the "else" part for the myPlace variable when the user chooses something other than the first item.

                 

                if (myDialog.myPlaceMenu.items[0].selected == true){

                 

                var myPlace = "- Not Defined -";
                }else{
                var myPlace = pairs [myPlaceList[myDialog.myPlaceMenu.selected]];
                alert (myPlace);
                }

                 

                Any help would be greatly appreciated.

                 

                thanks

                • 5. Re: Reading tab delimited data from a text file (JS CS3)
                  jmw107 Level 1

                  Hi -- Nevermind -- I figured it out (found an earlier post where you answered the same question).

                   

                   

                  var myPlace = pairs [myPlaceList[myDialog.myPlaceMenu.selection.index]];

                   

                  thanks again,

                   

                  Jim