3 Replies Latest reply on Jul 17, 2008 9:56 AM by (Lee_Fielding)

    [CS3 JS]  Reading TXT file content into String

    Level 1
      Hello,

      I'm currently wanting to display a dialog box that has a dropdown menu containing all countries of the world.
      I have an external txt file that contains a list of all countries.

      I thought I would simply read-in the contents of the 'txt' file into a string and use it for displaying the list.

      For example
      i Instead of the usual:
      > var myLandMenu = dropdowns.add({stringList:["A", "B"...], selectedIndex:0});


      i I thought of doing something like:
      > var myLandList = ....? HELP ?....

      > var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});

      Is this the way to do it?
      What would be the way to read in the text file content as a string?

      Thanks in advance,
      Lee
        • 1. [CS3 JS] Reading TXT file content into String
          Level 1
          > var myLandList = ....? HELP ?....
          > var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});

          It's hard to tell from context, but myLandList needs to be an array of strings.
          If the file has one element per line, this would be one way of handling the
          conversion:
          var file = File("~/countries.txt");
          file.open("r");
          var str = file.read();
          file.close();

          var myLandList = str.split(/[\r\n]+/);

          And assuming that this is ScriptUI and not the older ID UI, the menu creation
          would look more like:

          var myLandMenu = dropdowns.add(bounds, myLandList);
          myLandMenu.items[0].selected = true;

          -X
          --
          for photoshop scripting solutions of all sorts
          contact: xbytor@gmail.com
          • 2. Re: [CS3 JS]  Reading TXT file content into String
            Level 1
            Thank you very much - I was ablr to pull out of your reply a lot of information.

            At the end I did this:
            > with(dialogColumns.add()){

            var file = File("~/countries.txt");

            file.open("r");
            var str = file.read();
            file.close();
            var myLandList = str.split(/[\r\n]+/);
            var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});
            }

            Then I pulled the selected choice out so:
            > if (myLandMenu.selectedIndex == 0){

            var myLand = "- Not Defined -";
            }else{
            var myLand = myLandList[myLandMenu.selectedIndex];
            }

            It seems all is OK - Thank you very much!
            Regards,
            Lee
            • 3. Re: [CS3 JS]  Reading TXT file content into String
              Level 1
              Hello xbytor,
              I do hope you read this post,

              I have an additional question that has expanded from the script you gave me!

              The situation is:
              The user has already selected the Land in a previous dialog box.
              For arguments sake - lets say the chosen land was
              b Iceland
              position 35 in the Array!

              OK - Now I'd like to set up another dialog box - for when the user wants to make changes!

              If I use the bases of the previous script -
              b how can I make the initialselection
              start at the previously selected land = Iceland?

              It must be easy - it's just that I'm thick!

              Thanks in advance,
              Lee