10 Replies Latest reply on Jul 18, 2008 6:53 AM by (Lee_Fielding)

    [CS3 JS]  Predefining the selectedIndex in a DropDown menu

    Level 1
      Hello wise-ones,

      My situation is such that I have a dialogbox that allows the user to select a country of the world via a dropdown menu:
      > 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});
      }

      The choice is stored:
      > if (myLandMenu.selectedIndex == 0){
      var myLand = "- Not Defined -";
      }else{
      var myLand = myLandList[myLandMenu.selectedIndex];
      }

      Now - my problem..........
      At some stage - it could be that the user whishes to make changes.
      My stumbling stone is that I'd like the dropdown list to display the
      b previously selected country
      as the
      b selectedIndex
      !

      I'm sure it must be simple -
      simply search for
      i myLand
      in the
      i myLandList
      and then set the
      i selectedIndex
      as the
      i position
      of myLand within the myLandList Array....
      Sounds easy when I tell that myself - but it's got me cornered and stuck!
      Can anyone please help me - point me in the right direction?

      Thank you in advance,
      Lee
        • 1. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
          Peter Kahrel Adobe Community Professional & MVP
          Do you mean the previous time the script was run?

          Peter
          • 2. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
            Level 1
            Insert a label somewhere. For example, you might want to have the dialog sensitive to the active document, so you could have a document label that you seek out and set:

            If (app.documents[0].extractLabel("Selected Country") == "") {
            // document doesn't contain a previous selected country

            } else {
            // it does so use it

            }

            Then, on receiving an OK from the user after the dialog, you can set the new value:

            app.documents[0].insertLabel("Selected Country", myLandList[myLandMenu.selectedIndex]);

            If you use the actual name of the country rather than its index in the list, it would allow for adding new countries at some point in the future, but if you do that, you'll need iterate through the list to get the index on the way in.

            If you just want a single value across all documents, do the same thing but insert the label in the application object.

            Dave
            • 3. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
              Level 1
              Thanks,
              OK....
              Peter:
              > Yes - The land was chosen from a script that was run at an earlier time/day/month....

              Dave:
              > Sorry to say - but I'm a little confused from your reply! I'm not such an expert at this JavaScripting stuff!
              Actually I have a list with all countries of the world. The list must not be edited.
              I'm inexperienced with 'Labeling' if you can believe me?

              The bigger picture:
              The user has set up a document with aid of a 'creation' script - where they chose the Land and entered certain other values.

              Then - it could be that the user wants to update the document using a 'modification' script, and changes values.
              Now I would like to display the Land dopdown list again - BUT - have the previously chosen land as the initially selected option and not simply
              b selectedIndex:0

              Did this help - or maybe you knew it anyways, and I'm the confused one! :)
              Sorry if I anoy you as I don't know much about Label thingy?

              Regards,
              Lee
              • 4. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
                Peter Kahrel Adobe Community Professional & MVP
                Many objects in InDesign have a label. document is one. You could do e.g.
                >app.activeDocument.label = 'Honduras';

                to set the document's label to Honduras. You could then later retrieve this using this statement:
                >myLand = app.activeDocument.label;

                You then need to determine Honduras's index in the list and you can preselect Honduras.

                Peter
                • 5. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
                  Level 1
                  Well, Peter and I are both pointing you in the same direction. I'd use a keyed label rather than the single label property because it makes sure that you don't accidentally use the same label for more than one thing.

                  I practically wrote the code for you, so I'm not sure what you're missing.

                  Dave
                  • 6. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
                    Level 1
                    Thanks Peter, Thanks Dave,

                    OK - I actually have a characterStyle that I read in to see what the langauge was
                    i myCurrentLand
                    - so there's no problem finding the name of the land:
                    > app.findTextPreferences.appliedCharacterStyle = myLandCharStyleName;
                    f = myDoc.findText();
                    if (f[0].contents == "- Not Defined" | f[0].contents == ""){
                    var myCurrentLand = "- Not Defined -";
                    }else{
                    var myCurrentLand = f[0].contents;
                    }

                    My problem is that - what Peter said....
                    i You then need to determine Honduras's index in the list and you can preselect Honduras.

                    I don't know how to find
                    i myCurrentLand
                    's index in my
                    i myLandList
                    string!
                    :(

                    I've just made a 'work around' where myCurrentLand is inserted into the top of myLandList:
                    > with(dialogColumns.add()){
                    var file = File("~/countries.txt");
                    file.open("r");
                    var str = file.read();
                    file.close();
                    str = myCurrentLand+"\r---//---\r"+str;
                    var myLandList = str.split(/[\r\n]+/);
                    var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:0});
                    }
                    • 7. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
                      Level 1
                      function getIndex (myObj, myList) {
                      for (var j = myList.length - 1; j >= 0; j--) {
                      if (myObj -- myList[j]) return j
                      }
                      return -1
                      }

                      I just wrote that here, so there might be a minor syntax error.

                      Dave
                      • 8. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
                        Level 1
                        :) :) :)

                        Thank you Dave - Thank you very much.

                        My final script was:
                        > for (var j = myLandList.length - 1; j >= 0; j--) {

                        if (myCurrentLand == myLandList[j]) {
                        myLandIndex = j;
                        }
                        }
                        var myLandMenu = dropdowns.add({stringList:myLandList, selectedIndex:myLandIndex});

                        Works wonderfully!
                        It just goes to show that my boss should allow me to do the basic JavaScripting course....
                        Thanks once again.
                        Lee
                        • 9. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
                          Level 1
                          As long as you're confident that myCurrentLand is guaranteed to be in the list you're fine. I think I'd do this:

                          for (var myLandIndex = myLandList.length - 1; myLandIndex >= 0; myLandIndex--) {
                          if (myCurrentLand == myLandList[myLandIndex]) break;
                          }
                          }

                          etc.

                          This way, you're absolutely guaranteed that myLandIndex actually has a value when you come to use it and you don't go around the loop any more times than is absolutely necessary.

                          Dave
                          • 10. Re: [CS3 JS]  Predefining the selectedIndex in a DropDown menu
                            Level 1
                            :)
                            Keeping it simpler - the better!

                            Something else I've learnt!
                            Thanks you.