Skip to main content
Liphou
Inspiring
November 25, 2020
Question

[JS] Script optimization / optimisation de Script

  • November 25, 2020
  • 1 reply
  • 680 views

Good morning all, I realize a new sccript which runs on Indesgn 2021 (16), but I find it very lang, I wish to make apple to you to achieve the optimized, for my part I noticed that the script torn very well on the version 2019 and very slowly into 2020 and 2021, I guess I don't have a very good schedule for these versions!

This script is in progress, it is not finished, there are still errors in the senario but it is already doing the basic work.

thank you to all of you

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

Bonjour à tous,

Je réalise un nouveau sccript qui tourne su Indesgn 2021 (16), mais je le trouve très lang, je souhate faire apple à vous pour parvenir à l'optimisé, pour ma part j'ai constaté que des scripte torné très bien sur la version 2019 et très lentement sur 2020 et 2021, je supose que je ne dois pas très bien programmé pour ces version !

Se script est en chantier, il n'est pas fini, il reste des erreure dans le senario mais il fait déjà le travail basique.

 

Merci à vous tous

 

 

 

// ---------------------------------------------
//  Creation d'une ncre de text sur la section active
//  
//  © Philippe Ruelle
//  
//  --------------------------------------------- 

// 1 selection la phrase
// 2 faire une recheche sur les styles bien presi et compare les contenus





// test sur la selection 

if (app.documents.length > 0){ // IF 01 
        
    var laSelection = app.selection[0];
    
       
      
    for (var y = 0; y < laSelection.paragraphs.length; y++) {
            
           
            var newText = laSelection.paragraphs[y].contents.replace(/[^\w]/g,"");
                  
       
             laRecherchez(newText,laSelection.paragraphs[y].appliedParagraphStyle.name,laSelection.paragraphs[y]);
             //laSelection.paragraphs[y].fillColor = "Hyper";
        }

} else {
        alert("Ouvrir un document et selectioné votre text."); 

} // fin du IF 01       

// nettyage

// les fonctions Générals 
// recheche sur les titre 

function laRecherchez(leTitre, leStyle, leDepart) {
    // recheche ou test sur les paragraphe ???? on va test la recheche
       
        // corespondance des Style du plan avec les style de la mise en page  a COMPLETE !!!
        var lesStyle = {'PLAN-a-Section':'Titre Section', 'PLAN-b-Para':'Titre para','PLAN-c-A':'Titre 01 Autre','PLAN-d-i':'Titre 02','PLAN-e-a':'Titre 03','PLAN-f-1':'Titre 04'}; // Problème sur les AT  ex : 'Titre 02 AT'
        var laDestination;
        
       // $.writeln(lesStyle['PLAN-a-Section']);
        
        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
    
        with (app.findGrepPreferences){
            //findWhat = ""; // la commande de la recheche
            //fillColor = "Liens"; // sur la couleur
            appliedParagraphStyle = lesStyle[leStyle];   // sur le stile de paragraphe (voir sur les style de titres)
        } // fin 

        var lesRecherches = app.activeDocument.findGrep();
        //$.writeln('lesRecherche : ' + lesRecherches.length))
        for (var i = 0; i < lesRecherches.length; i++) {
            // ICI faire boucles ... pour tous les para de la selection
            //$.writeln('lesRecherche : ' + lesRecherches[i].contents.replace(/[^\w]/g,""))
            if (lesRecherches[i].contents.replace(/[^\w]/g,"") == leTitre){
                laDestination = laCreationAncre(lesRecherches[i].contents.replace(/[^\w]/g,""), lesRecherches[i]);
                var source = app.activeDocument.hyperlinkTextSources.add(leDepart); /// creation 
                app.activeDocument.hyperlinks.add(source, laDestination).name = leTitre; // problème de titre identique retrava
                i = lesRecherches.length;    
                leDepart.fillColor = "Hyper";
            } else {
                leDepart.fillColor = "DOUTE";
            }
        }
        // test coparatif et creation de l'ancre
}

function laCreationAncre (leName, laSelectionFind) {
   // 
   //   ATTENTION peut-être reduire le nom de hyperlinkTextDestinations et ajouté une numero unique 
   //   
   //   Retourné le nom de l'ancre pour cree le lien
   //
    try {
        if (!app.activeDocument.hyperlinkTextDestinations.itemByName(leName).isValid) {
            var hypTextDest = app.activeDocument.hyperlinkTextDestinations.add(laSelectionFind);
            hypTextDest.name = leName;
        }
    }
    catch(e) {
        $.writeln('erreur Ancre')
    }
    return hypTextDest;    
}

 

 

 

This topic has been closed for replies.

1 reply

brian_p_dts
Community Expert
Community Expert
November 25, 2020

Not exactly sure why you would be experiencing a slowdown between versions, but in general, you want to limit your calls to Regex and the InDesign DOM as much as possible by defining variables outside for loops as much as possible. I also find using GetElements on a collection works much faster. Looking at the top few lines of code, here is how I would optimize: 

 

 

if (app.documents.length > 0){ // IF 01 
        
    var laParagraphs = app.selection[0].paragraphs.everyItem().getElements();
    var numParagraphs = laParagraphs.length;
    var repRegex = /[^\w]/g;
    var newText, y, laPara, applyName;  
    for (y = 0; y < numParagraphs; y++) {
        laPara = laParagraphs[y];   
        newText = laPara.contents.replace(repRegex,"");
        applyName = laPara.appliedParagraphStyle.name;
        laRecherchez(newText, applyName, laPara);
             //laSelection.paragraphs[y].fillColor = "Hyper";
    }

} else {
        alert("Ouvrir un document et selectioné votre text."); 

} // fin du IF 01

 

Liphou
LiphouAuthor
Inspiring
November 28, 2020

Thank you Brianp311,

This is better written, unfortunately it does not solve the problem, my problem must be located in the search with the function with the Find

 

Merci Brianp311,

Cela est meiux écrit, malheureusement ca ne solutione pas le problème, mon problème dois se situé dans le recheche avec la fonction avec le Find

 

 

function laRecherchez(leTitre, leStyle, leDepart) {
        var lesStyle = {'PLAN-a-Section':'Titre Section', 'PLAN-b-Para':'Titre para','PLAN-c-A':'Titre 01 Autre','PLAN-d-i':'Titre 02','PLAN-e-a':'Titre 03','PLAN-f-1':'Titre 04'}; // Problème sur les AT  ex : 'Titre 02 AT'
        
var laDestination;
        
        
        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
    
        with (app.findGrepPreferences){
            appliedParagraphStyle = lesStyle[leStyle];   
        }
        var lesRecherches = app.activeDocument.findGrep();
        for (var i = 0; i < lesRecherches.length; i++) {
            
            if (lesRecherches[i].contents.replace(/[^\w]/g,"") == leTitre){
                laDestination = laCreationAncre(lesRecherches[i].contents.replace(/[^\w]/g,""), lesRecherches[i]);
                var source = app.activeDocument.hyperlinkTextSources.add(leDepart); /// creation 
                app.activeDocument.hyperlinks.add(source, laDestination).name = leTitre; // problème de titre identique a retravallier
                i = lesRecherches.length;    
                leDepart.fillColor = "Hyper";

            } else {
                leDepart.fillColor = "DOUTE";
            }
        
        }

    

 

 

 

I have exactly the same problem in another script, and I located the problem in a "find" function which is 6 sec. on a 2018 Indesign and on a 2021 Indesign ... I had to shut down after 1 hour it moved forward, some of the add of "@@@" ...

and the problem is there:

 

 

J’ai exctement le même problème dans un autre script, et j’ai localisé le problème dans une fonction "find" qui m’est 6 sec. sur un Indesign 2018 et sur un Indesign 2021 ... j'ai du l’arreté après 1 heure il avanit fait une partie des ajoute de "@@@"... 

 

et le problème est là :

 

 

 

function notesVersBlocs() {
   
               //
$.writeln("notesVersBlocs" + logHeure () );
       var monDoc = app.activeDocument;
// traiter tous les articles
        for( var i = 0; i < monDoc.stories.length; i++ ) {
        // obtenir une référence aux notes d’un article
                var mesNotes = monDoc.stories[i].footnotes;
                //$.writeln(mesNotes.length);
                for( var j = 0; j < mesNotes.length; j++ ) {
                    
                    if (mesNotes[j].paragraphs.length == 1){
                        mesNotes[j].insertionPoints[-1].contents = '@@@';
                    }  
                }
            }
}

 

 

 

Liphou
LiphouAuthor
Inspiring
November 28, 2020

I think I am closer to a problem of refreshing or placing texts by indesign, than a problem with the script !! (???)

 

Je pense être plus proche d'un problème de rafraichissement ou de placement de textes par indesign, que d'un problème de script !!(???)