6 Replies Latest reply on Jan 10, 2013 3:22 AM by Jump_Over

    dropdown list not sorted

    gert verrept Level 2

      We use a script to open specific templates. The script looks at a specific folder and displays a list of all files ending on "INDT". Displaying the folder in explorer, we get this

      ScreenShot1.jpgwhen we use the script, it becomes:

      ScreenShot.jpg

      Next is part of the script used:

       

      var ini_bestand = File((Folder(app.activeScript)).parent + "/mstr.txt"); //--------mstr.txt=where is folder located

       

      initialiseer_map();

      lees_mapnaam_uit();

       

      var masterpages = Folder(folder_masters).getFiles("*.indt");//---------all templates have 3 pages, when opening template, add 25 pages and link textframes

      var aantal_masters = masterpages.length;

      var aantal_paginas = 25;

       

      var master_pages_lijst = new Array;

      for (i=0; i<aantal_masters; i++) {

                master_pages_lijst.push(masterpages[i].name);

      }

       

      //menu laten zien

      var stramien_pagina_menu = stramienpagina();

       

       

      //stramienpagina openen

      if (stramien_pagina_menu.show()){

       

                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;

                app.open(masterpages[stramien_lijst.selectedIndex]);

                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

       

                pagina_aantal = app.activeDocument.pages.length; //-------when we select a template 25 pages are added

       

                if (pagina_aantal > 1) {

                          if (masterpages[stramien_lijst.selectedIndex] != "/p/20_DOCUMENTS/MASTERS/MSTR_PRC.indt") {

                          if (masterpages[stramien_lijst.selectedIndex] != "/p/20_DOCUMENTS/MASTERS/MSTR_PRC_TRAITE.indt") {

                          if (masterpages[stramien_lijst.selectedIndex] != "/p/20_DOCUMENTS/MASTERS/MSTR_PRC_NATUUR.indt") {

                          voeg_paginas_toe();

                          link_tekstframes();

                          }

                }

                }

          }

       

                app.activeWindow.activePage = app.activeDocument.pages.item(0);

       

      }

       

      //--------------------------------------------------------------------

      //functies------------------------------------------------------------

       

       

      function stramienpagina(){

      var stramien = app.dialogs.add({name:"Master document", canCancel:true} );

                with (stramien){

       

                          with (dialogColumns.add() ){

       

                                    //tekstregel plaatsen

                                    with (dialogRows.add() ){

                                                        staticTexts.add({staticLabel:"Select the master needed!                 "} );

                                    }

                                    with (dialogRows.add() ){

                                                        staticTexts.add({staticLabel:""} );

                                    }

       

                                    //dropdowns plaatsen

                                    with (dialogRows.add() ){

       

                                    stramien_lijst = dropdowns.add({stringList:master_pages_lijst,selectedIndex:0}); //-------this list isn't sorted

       

       

                                    }

                           }

                }

                return stramien;

      }

       

      the rest of the script isn't of any use, it links the frames on the template etc.

       

      All suggestions are welcome. (We use cs6 on win 7 pro 64bit)

        • 1. Re: dropdown list not sorted
          -hans- Level 4

          simple JS-sort inserted anywhere should be enough

          ...

          var master_pages_lijst = new Array;

          for (i=0; i<aantal_masters; i++) {

                    master_pages_lijst.push(masterpages[i].name);

          }

          master_pages_lijst.sort();

          ...

          • 2. Re: dropdown list not sorted
            gert verrept Level 2

            Thnkx hans works fine

            • 3. Re: dropdown list not sorted
              Jump_Over Level 5

              Hi,

               

              Sorting a list should help but notice that only array with names is sorted (dialog dropdown list).

              So stramien_lijst.selectedIndex is not an index of your chosen masterFile anymore.

               

              Since getFiles() gives you an array of files ==> sort this  before you create an array with names.

              This way you share an index in both arrays.

               

              To sort an array of files by their names you could use a function:

               

              function sortNames(file1,file2) {

              return (file1.name>file2.name);

              }

               

              and in your code:

               

              masterpages.sort(sortNames)     // here your array with files supposes to be sorted

               

              rgds

              • 4. Re: dropdown list not sorted
                -hans- Level 4

                Hi Gert,

                 

                as you return the index Jump_Over is, of course, right!

                • 5. Re: dropdown list not sorted
                  [Jongware] Most Valuable Participant

                  For proper execution, I think this would need to be changed:

                   

                  function sortNames(file1,file2) {

                  return (file1.name>file2.name);

                  }

                   

                  It's a common misconception that sort needs to return 'true' or 'false'. Actually, the sort function needs to be able to return *three* values: Smaller, Same, and Greater, indicated by a negative number, zero, and a positive number. (Although in fairness it's unlikely you'd find two files with a "same" filename -- nevertheless, you might if you read files from several folders.)

                   

                  The proper function to use is localeCompare, and it returns the 'proper' comparison right away:

                   

                  return file1.name.localeCompare(file2.name);

                   

                  By way of bonus, it'll also sort accented characters correctly :-)

                  • 6. Re: dropdown list not sorted
                    Jump_Over Level 5

                    Hi,

                    What I need to return is three times "YES".

                    Thanks Jongware:)