Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

dropdown list not sorted

Engaged ,
Jan 09, 2013 Jan 09, 2013

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.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)

TOPICS
Scripting
903
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 09, 2013 Jan 09, 2013

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 fold

...
Translate
Enthusiast ,
Jan 09, 2013 Jan 09, 2013

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.name);

}

master_pages_lijst.sort();

...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 09, 2013 Jan 09, 2013

Thnkx hans works fine

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 09, 2013 Jan 09, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 09, 2013 Jan 09, 2013

Hi Gert,

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2013 Jan 09, 2013

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 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 10, 2013 Jan 10, 2013
LATEST

Hi,

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

Thanks Jongware:)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines