14 Replies Latest reply on May 13, 2014 1:49 AM by BEGINNER_X

    Find Rule and insert content

    BEGINNER_X Level 3

      Hi All,

       

      I need to find the rule with stroke 1 and color is "MM".

       

      Once I find the rule, need to replace the rule with the content "aaa".

       

      Trying  the below code is:

       

      var myPg = app.activeDocument.allPageItems;
      
      for(i=myPg.length-1; i>=0; i--)
      {
      //~     if(myPg[i].constructor.name == "GraphicsLine")
      //~     {
              
              if(myPg[i].strokeWeight == 1 && myPg[i].strokeColor.name == "MM")
              {
                  myPg[i].remove();
                  myPg[i].insertionPoints[0].contents = "aaa"      //Error in this line
                  }
      //~         }
          }
      
      
      

       

      Kindly refer the attachment for more reference.

      rule.png

       

      Please help for my request.

       

      Regards

      Siraj

        • 1. Re: Find Rule and insert content
          Jump_Over Level 5

          Hi,

           

          Is your "rule" a graphicLine (a separate object) or a ruleBelow/ruleAbove (paragraph property).

           

          Depending on this the way of remove them differ. A property is to switch true/false.

          Just clarify above before go further

           

          Jarek

          • 2. Re: Find Rule and insert content
            BEGINNER_X Level 3

            Hi Jump,

             

            Thanks for your reply...

             

            The line should be a Graphic line (manually drawn line) using line tool.

             

            Please help for your request.

            • 3. Re: Find Rule and insert content
              Laubender Adobe Community Professional & MVP

              @Siraj – after inspecting your screenshot closely, it could well be that object selected is a graphic line inside a paragraph. Then you could access it by a texts object like Story or even Paragraph.

               

              Select an insertionPoint inside the paragraph and check with:

               

              app.selection[0].texts[0].paragraphs[0].graphicLines.length;
              

               

              If that is 1 you could access the graphic line and read out the color name of the stroke:

               

              app.selection[0].texts[0].paragraphs[0].graphicLines[0].strokeColor.name;
              

               

              So, basically you need to search Texts objects and look after graphicLine with a particular color name and stroke weight.

               

              Uwe

              • 4. Re: Re: Find Rule and insert content
                Laubender Adobe Community Professional & MVP

                I meant: you can access all graphicLines in all stories and walk through them with a for loop.

                 

                var myGraphicLinesInStories = app.documents[0].stories.everyItem().graphicLines.everyItem().getElements();
                
                for(var n=0;n<myGraphicLinesInStories.length;n++){
                    if(myGraphicLinesInStories[n].strokeColor.name == "MM" && myGraphicLinesInStories[n].strokeWeight == "1 pt"){
                        $.writeln("Item found.");
                        };
                    };
                

                 

                Uwe

                • 5. Re: Find Rule and insert content
                  BEGINNER_X Level 3

                  Hi Laubender,

                   

                  Thanks for your reply...

                   

                  Its working fine in my end especially for the below line.

                  1. var myGraphicLinesInStories = app.documents[0].stories.everyItem().graphicLines.everyItem().getElements(); 

                   

                  But the main concern is, need to replace the rule with the content "aaa"

                   

                  Kindly help for this request.

                   

                  Thanks in advance

                  Siraj

                  • 6. Re: Re: Find Rule and insert content
                    Laubender Adobe Community Professional & MVP

                    What is "aaa"?
                    An unformatted String of text?
                    Should it be formatted?

                     

                    If you want just the new content without special formatting you could view the found graphicLine as an simple Character object with graphicLine.parent. The parent of a graphic object inside a texts object is a single character.

                     

                    Now you can assign a new contents to that parent:

                     

                    myGraphicLinesInStories[n].parent.getElements()[0].contents = "aaa";
                    

                     

                    Of course, if you do something like that, you have to loop from back to forth not messing up the index.

                     

                    Uwe

                    • 7. Re: Find Rule and insert content
                      BEGINNER_X Level 3

                      Thank You Laubender and Jump Over...

                       

                      For the content "aaa" I need to replace with "______________".

                       

                      Request:

                       

                      My request is while extract the Indesign to word, the graphics lines is missing.

                       

                      So, that I planned to replace the "________" just (underscore) for view purpose.

                       

                      Thanks a lot.

                      Any other better logic is appreciatable.

                       

                       

                      Again Thanks

                      Siraj

                      • 8. Re: Re: Find Rule and insert content
                        BEGINNER_X Level 3

                        Hi All,

                         

                        Instead of (strokeColor.name == "MM"), I need to change the (strokeColor.colorValue == [0, 100, 0, 0]). But I fails to produce the output.


                        I tried this line,

                        //~ alert(app.selection[0].strokeColor.colorValue)

                         

                        Full Code is:

                         

                        app.activeDocument.viewPreferences.horizontalMeasurementUnits =  MeasurementUnits.POINTS;
                        app.activeDocument.viewPreferences.verticalMeasurementUnits =  MeasurementUnits.POINTS;
                        
                        var myGraphicLinesInStories = app.documents[0].stories.everyItem().graphicLines.everyItem().getElements();  
                          
                        for(var n=0;n<myGraphicLinesInStories.length;n++){  
                            if(myGraphicLinesInStories[n].strokeColor.colorValue == [0, 100, 0, 0])     // Here is the problem
                            {  
                                myGraphicLinesInStories[n].parent.getElements()[0].contents = "aaa";  
                                };  
                            };  
                        
                        

                         

                        Please help, this to be urgent request.

                         

                        Thanks in advance

                        Siraj

                        • 9. Re: Re: Re: Find Rule and insert content
                          Jump_Over Level 5

                          Hi,

                           

                          Problem is here:

                          a = [0,100,0,0];
                          b = [0,100,0,0];
                          a == b    // false
                          

                          comparing array you are expecting each element's value is compared but not.

                          You could add Array.prototype compare() to reach a goal, like this:

                          Array.prototype.compare = function(array) {
                          var len = this.length;
                          if (len != array.length) return false;
                          while (len-->0)
                               if (this[len] != array[len]) return false;
                          
                          return true;
                          }
                          a = [0,100,0,0];
                          b = [0,100,0,0];
                          a.compare(b)    // true
                          

                           

                          Jarek

                          • 10. Re: Re: Re: Re: Find Rule and insert content
                            Laubender Adobe Community Professional & MVP

                            @Jarek – what also can be done in this case is doing the comparison on String basis.

                             

                            a = [0,100,0,0]; 
                            b = [0,100,0,0]; 
                            a.toString() == b.toString()  // true
                            

                             

                            Uwe

                            • 11. Re: Re: Re: Re: Re: Find Rule and insert content
                              Laubender Adobe Community Professional & MVP

                              @Jarek – I said in this case, because of course using it with multidimensional arrays would lead to false positives:

                               

                              a =[[0,100,0],[100,0,0],[0,0,100],[50,0,0]];
                              b =[[0,100,0,100],[0,0,0,0],[100,50,0,0]];
                              //DANGEROUS:
                              a.toString() == b.toString()  // false positive: true
                              

                               

                              Uwe

                              • 12. Re: Re: Re: Re: Re: Re: Find Rule and insert content
                                Laubender Adobe Community Professional & MVP

                                @Jarek – but in the case of multidimensional arrays your Array.prototype.compare function will also not work as expected:

                                 

                                Array.prototype.compare = function(array) { 
                                var len = this.length; 
                                if (len != array.length) return false; 
                                while (len-->0) 
                                    if (this[len] != array[len]) return false; 
                                
                                return true; 
                                } 
                                
                                a =[[0,100,0,100],[0,0,0,0],[100,50,0,0]];
                                b =[[0,100,0,100],[0,0,0,0],[100,50,0,0]];
                                //WRONG RESULT:
                                a.compare(b)    // false
                                

                                 

                                Uwe

                                • 13. Re: Find Rule and insert content
                                  Jump_Over Level 5

                                  @Uwe - surely right.

                                   

                                  Each solution has its shadow named assumings

                                   

                                  Jarek

                                  • 14. Re: Find Rule and insert content
                                    BEGINNER_X Level 3

                                    Hi Laubender/Jump_Over,

                                     

                                    Sorry the late reply....

                                     

                                    Thank you for your response and also spending your valuable time for me.

                                     

                                    I got the solution

                                     

                                    1.     if(myGraphicLinesInStories[n].strokeColor.colorValue == "0,100,0,0")   //works fine for me

                                     

                                    Thanks a lot

                                    Siraj