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

working script - too slow

Engaged ,
Sep 16, 2021 Sep 16, 2021

Copy link to clipboard

Copied

Hi.

The following script works fine. What bothers me is the relatively long running time. Processing is too slow. Can one optimize something about it without rewriting the whole thing? At beginner level.

It changes textfields and its tables based on input through a dialog.

 

app.doScript(funktionAusführen, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Funktionsprozess");
 
 function funktionAusführen () {

    var myDoc = app.activeDocument;
    var allSelection = myDoc.selection;
        if(allSelection.length == "0") {alert("Bitte mindesten ein Textfeld auswählen!"); exit();} 

    var allSelection = app.selection,
    runs = allSelection.length,
    osGrArtikel = myDoc.objectStyleGroups.itemByName('SF_Artikel'),
    psGrArtikel = myDoc.paragraphStyleGroups.itemByName('SF_Artikel'),
    psGrTabelle = myDoc.paragraphStyleGroups.itemByName('SF_Artikel_Tabellen'),
    
    osArtikel55 = osGrArtikel.objectStyles.itemByName('SF_Artikel_55mm'),
    osArtikel85 = osGrArtikel.objectStyles.itemByName('SF_Artikel_85mm'),
    osArtikel115 = osGrArtikel.objectStyles.itemByName('SF_Artikel_115mm'),
    psArtikelDruck = psGrArtikel.paragraphStyles.itemByName('SF_Artikel_1sp_Druck'),
    psArtikelNr = psGrArtikel.paragraphStyles.itemByName('SF_Artikel_1sp_ArtNr'),
    psArtikelTab = psGrArtikel.paragraphStyles.itemByName('SF_Artikel_Tabelle'),
    
    psMenge = psGrTabelle.paragraphStyles.itemByName('SF_Tabellen_Menge'),
    psWeissMenge = psGrTabelle.paragraphStyles.itemByName('SF_Tabelle_weiss_Menge'),   
    
    tsBasis = myDoc.tableStyles.itemByName('SF_Tabelle_Basis'),
    tsAufHG = myDoc.tableStyles.itemByName('SF_Tabelle_aufHG'),
    tsKaufTipp = myDoc.tableStyles.itemByName('SF_Tabelle_KaufTipp'),
    tcsFooter = myDoc.cellStyles.itemByName('SF_Tabelle_Standard_Fuss'),
 
    
    osNone = myDoc.objectStyles.itemByName('[Ohne]'),
    psNone = myDoc.paragraphStyles.itemByName('[Einf. Abs.]'),
    csNone = myDoc.characterStyles.itemByName('[Ohne]'),
    
    tsNone = myDoc.tableStyles.itemByName('[Einfache Tabelle]'),
    tcsNone = myDoc.cellStyles.itemByName('[Ohne]');
  
    // variables for table-style, last row.width, last row.original width
    var tsVar, spBr, spVar = ""; 
    // variables with standard values for horizontal scale and tracking
    var spaceVar = 97, lSpVar = -10;
    // variables with standard values for first line indent and left indent
    var flIndent = -2.5; lIndent = 2.5;
    // variable for paragraph style - first paragraph in first cell in the first raw of the table
    var pMengeStyle;

    dialogPart() ;

    for (i=0; i<runs; i++) {
        
        var thisTables = allSelection[i].tables.length;
        
        clearJunk() ;
        removeStyles();
        applyStyles();
        
        }//SCHLEIFE ENDE



    function clearJunk() {

        eachPara = allSelection[i].paragraphs.length;
        var myPara = allSelection[i].paragraphs;

        for (var n=0; n<eachPara; n++) {
             
            app.findGrepPreferences.findWhat = "(Art\\.-Nr\\.\\s.+?)\\n";      
            app.changeGrepPreferences.changeTo = "$1~b";
            myPara[n].changeGrep();   

            app.findGrepPreferences.findWhat = "\\n";      
            app.changeGrepPreferences.changeTo = " ";
            myPara[n].changeGrep();   
            
            app.findGrepPreferences.findWhat = " +";
            app.changeGrepPreferences.changeTo = " ";
            myPara[n].changeGrep();
            }//SCHLEIFE ENDE
        
        }//FUNKTION clearJunk ENDE

    // override any style
    function removeStyles(eachPara) {
        allSelection[i].applyObjectStyle(osNone, true);
        
        eachPara = allSelection[i].paragraphs.length;
        var myPara = allSelection[i].paragraphs;

        for (var n=0; n<eachPara; n++) {
            //remove styles
            myPara[n].applyParagraphStyle(psNone, true);
            myPara[n].applyCharacterStyle(csNone, true);
             //Clear Overrides
           myPara[n].clearOverrides(OverrideType.ALL);
             }//SCHLEIFE ENDE
        
        // format table also, if there is one
        if(allSelection[i].tables.length > 0){
            for (var t=0; t<thisTables; t++) {
                myLastRow = allSelection[i].tables[t].rows[-1];
                allSelection[i].tables[t].rows.everyItem.rowType = RowTypes.BODY_ROW;
                //remove styles
                allSelection[i].tables[t].appliedTableStyle = tsNone;
                allSelection[i].tables[t].cells.everyItem.applyedCellStyle = tcsNone;
                
                for(var tc=0;tc<allSelection[i].tables[t].cells.length;tc++) {
                    //Clear CellStyle Overrides
                    allSelection[i].tables[t].cells[tc].clearCellStyleOverrides();
                    }//INNERE SCHLEIFE ENDE
                
                }//ÄUßERE SCHLEIFE ENDE
            }//BEDINGUNG ENDE   
        
        }//FUNKTION removeStyles ENDE
    
    
    function applyStyles() {
        
        if (allSelection[i].parentPage.side == PageSideOptions.RIGHT_HAND) {

            if (tsVar.name == "SF_Tabelle_aufHG") {flIndent = -5; lIndent = 5;}
            if (tsVar.name == "SF_Tabelle_KaufTipp") {flIndent = -7.5; lIndent = 7.5; }
            if (tsVar.name != "SF_Tabelle_Basis" && osVar.name == "SF_Artikel_115mm") {spBr = spBr; }
            
            else {
            if (tsVar.name == "SF_Tabelle_Basis") {spBr = spBr;}
            
            if ((tsVar.name == "SF_Tabelle_aufHG" && osVar.name == "SF_Artikel_55mm") | 
            (tsVar.name == "SF_Tabelle_aufHG" && osVar.name == "SF_Artikel_85mm")) {spBr = spBr-2.5; spaceVar = 95; lSpVar = -15;}
            
            if ((tsVar.name == "SF_Tabelle_KaufTipp" && osVar.name == "SF_Artikel_55mm") | 
            (tsVar.name == "SF_Tabelle_KaufTipp" && osVar.name == "SF_Artikel_85mm")) {spBr = spBr-5; spaceVar = 93; lSpVar = -15;}
            
                }
            
            }  
        else {
            if (tsVar.name == "SF_Tabelle_Basis") {spBr = spBr;}
            
            if (tsVar.name == "SF_Tabelle_aufHG") {
                spBr = spBr-2.5; spaceVar = 95; lSpVar = -15;
                }
            
            if ((tsVar.name == "SF_Tabelle_KaufTipp" && osVar.name == "SF_Artikel_55mm") | 
            (tsVar.name == "SF_Tabelle_KaufTipp" && osVar.name == "SF_Artikel_85mm") | 
            (tsVar.name == "SF_Tabelle_KaufTipp" && osVar.name == "SF_Artikel_115mm")) {spBr = spBr-5; spaceVar = 93; lSpVar = -15;}
                 
                }
           
            allSelection[i].applyObjectStyle(osVar, true);
            
            eachPara = allSelection[i].paragraphs.length;
            var myPara = allSelection[i].paragraphs;
            
            // Jeden Absatz prüfen, ob er als erstes Wort "Werbefläche" enthält
            for (var n=0; n<eachPara; n++) {
                var m = n-1;
                if (myPara[n].words.length > 0){
                    if(myPara[n].words[0].contents === "Werbefläche") {
                        myPara[n].applyParagraphStyle(psArtikelDruck, true); 
                        myPara[n].leftIndent = lIndent;
                        myPara[m].leftIndent = lIndent;
                        myPara[n].firstLineIndent = flIndent;
                        myPara[m].firstLineIndent = flIndent;
                        break;
                        }//BEDINGUNG ENDE
                }//BEDINGUNG ENDE

                 // format table also, if there is one
                if(allSelection[i].tables.length > 0){
                     for (var t=0; t<thisTables; t++) {
                        if (myLastRow.rowType.toString() != "FOOTER_ROW") {myLastRow.rowType = RowTypes.FOOTER_ROW;}//BEDINGUNG ENDE
                       //apply styles
                        allSelection[i].tables[t].appliedTableStyle = tsVar;
                        allSelection[i].tables[t].rows[0].cells[0].paragraphs[0].applyParagraphStyle(pMengeStyle, true);

                        myLastRow.cells[0].appliedCellStyle = tcsFooter;
                        myLastRow.width = spBr ;
                        
                        myLastRow.cells[0].texts.everyItem().horizontalScale = spaceVar;
                        myLastRow.cells[0].texts.everyItem().tracking = lSpVar;
                        
                        }//SCHLEIFE ENDE
                    }//BEDINGUNG ENDE         
            
                }//SCHLEIFE ENDE
            
            // reset variable values
            spBr = spVar;
            vflIndent = -2.5; lIndent = 2.5;

        }//FUNKTION applyStyles ENDE

    function dialogPart() {        
        var w = new Window('dialog', "Bitte Formatierung wählen");
             w.alignChildren = "left";
            grp1 = w.add('panel', undefined, "Satzbreite");
                grp1.alignChildren = "left";
                grp1.add('radiobutton', undefined, '55 mm');
                grp1.add('radiobutton', undefined, '85 mm');
                grp1.add('radiobutton', undefined, '115 mm');
                grp1.children[0].value = true;
             grp2 = w.add('panel', undefined, "Variante");
                grp2.alignChildren = "left";
                grp2.add('radiobutton', undefined, 'standard', {borderStyle:'etched'});
                grp2.add('radiobutton', undefined, 'auf Hintergrund', {borderStyle:'sunken'});
                grp2.add('radiobutton', undefined, 'KaufTipp', {borderStyle:'raised'});
                grp2.children[0].value = true;
                
            var finishGroup = w.add("group");
            finishGroup.add("button", undefined, "OK");
            finishGroup.add("button", undefined, "Cancel");       

        function selected_rbutton (rbuttons) {
            
            for(var i=0; i<rbuttons.children.length; i++){
                
                if (rbuttons.children[i].value == true) {
                    
                    return rbuttons.children[i].text;
                    
                    } 
                }
            }
        
            if (w.show() == 1) {
 
                switch (selected_rbutton(grp1)) {
                    case '55 mm': spVar = spBr = "55 mm", osVar = osArtikel55;
                    break;
                    case '85 mm': spVar = spBr = "85 mm", osVar = osArtikel85;
                    break;                    
                    case '115 mm': spVar = spBr = "55 mm", osVar = osArtikel115;
                    break;        
                    }
                                   
                switch (selected_rbutton(grp2)) {
                    case 'standard': tsVar = tsBasis, pMengeStyle =  psMenge;
                    break;
                    case 'auf Hintergrund': tsVar = tsAufHG, pMengeStyle =  psMenge;
                    break;                    
                    case 'KaufTipp': tsVar = tsKaufTipp, pMengeStyle =  psWeissMenge;
                    break;
                    }

                }  else {alert("Prozess abgebrochen"); exit();}      
        
        }
}
    

 

 

 

 

TOPICS
Performance , Scripting

Views

343

Translate

Translate

Report

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 ,
Sep 16, 2021 Sep 16, 2021

Copy link to clipboard

Copied

One thing I'd try is using everyItem in your clearJunk function: 

 

var myPara = allSelection[i].paragraphs.everyItem();

        app.findGrepPreferences.findWhat = "(Art\\.-Nr\\.\\s.+?)\\n";      
        app.changeGrepPreferences.changeTo = "$1~b";
        myPara.changeGrep();
        
        app.findGrepPreferences.findWhat = "\\n";      
        app.changeGrepPreferences.changeTo = " ";
        myPara.changeGrep();   
        
        app.findGrepPreferences.findWhat = " +";
        app.changeGrepPreferences.changeTo = " ";
        myPara.changeGrep();

Votes

Translate

Translate

Report

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 ,
Sep 21, 2021 Sep 21, 2021

Copy link to clipboard

Copied

Also try breaking your functions out and pass them scoped variables, rather than nesting all functions. 

Votes

Translate

Translate

Report

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 ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

Thanks brian. I´ll try. 🙂

Votes

Translate

Translate

Report

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 ,
Sep 17, 2021 Sep 17, 2021

Copy link to clipboard

Copied

Hi,

 

You could also look at the ScriptPreference enableRedraw and switch this off for you script.

 

Just add code before your script starts

app.scriptPreferences.enableRedraw = false;

// Your script

app.scriptPreferences.enableRedraw = true;

Votes

Translate

Translate

Report

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 ,
Sep 21, 2021 Sep 21, 2021

Copy link to clipboard

Copied

Good idea, but not the solution.:)

nevertheless thanks.

Votes

Translate

Translate

Report

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 ,
Sep 21, 2021 Sep 21, 2021

Copy link to clipboard

Copied

Change this:

var myPara = allSelection[i].paragraphs

to this:

var myPara = allSelection[i].paragraphs.everyItem().getElements();


Change this, which replaces all spaces, even single ones:

app.findGrepPreferences.findWhat = " +";

to this, which targets only two or more spaces:

app.findGrepPreferences.findWhat = "  +";

Instead of applying styles to individual paragraphs, apply it to all paragraphs in a single line:

....paragraphs.everyItem().applyParagraphStyle (app.documents[0].paragraphStyles.item(myStyleName), true);

Create an array of tables in each text object. Change

if(allSelection[i].tables.length > 0){
   for (var t=0; t<thisTables; t++) {

To this

myTables = allSelection[i].tables.everyItem().getElements();
if(myTables.length > 0){
   for (var t=0; t<myTables.length; t++) {

No doubt some more fine-tuning is possible, but this is a start.

Votes

Translate

Translate

Report

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 ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

LATEST

Hi Peter.

1.)

The line

 

 

var myPara = allSelection[i].paragraphs.everyItem().getElements();

 

 

doesn´t work with

 

 

app.findGrepPreferences.findWhat = "(Art\\.-Nr\\.\\s.+?)\\n";      
        app.changeGrepPreferences.changeTo = "$1~b";
        myPara.changeGrep();

 

 

but with:

 

 

myPara = allSelection[i].paragraphs.everyItem();

 

 

2.)

The search-and-replace hint was good, thanks!

3.)

I can´t apply the paragraph style to all paragraphs. There are different kinds of.

Or maybe I misunderstood your hint.

UPDATE: Oh, now I know what you mean. Good idea!

4.)

Arrays of tables works good as well.

Votes

Translate

Translate

Report

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