Copy link to clipboard
Copied
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
when we use the script, it becomes:
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)
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
...Copy link to clipboard
Copied
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();
...
Copy link to clipboard
Copied
Thnkx hans works fine
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Gert,
as you return the index Jump_Over is, of course, right!
Copy link to clipboard
Copied
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 🙂
Copy link to clipboard
Copied
Hi,
What I need to return is three times "YES".
Thanks Jongware:)