13 Replies Latest reply on Mar 25, 2016 8:27 AM by tpk1982

    Grouped objects fill color

    tpk1982 Level 4

      Hi,

       

      I need to find the magenta color used in the document. The below coding is working fine. But if my document contains grouped objects it throws error. Please help me how to solve this.

       

      var pageLen = app.activeDocument.pages.length     
      var mag=0;  
      var myLayersArray = [];  
      for(i=0;i<pageLen;i++)     
      {     
              var pageItemLen = app.activeDocument.pages.item(i).pageItems.length     
              for(j=0;j<pageItemLen;j++)     
              {     
                  color = 0; scolor =0     
                  if(app.activeDocument.pages.item(i).pageItems.item(j).fillColor.name != "None")  {   
                      color = app.activeDocument.pages.item(i).pageItems.item(j).fillColor.colorValue;  }   
                  if(app.activeDocument.pages.item(i).pageItems.item(j).strokeColor.name != "None")  {   
                      scolor = app.activeDocument.pages.item(i).pageItems.item(j).strokeColor.colorValue; }    
                  if(color == "0,100,0,0" || scolor == "0,100,0,0")   {  
                        mag=1;     
                    myLayersArray.push( app.activeDocument.pages.item(i).pageItems.item(j).itemLayer.name);    }
                }     
          }     
          
          
      if(mag==1)     
      {     
                 alert("ERROR: Some Page objects contain Magenta color fill/stroke"+" in "+myLayersArray.join(", "));     
          }     
                      else{     
                       lalert("Page objects not contain Magenta color fill/stroke");    
      }  
      

       

       

      I tried with the two option [object Group] and instanceof Group but failed.

       

      if (pageItemLen instanceof Group){continue}  
      

       

      and

       

      if ( pageItemLen != "[object Group]" ) {  
      

       

      Regards,

      Karthi

        • 1. Re: Grouped objects fill color
          Chinnadk Level 4

          Hi Karthi,

           

          Try this,

           

          var doc = app.activeDocument;
          var pageitems = doc.pageItems;
          var myLayersArray = []; 
          for(var i=0;i<pageitems.length;i++)
          {
                  if(pageitems[i].pageItems.length > 0)
                  {
                          var grpitems = pageitems[i].pageItems
                          for(var j=0;j<grpitems.length;j++)
                          {
                                  findmegenda(grpitems[j])
                              }
                      }
                  else
                  {
                          findmegenda(pageitems[i])
                      }
              }
          if(myLayersArray.length > 0)
          {
                  alert("ERROR: Some Page objects contain Magenta color fill/stroke"+" in "+myLayersArray.join(", "))
              }
          else
          {
                  alert("Page objects not contain Magenta color fill/stroke"); 
              }
          function findmegenda(pageitem)
          {
                  if(pageitem.fillColor.name != "None")
                  {
                          if(pageitem.fillColor.colorValue == "0,100,0,0")
                          {
                                  myLayersArray.push(pageitem.itemLayer.name);
                              }
                      }
                  else if (pageitem.strokeColor.name != "None")
                  {
                          if(pageitem.strokeColor.colorValue == "0,100,0,0")
                          {
                                  myLayersArray.push(pageitem.itemLayer.name);
                              }
                      }
              }
          

           

          Regards,

          Chinna

          • 2. Re: Grouped objects fill color
            tpk1982 Level 4

            Hi Chinna,

             

            Thanks for your reply. But i got the following error.

             

            Screen Shot 2016-03-25 at 2.51.41 PM.png

            Thanks,

            Karthi

            • 3. Re: Grouped objects fill color
              Chinnadk Level 4

              I just modified my code I think your trying with my old post. Please try the updated code.

              • 4. Re: Grouped objects fill color
                Ronald63 Level 4

                Hi,

                 

                Try this ...

                var doc = app.activeDocument,
                mag=0,
                myLayersArray = new Array();
                
                var mPageItems = doc.allPageItems;
                var len = mPageItems.length;
                
                while (len--){
                    var aPageItem = mPageItems[len],
                    color = 0,
                    scolor =0;
                    if (aPageItem instanceof Group){continue}
                    if(aPageItem.fillColor.name != "None")
                    {
                        color = aPageItem.fillColor.colorValue;
                    }
                    if(aPageItem.strokeColor.name != "None")
                    {
                        scolor = aPageItem.strokeColor.colorValue;
                    }
                    if(color == "0,100,0,0" || scolor == "0,100,0,0")
                    {
                        mag=1;
                        myLayersArray.push( aPageItem.itemLayer.name);
                    }
                }
                
                if(mag==1)
                {
                    alert("ERROR: Some Page objects contain Magenta color fill/stroke"+" in "+myLayersArray.join(", "));
                }
                else{
                    alert("Page objects not contain Magenta color fill/stroke");
                }
                
                • 5. Re: Grouped objects fill color
                  tpk1982 Level 4

                  Hi Chinna & Ronald,

                   

                  Bothe the script gave error messages.

                   

                  Chinna:

                   

                  Screen Shot 2016-03-25 at 3.31.01 PM.png

                   

                  Ronald:

                   

                  Screen Shot 2016-03-25 at 3.31.31 PM.png

                   

                  Both shows "graphic attribute" error. May i know is it some thing related with images?

                   

                  Regards,

                  Karthi

                  • 6. Re: Grouped objects fill color
                    tpk1982 Level 4

                    The grouped objects contains a outlined text, is it the issue?

                    • 7. Re: Grouped objects fill color
                      pixxxel schubser Level 5

                      Hi tpk1982,

                      IMHO this error messages are typical for placed images in your document. Do you have some placed images in your document?

                       

                      This child of page items does not have a fill or a stroke color (only his parent „container“)

                      • 8. Re: Grouped objects fill color
                        tpk1982 Level 4

                        Hi pixxxel schubser ,

                         

                        Exactly, Yes i have one tif background file. I hope we are close to the solution for this question.

                         

                        Regards,

                        Karthi

                        • 9. Re: Grouped objects fill color
                          Marc Autret Level 4

                          Hi,

                           

                          That's a good example of using the myObj.properties.XXX indirection. The Image object—which is counted as a page item due to its geometric bounds—inherits the formal properties fillColor and strokeColor while it can't actually have those properties set. So, syntactically, myImage.fillColor looks good but the underlying DOM command causes a runtime error.

                           

                          A good approach is to admit we can't know in advance how the DOM will grow in the future, so, when such an issue is detected, do not try to anticipate all wrong cases (through some instance of conditions) and apply the agnostic myObj.properties.XXX test. Here you just need to test whether myPageItem.properties.fillColor is OK before further processing.

                           

                          Possible implementation:

                           

                          const CMYK_TRACE = "0,100,0,0";
                          const RESULT_STR = '%1 (%2) on layer "%3"';
                          
                          function findCmykPattern(/*Document*/doc,  r,z,a,o,k,t)
                          {
                              if( !doc ) throw "No document available."
                          
                              const __ = $.global.localize,
                                    FS = { fillColor:'fill', strokeColor:'stroke' };
                             
                              r = [];
                              z = 0;
                              a = doc.allPageItems;
                          
                              while( o=a.pop() )
                              {
                                  for( k in FS )
                                  {
                                      if( !(t=o.properties[k]) ) continue;
                                      if( CMYK_TRACE != '' + t.properties.colorValue ) continue;
                                      r[z++] = __( RESULT_STR, o.__class__, FS[k], o.itemLayer.name);
                                  }
                              }
                          
                          
                              alert( z ?
                                  __( "ERROR: Unwanted CMYK at %1 location(s):\r\r%2", z, r.join(",\r") ) :
                                  __( "All page items are OK regarding CMYK requirements." )
                                  );
                          };
                          
                          findCmykPattern(app.properties.activeDocument);
                          
                          

                           

                          Hope that helps,

                          Marc

                          • 10. Re: Grouped objects fill color
                            Laubender Adobe Community Professional & MVP

                            Hi Karthi,

                            yes, the allPageItems array does include graphics as well. This was different in InDesign CS3 and CS4.

                             

                            The Graphics class has the fillColor property and fillTint as well. Even strokeWeight, strokeTint (EDIT) or strokeTint.

                            Even the Image class has all these properties.

                             

                            pixxxel schubser is wrong on this.

                             

                            Back to your placed image:

                            A placed tiff image is part of the Image class, the Image Class is part of the Graphics class, so you might think, why is there an error?

                             

                            As your error message is indicating, some graphics of type image do not support fillColor (or fillTint).

                            Some do, like grayscale or 1bit bitmap tiff images without transparency.

                             

                            If fillColor is not supported, then fillColor.colorValue is not supported as well.


                            Also pageItems filled with the swatch [None] do not support the colorValue property, but obviously will support fillColor as property.

                             

                            Beware of Group objects and Button objects, where an applied fillColor will apply the color to more than one pageItem.
                            However, if all pageItems of a group have the same fillColor there will be no error, if you ask for fillColor.colorValue.

                             

                            Usually I hate this, but we could simply step over these cases with a try catch and continue in the for loop through the allPageItems array.

                             

                            try{
                            // condition, that throws the error.
                            }catch(e){continue}
                            
                            
                            

                             

                            And another thing:

                            colorValue of fillColor is always type of Array.

                            So to compare a string with an array you could do this:

                             

                            fillColor.colorValue.toString() == "0,100,0,0";
                            
                            
                            

                             

                            Further:

                            Will a color value like "0,99.999,0,0" qualify as "Magenta"?

                            If yes, you could do a match on the stringyfied array:

                             

                            // Example:
                            var regEx = RegExp("0,\\d{2,3},0,0"); // Two digit or three digit numbers of magentas. One digit number magentas will not qualify.
                            var a = fillColor.colorValue; // Array
                            a[1] = Math.floor(a[1]); // Or do a different rounding. A Magenta could well be of value [0,67.999,0,0].
                            if(fillColor.colorValue.toString().match(regEx)){doSomething()};
                            
                            
                            

                             

                            Or will a tint value like 5 qualify? Then you have to look at the tint values as well. //EDIT
                            // If it will not qualify, you will have to look at the tint values as well.


                            Right now, you catch every tint of a color with one exact colorValue.

                             

                            Uwe

                             

                            //EDIT. And: Did not see Marc's answer before posting.
                            //ANOTHER EDIT in my example code. Pasted a portion two times.

                            • 11. Re: Grouped objects fill color
                              Marc Autret Level 4

                              Thanks Uwe for these precisions.

                               

                              Indeed in CS4 (v.6.0) the Graphic object had not PageItem as a "base class." This inheritance has been introduced in CS5, so we now have PageItem > Graphic > Image.

                               

                              Also, I didn't notice that some Image instances may have a functional fillColor property. Thanks to you for pointing this out. (My snippet above should support this case anyway.)

                               

                              @+

                              Marc

                              • 12. Re: Grouped objects fill color
                                Laubender Adobe Community Professional & MVP

                                I did a mistake in my example code, that I could not edit after posting.
                                The last line should be changed.

                                 

                                Here the full example again with the corrected mistake:

                                 

                                // Example: 
                                var regEx = RegExp("0,\\d{2,3},0,0"); // Two digit or three digit numbers of magentas. One digit number magentas will not qualify. 
                                var a = fillColor.colorValue; // Array 
                                a[1] = Math.floor(a[1]); // Or do a different rounding. A Magenta could well be of value [0,67.999,0,0]. 
                                if(a.toString().match(regEx)){doSomething()};
                                

                                 

                                Hi Marc,

                                yes, placed grayscale images or 1bit images can be colored in InDesign.
                                They could be of image type name TIFF, JPEG or even Photoshop. Maybe other formats like PNG, if they support grayscale.

                                Provided they are flattened to the "Background" layer—the one you can see in PhotoShop formatted in Italics—and have no additional layer.

                                 

                                That's a cool feature.

                                 

                                Uwe

                                • 13. Re: Grouped objects fill color
                                  tpk1982 Level 4

                                  Hi Marc/Uwe,

                                   

                                  Thank you so much for your help.

                                   

                                  @Uwe: Awesome explanation. Very much appreciated your help.

                                   

                                  So my understanding, we should ignore those images while checking?

                                   

                                  Please correct me if my understanding is wrong.

                                   

                                  Regards,

                                  Karthi