18 Replies Latest reply on May 15, 2018 8:59 AM by manuelb27477138

    Apply Paragraph Styles Depending on Text Values

    manuelb27477138 Level 1

      Hello everyone!

       

      I want apply my custom Paragraph Styles to some text, this text have a similar Styles but not the same.

      Is possible create many conditionals and apply my Custom Paragraphs Styles depending on Text Values?

       

      For example: in my file Indesign.indd I want find the texts with the next values and apply my Custom Paragraph Style called Text Number locate in my folder NEW STYLES.

                 fontStyle == "Roman",
                  pointSize <= 10.5,
                  spaceAfter <=1,
                  spaceBefore <= 3.292,
                  leftIndent <= 7.408,
                  rightIndent <= 0,
                  firstLineIndent <= -7.408,
                  hyphenation: false,
                  justification == Justification.LEFT_ALIGN || justification == Justification.LEFT
      

       

      Please this script is very important for me, I appreciate any help.

      Thanks so much!

        • 1. Re: Apply Paragraph Styles Depending on Text Values
          Loic.Aigon Adobe Community Professional

          As far as I know you won't be able to set logical values. But you could still look at some base set or properties and loop through results to see if they have any of the multiple possible values.

          • 2. Re: Apply Paragraph Styles Depending on Text Values
            manuelb27477138 Level 1

            Hi Loic,

            Do you have any code o example, just for start from scratch? sorry, I am beginner.

            I search a lot in Google but I can't find examples.

             

            Thanks!

            • 3. Re: Apply Paragraph Styles Depending on Text Values
              Loic.Aigon Adobe Community Professional

              There are probably many similar samples inside the forum.

              As it seems you only have one static value (i.e. hyphenation), you could start this way:

              //MAIN FUNCTION
              var main = function() {
              
              //VARS
              var doc = app.properties.activeDocument,
              fgp = app.findGrepPreferences.properties,
              texts = [], n = 0, nthText, myParaStyle,
              paraStyleName = "some para style name";
              
              //Exit if no documents open
              if ( !doc ) {
              alert("You need an open document" );
              return;
              }
              
              
              //Create ParaStyle if needed. Otherwise use it
              myParaStyle = doc.paragraphStyles.item ( paraStyleName );
              if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );
              
              //Setting grep find preferences
              app.findGrepPreferences.properties = {
              allowArbitraryHyphenation:true,
              findWhat:".+",
              }
              
              //Finding texts with static properties
              texts = doc.findGrep();
              n = texts.length;
              
              //Looping through found texts to inspect further properties with values ranges.
              while ( n--  ) {
              nthText = texts[n];
              if ( nthText.pointSize <= 10.5
              // && nthText "other property"…
              // && nthText "other property"…
              // Now it's your turn to do the dirty work ;)
              //…
              ) {
              
              //Applying style
              nthText.appliedParagraphStyle = myParaStyle;
              }
              }
              
              
              //reverting UI values in the F/C panel
              app.findGrepPreferences.properties = fgp;
              }
              
              
              var u;
              
              
              app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
              

               

              HTH

               

              Loic

              • 4. Re: Apply Paragraph Styles Depending on Text Values
                manuelb27477138 Level 1

                Hi Loic!

                I start to with your code. Look my file is very simple. But when I run the script the style Text Old didn't change to the style Text New

                Screen Shot 2018-05-11 at 23.26.49.png

                 

                At the moment, I only modify this line:

                paraStyleName = "Text New";
                

                 

                 

                Thanks so much!

                • 5. Re: Apply Paragraph Styles Depending on Text Values
                  Loic.Aigon Adobe Community Professional

                  Hi,

                   

                  Some other settings may corrupt the lookup process. Let's try resetting any existing props:

                   

                  //MAIN FUNCTION  
                  var main = function() {  
                    
                  //VARS  
                  var doc = app.properties.activeDocument,  
                  fgp = app.findGrepPreferences.properties,  
                  texts = [], n = 0, nthText, myParaStyle,  
                  paraStyleName = "some para style name";  
                    
                  //Exit if no documents open  
                  if ( !doc ) {  
                  alert("You need an open document" );  
                  return;  
                  }  
                    
                    
                  //Create ParaStyle if needed. Otherwise use it  
                  myParaStyle = doc.paragraphStyles.item ( paraStyleName );  
                  if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );  
                    
                  //Setting grep find preferences  
                  app.findGrepPreferences = null;
                  app.findGrepPreferences.properties = {  
                  allowArbitraryHyphenation:true,  
                  findWhat:".+",  
                  }  
                    
                  //Finding texts with static properties  
                  texts = doc.findGrep();  
                  n = texts.length;  
                    
                  //Looping through found texts to inspect further properties with values ranges.  
                  while ( n--  ) {  
                  nthText = texts[n];  
                  if ( nthText.pointSize <= 10.5  
                  // && nthText "other property"…  
                  // && nthText "other property"…  
                  // Now it's your turn to do the dirty work ;)  
                  //…  
                  ) {  
                    
                  //Applying style  
                  nthText.appliedParagraphStyle = myParaStyle;  
                  }  
                  }  
                    
                    
                  //reverting UI values in the F/C panel  
                  app.findGrepPreferences.properties = fgp;  
                  }  
                    
                    
                  var u;  
                    
                    
                  app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );  
                  
                  • 6. Re: Apply Paragraph Styles Depending on Text Values
                    manuelb27477138 Level 1

                    Hi Loic!

                    the code you added haven't any changes. I compare both and are the same, is not working

                     

                    thanks you!

                    • 7. Re: Apply Paragraph Styles Depending on Text Values
                      Loic.Aigon Adobe Community Professional

                      You may have missed line #22:

                      app.findGrepPreferences = null;

                       

                      The fact the code doesn't work may imply some F/C settings exclude the text from match. Is the hyphenation turned off on your text. Send your file if needed.

                      • 8. Re: Apply Paragraph Styles Depending on Text Values
                        manuelb27477138 Level 1

                        Hi Loic!

                        thanks por the line. But the script don't do nothing.

                        Please look, this is my styles:

                        Screen Shot 2018-05-13 at 19.30.49.png

                         

                        And I add my custom style in the line #8:

                        paraStyleName = "Text New";

                         

                        I use Adobe Indesign CC 2018.

                        Here are my indesign File: Download

                         

                        Thanks for your patience!

                        • 9. Re: Apply Paragraph Styles Depending on Text Values
                          Loic.Aigon Adobe Community Professional

                          My bad, here is the fix :

                           

                          //MAIN FUNCTION    
                          var main = function() {    
                              
                          //VARS    
                          var doc = app.properties.activeDocument,    
                          fgp = app.findGrepPreferences.properties,    
                          texts = [], n = 0, nthText, myParaStyle,    
                          paraStyleName = "Text New";    
                              
                          //Exit if no documents open    
                          if ( !doc ) {    
                          alert("You need an open document" );    
                          return;    
                          }    
                              
                              
                          //Create ParaStyle if needed. Otherwise use it    
                          myParaStyle = doc.paragraphStyles.item ( paraStyleName );    
                          if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );    
                              
                          //Setting grep find preferences    
                          app.findGrepPreferences = null;  
                          app.findGrepPreferences.properties = {    
                          findWhat:".+",    
                          }    
                              
                          //Finding texts with static properties    
                          texts = doc.findGrep();    
                          n = texts.length;    
                          
                          
                          //Looping through found texts to inspect further properties with values ranges.    
                          while ( n--  ) {    
                          nthText = texts[n];    
                          
                          
                          if ( !nthText.hyphenation
                          &&
                          nthText.pointSize <= 10.5    
                          // && nthText "other property"…    
                          // && nthText "other property"…    
                          // Now it's your turn to do the dirty work ;)    
                          //…    
                          ) {    
                              
                          //Applying style    
                          nthText.appliedParagraphStyle = myParaStyle;    
                          }    
                          }    
                              
                              
                          //reverting UI values in the F/C panel    
                          app.findGrepPreferences.properties = fgp;    
                          }    
                              
                              
                          var u;    
                              
                              
                          app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" ); 
                          
                          • 10. Re: Apply Paragraph Styles Depending on Text Values
                            manuelb27477138 Level 1

                            Hi Loic,

                            I am sorry, I try several times your new code, but still not working.

                             

                            Thank you!

                            • 11. Re: Apply Paragraph Styles Depending on Text Values
                              Loic.Aigon Adobe Community Professional

                              At this point, it doesn't make much sense. I tried to download your file but it went corrupted. May you send it as idml ?

                              • 12. Re: Apply Paragraph Styles Depending on Text Values
                                manuelb27477138 Level 1

                                Hi Loic,

                                I upload again the files, and include the idml:

                                 

                                Download

                                 

                                Thanks!

                                • 13. Re: Apply Paragraph Styles Depending on Text Values
                                  Loic.Aigon Adobe Community Professional

                                  Capture d’écran 2018-05-14 à 12.05.47.png

                                   

                                  Just turn hyphenation off in your sample and the scripts works fine as it's a requirement…

                                   

                                  if ( !nthText.hyphenation…

                                  • 14. Re: Apply Paragraph Styles Depending on Text Values
                                    manuelb27477138 Level 1

                                    Hi Loic,

                                    amazing! the script start to work. Now I try to add more styles, but I get a error. Is correct my new code?

                                    I added the lines #2 and #4:

                                        texts = [], n = 0, nthText, myParaStyle,    
                                        texts2 = [], n = 0, nthText2, myParaStyle2,
                                        paraStyleName = "Text New";   
                                        paraStyleName2 = "Quotation New";  
                                    

                                     

                                    also line #23:

                                        myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );      
                                        if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 ); 
                                    

                                     

                                    also line #54:

                                        if ( !nthText2.hyphenation  
                                            && nthText2.pointSize <= 10.5      
                                            && fontStyle == "Italic"     
                                            // && nthText "other property"…      
                                            // Now it's your turn to do the dirty work       
                                            //…      
                                            ) {      
                                                  
                                            //Applying style      
                                            nthText2.appliedParagraphStyle = myParaStyle2;      
                                            }  
                                    

                                     

                                    FULL CODE:

                                     

                                    //MAIN FUNCTION      
                                    var main = function() {      
                                          
                                        //VARS      
                                        var doc = app.properties.activeDocument,      
                                        fgp = app.findGrepPreferences.properties,      
                                        texts = [], n = 0, nthText, myParaStyle,    
                                        texts2 = [], n = 0, nthText2, myParaStyle2,
                                        paraStyleName = "Text New";   
                                        paraStyleName2 = "Quotation New";   
                                              
                                        //Exit if no documents open      
                                        if ( !doc ) {      
                                        alert("You need an open document" );      
                                        return;      
                                        }      
                                              
                                              
                                        //Create ParaStyle if needed. Otherwise use it      
                                        myParaStyle = doc.paragraphStyles.item ( paraStyleName );      
                                        if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );      
                                    
                                    
                                        myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );      
                                        if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );    
                                              
                                        //Setting grep find preferences      
                                        app.findGrepPreferences = null;    
                                        app.findGrepPreferences.properties = {      
                                        findWhat:".+",      
                                        }      
                                              
                                        //Finding texts with static properties      
                                        texts = doc.findGrep();      
                                        n = texts.length;      
                                          
                                          
                                        //Looping through found texts to inspect further properties with values ranges.      
                                        while ( n--  ) {      
                                        nthText = texts[n];  
                                        nthText2 = texts2[n];     
                                            
                                        if ( !nthText.hyphenation  
                                        && nthText.pointSize <= 10.5      
                                        // && nthText "other property"…      
                                        // && nthText "other property"…      
                                        // Now it's your turn to do the dirty work       
                                        //…      
                                        ) {      
                                              
                                        //Applying style      
                                        nthText.appliedParagraphStyle = myParaStyle;      
                                        }
                                    
                                    
                                        if ( !nthText2.hyphenation  
                                            && nthText2.pointSize <= 10.5      
                                            && fontStyle == "Italic"     
                                            // && nthText "other property"…      
                                            // Now it's your turn to do the dirty work       
                                            //…      
                                            ) {      
                                                  
                                            //Applying style      
                                            nthText2.appliedParagraphStyle = myParaStyle2;      
                                            }         
                                        
                                    }      
                                              
                                              
                                        //reverting UI values in the F/C panel      
                                        app.findGrepPreferences.properties = fgp;      
                                        }      
                                              
                                              
                                        var u;      
                                              
                                              
                                        app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );  
                                    

                                    This is my new styles, I upload my indesign files.

                                     

                                    Look:

                                    Screen Shot 2018-05-14 at 12.40.10.png

                                     

                                     

                                    Screen Shot 2018-05-14 at 12.47.52.png

                                     

                                     

                                    Thank you!

                                    • 15. Re: Apply Paragraph Styles Depending on Text Values
                                      Loic.Aigon Adobe Community Professional
                                      //MAIN FUNCTION        
                                      var main = function() {        
                                              
                                          //VARS        
                                          var doc = app.properties.activeDocument,        
                                          fgp = app.findGrepPreferences.properties,        
                                          texts = [], n = 0, nthText, myParaStyle,      
                                          texts2 = [], n = 0, nthText2, myParaStyle2,  
                                          paraStyleName = "Text New";     
                                          paraStyleName2 = "Quotation New";     
                                                  
                                          //Exit if no documents open        
                                          if ( !doc ) {        
                                          alert("You need an open document" );        
                                          return;        
                                          }        
                                                  
                                                  
                                          //Create ParaStyle if needed. Otherwise use it        
                                          myParaStyle = doc.paragraphStyles.item ( paraStyleName );        
                                          if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );        
                                        
                                        
                                          myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );        
                                          if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );      
                                                  
                                          //Setting grep find preferences        
                                          app.findGrepPreferences = null;      
                                          app.findGrepPreferences.properties = {        
                                          findWhat:".+",        
                                          }        
                                                  
                                          //Finding texts with static properties        
                                          texts = doc.findGrep();        
                                          n = texts.length;        
                                              
                                              
                                          //Looping through found texts to inspect further properties with values ranges.        
                                          while ( n--  ) {        
                                          nthText = texts[n];         
                                                
                                          if ( !nthText.hyphenation    
                                          && nthText.pointSize <= 10.5        
                                          // && nthText "other property"…        
                                          // && nthText "other property"…        
                                          // Now it's your turn to do the dirty work         
                                          //…        
                                          ) {        
                                                  if ( nthText.fontStyle == "Italic" ) {
                                      nthText.appliedParagraphStyle = myParaStyle2;     
                                      }
                                      else {
                                      nthText.appliedParagraphStyle = myParaStyle;     
                                      }
                                          //Applying style           
                                          }  
                                              }    
                                                  
                                          //reverting UI values in the F/C panel        
                                          app.findGrepPreferences.properties = fgp;        
                                          }        
                                                  
                                      
                                                  
                                          var u;        
                                                  
                                                  
                                          app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );  
                                      
                                      • 16. Re: Apply Paragraph Styles Depending on Text Values
                                        manuelb27477138 Level 1

                                        Hi Loic!

                                        I am very happy because your script is working

                                        Now I start to test deeply, and I find some obstacle, please can you help me?

                                         

                                        Like you can see my Styles to apply are inside a folders:

                                        Screen Shot 2018-05-15 at 10.19.17.png

                                         

                                        To fix this, I did this:

                                            paraStyleName = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName("Text New");     
                                            paraStyleName2 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName("Quotation New");   
                                        

                                         

                                        Why is not working?

                                        Screen Shot 2018-05-15 at 10.24.41.png

                                         

                                        Thanks!

                                        • 17. Re: Apply Paragraph Styles Depending on Text Values
                                          Loic.Aigon Adobe Community Professional

                                          It's likely because script paraStyleName variable expects a string so it's used to target a specific style. In your case, you are passing a script reference so later on, the script tries to set a reference to a paragraph style based on a string which is actually a paragraph style instance.

                                          You need to rewrap the script so instead of passing a string, you can pass the style itself:

                                          var paraStyle = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName("Text New");

                                          Then

                                          nthText.appliedParagraphStyle = paraStyle;

                                          • 18. Re: Apply Paragraph Styles Depending on Text Values
                                            manuelb27477138 Level 1

                                            Hi Loic, is working, you help me a lot really deep thank you!

                                             

                                            Download the files

                                             

                                            // The Script apply paragraph styles based in the text options. For example if your text are in bold, the script will apply the Paragraph Quotation Bold.
                                            // It is a easy way to typeset long documents with your custom styles.
                                            // Files to download: https://github.com/firedevelop/id0000013-Adobe-InDesign-Scripts-Examples/tree/master/id0000074-Apply-Paragraph-Styles-Depending-on-Text-Values
                                            // Thanks to the author: Loic.Aigon from Adobe Forums
                                            //MAIN FUNCTION          
                                            var main = function() {            
                                                //VARS          
                                                var doc = app.properties.activeDocument,          
                                                fgp = app.findGrepPreferences.properties,          
                                                texts = [], n = 0, nthText, myParaStyle,        
                                                texts2 = [], n = 0, nthText2, myParaStyle2,  
                                                texts3 = [], n = 0, nthText3, myParaStyle3,  
                                                texts4 = [], n = 0, nthText4, myParaStyle4, 
                                                paraStyleName = "Text New";   
                                                paraStyleName2 = "Text New 12";   
                                                paraStyleName3 = "Quotation New Bold";    
                                                paraStyleName4 = "Quotation New Italic";
                                                          
                                                //Exit if no documents open          
                                                if ( !doc ) {          
                                                alert("You need an open document" );          
                                                return;          
                                                }              
                                                          
                                                //Create ParaStyle if needed. Otherwise use it      
                                                var paraStyle = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName);    
                                                myParaStyle = doc.paragraphStyles.item ( paraStyleName );          
                                                // if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );          
                                                
                                                var paraStyle2 = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName2);
                                                myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );          
                                                // if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );  
                                            
                                            
                                                var paraStyle3 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName3);
                                                myParaStyle3 = doc.paragraphStyles.item ( paraStyleName3 );          
                                                // if ( !myParaStyle3.isValid ) myParaStyle3 = doc.paragraphStyles.add ( paraStyleName3 );  
                                            
                                            
                                                var paraStyle4 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName4);
                                                myParaStyle4 = doc.paragraphStyles.item ( paraStyleName4 );          
                                                // if ( !myParaStyle4.isValid ) myParaStyle4 = doc.paragraphStyles.add ( paraStyleName4 ); 
                                                          
                                                //Setting grep find preferences          
                                                app.findGrepPreferences = null;        
                                                app.findGrepPreferences.properties = {          
                                                findWhat:".+",          
                                                }          
                                                          
                                                //Finding texts with static properties          
                                                texts = doc.findGrep();          
                                                n = texts.length;          
                                                      
                                                //Looping through found texts to inspect further properties with values ranges.          
                                                while ( n--  ) {nthText = texts[n];                   
                                                    if (nthText.fontStyle =="Roman" && nthText.pointSize <= 10.5 ){nthText.appliedParagraphStyle = paraStyle;} 
                                                    if (nthText.pointSize == 12){nthText.appliedParagraphStyle = paraStyle2;} 
                                                    if (nthText.fontStyle == "Bold"){nthText.appliedParagraphStyle = paraStyle3;} 
                                                    if (nthText.fontStyle == "Italic") {nthText.appliedParagraphStyle = paraStyle4;}   
                                                }      
                                                          
                                                //reverting UI values in the F/C panel          
                                                app.findGrepPreferences.properties = fgp;          
                                                }          
                                                var u;                 
                                                app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );  
                                            

                                             

                                             

                                            Some images about the script:

                                            Screen Shot 2018-05-15 at 17.50.26.png

                                             

                                            Screen Shot 2018-05-15 at 17.50.37.png