25 Replies Latest reply on Jul 28, 2018 2:08 PM by CarlosCanto

    Accessing the elements of a page item

    ProfessorCal

      Good people of the Adobe forums,

       

      I am working on a script that performs a series of things on all of the artwork in a document.  At one point, it needs to compare the fill color of every item to a sample color.  The problem is, the artwork is a collection of compound path items and path items.  When I look for pathItems[x].fillColor, it works fine.  When I look for compoundPathItem[x].fillColor, it comes back undefined.  So, I've moved to looking at page items, but I seem to be unable to get to the internals on the page items.  If I have a compound path item, how would I address its properties from its parent page item?  I tried:

       

      pageItem[x].compoundPathItem.pathItems[0].fillColor,

      pageItems[x].fillColor,   & 

      pageItems[x].compoundPathItem.fillColor. 

       

      They all seem to break.

        • 1. Re: Accessing the elements of a page item
          Gustavo Del Vechio Level 3

          Hi

           

          The fillColor property is not valid for compoundPathItems. This is because the color of a compound path is determined by its interior paths. So, if you change the color of the paths inside a compound path, you actually change the color of the compound path. Consider the compound path like a structure, a container that takes the color of their components (its paths).

           

          This is why you get a return "undefined" when you try to read the "fillColor" of a compoundPathItem.

           

          Hope to be helped

          Best Regards

          Gustavo.

          1 person found this helpful
          • 2. Re: Accessing the elements of a page item
            Gustavo Del Vechio Level 3

            For example (did not test yet):

             

             

            var doc = app.activeDocument;
            var compounds = doc.compoundPathItems; //this will return an array of the CompoundPaths;
            
            
            for (var g=0 ; g<compounds.length ; g++){
               var interiorPaths =  compounds[g].pathItems; //it will give you an array of the paths inside the compoudPaths. So you can read the fillColor of the paths
               //now play with the interiorPaths array. You could declare here a new -for- statement to cycle between these paths.
            };
            
            

             

            This will allow you to work with the paths inside the compound paths.

             

            This is only one way to work. There's other ways also to work.

             

            Best Regards

            Gustavo.

             

            Message was edited by: Gustavo Del Vechio

            1 person found this helpful
            • 3. Re: Accessing the elements of a page item
              Gustavo Del Vechio Level 3

              P.S: I've just correct the above code.

               

              Gustavo.

              • 4. Re: Accessing the elements of a page item
                ProfessorCal Level 1

                Gustavo,

                 

                Thank you for your quick response!  Unfortunately, that doesn't address my big problem.  I will try to explain a bit better..

                 

                I know about having to access the path items inside of a compound path.  That's why I tried "pageItem[x].compoundPathItem.pathItems[0].fillColor" (first option listed in my first post)..  The tough bit is that it appear I need to work my way through all of the art in terms of page items, instead of looping through the path items separately and the compound path items separately.  This helps keep things in proper order as the script goes on, after comparing, to move the items to new groups that have to be in a specific order.

                 

                So, it brings me back to the problem of finding out the fill color of a page item whether it is a regular path item or a compound path item.

                • 5. Re: Accessing the elements of a page item
                  Gustavo Del Vechio Level 3

                  Hi ProfessorCal

                   

                  Thank you. I think I understand you. So you want to work by looping between all pageItems. An idea, perhaps could help: what about if you insert an "if" statement inside your loop, so it first analyzes the kind of pageItem, and you have different codes for the different kinds of items (like paths or compoundPaths). Example:

                   

                  if (pageItem[x].typename == "PathItem"){

                  //your code here.

                  };

                  else

                  if (pageItem[x].typename == "CompoundPathItem"){

                  //your code here

                  };

                   

                  Would it help your code?

                   

                  P.S: you could even evaluate if the current pathItem is inside or not any group. Example:

                   

                   

                  if (pageItem[x].typename == "PathItem" && pageItem[x].parent.typename == "Layer"){

                  //so its an isolated path not inside any group or compoundPath.

                  };

                   

                   

                  Abraços!

                  Gustavo.

                   

                  Message was edited by: Gustavo Del Vechio

                  1 person found this helpful
                  • 6. Re: Accessing the elements of a page item
                    ProfessorCal Level 1

                    Actually, that's exactly what we're doing.

                     

                    Later parts of the code have to look at the fill color of each item, check its type (gradient -vs- spot) and compare it to other colors.  In order to avoid a series of 'if' statements every time, we have made a function that takes each page item and returns a path item from it so we can work with that within the rest of the code (it's easier to write all of these comparisons if your'e always expecting to get a path item, and the first path item of every compound path has properties that apply to the entire compound path..) SO...  that function looks like this:

                     

                    function getLeadItem(glPage) {   //glPage is the Page Item

                        if (glPage.typename == 'CompoundPathItem'){

                            return glPage.pathItems[0]    //returns the first path item of the compound path (...but does not work)

                        } else {

                            return glPage  (if it is a normal page item, it just returns itself)

                        }

                        glPage = null

                    }

                     

                    for all of the page items that are regular path items, the code later on that looks like this:

                     

                    if(item.fillColor == '[GradientColor]'){

                                selectedColor = doc.activeLayer.pageItems[y].fillColor.gradient

                     

                    But any of the compound path ones break, which is why we made the function earlier, so that 'item' would ALWAYS be a path item, and not compound.

                    • 7. Re: Accessing the elements of a page item
                      CarlosCanto Adobe Community Professional & MVP

                      your function DOES work, are you dealing with Outlined text? text usually gets grouped once it's outlined

                       

                      var idoc = app.activeDocument;
                      var glPage = idoc.pageItems[0];
                      
                      var alert(glPage.typename);
                      var leadItem = getLeadItem (glPage);
                      
                      alert(leadItem.fillColor.typename);
                      
                      function getLeadItem(glPage) {   //glPage is the Page Item
                      
                          if (glPage.typename == 'CompoundPathItem'){
                      
                              return glPage.pathItems[0] ;   //returns the first path item of the compound path (...but does not work)
                      
                          } else {
                      
                              return glPage;//  (if it is a normal page item, it just returns itself)
                      
                          }
                      
                          glPage = null
                      
                      }
                      
                      • 8. Re: Accessing the elements of a page item
                        ProfessorCal Level 1

                        Carlos, Gustavo,

                         

                        Thank you both for the help.  Looks like I was misinterpreting my error messages.  The problem seems to have more to do with the way data is being fed to the function and how our list of page items is changing over time as other parts of the code move things around.

                         

                        Thanks again.

                        • 9. Re: Accessing the elements of a page item
                          Larry G. Schneider Adobe Community Professional & MVP

                          Then you may need to start at the back and work forward which reduces the chances of changing the index of the page item.

                          • 10. Re: Accessing the elements of a page item
                            scottRaysNI Level 1

                            Morning Carlos,

                             

                            I saw your comment related to getting the fillColor from a CompoundPath by checking the first item in the PathItems array ( compoundPath.pathItems[p].fillColor ). I am familiar with this solution and use it frequently.

                             

                            But you mention something about how when text is converted to outlines it's grouped. I am having difficulty querying the fillColor on these types of CompoundPaths?

                             

                            Any thoughts?

                             

                            Thanks!

                            Scott

                            • 11. Re: Accessing the elements of a page item
                              Silly-V Adobe Community Professional

                              Outlined text is a group where all characters are individual compound path items.

                               

                              Assuming document has only one text item which was freshly converted to outlines:

                                  alert(app.activeDocument.groupItems[0].compoundPathItems[0].pathItems[0].fillColor);

                              • 12. Re: Accessing the elements of a page item
                                CarlosCanto Adobe Community Professional & MVP

                                I think he means Outlined text turned into a compoundPath

                                • 13. Re: Accessing the elements of a page item
                                  CarlosCanto Adobe Community Professional & MVP

                                  do you want to query color or to apply color to compound paths?

                                  • 14. Re: Accessing the elements of a page item
                                    renél80416020 Level 3

                                    Salut !

                                    // JavaScript Document

                                    function test() {
                                      var docRef = activeDocument;
                                        var obj, type, liste, tabs, ch;
                                            liste = "";
                                            tabs = docRef.pathItems;
                                            alert("tabs.length = "+tabs.length)
                                          if (tabs.length) {
                                            for(var i = 0, o = 0; i < tabs.length; i++) {
                                              obj = tabs[i];
                                              type = obj.parent.typename;
                                                if(type == "CompoundPathItem") {
                                                  ch = obj.parent.pathItems[0].fillColor.typename;
                                                  i += obj.parent.pathItems.length-1;
                                                  o++;
                                                  liste += "Objet "+o+" "+type+"\r"+ch+"\r";
                                                }
                                                else {
                                                  ch = obj.fillColor.typename;
                                                  o++;
                                                  liste += "Objet "+o+" "+obj.typename+"\r"+ch+"\r";
                                                }
                                            }
                                            alert(liste);
                                          }
                                          else alert("Aucun tracé trouvé ?","De Elleere");
                                    }
                                    test();
                                    

                                    de elleere

                                    • 15. Re: Accessing the elements of a page item
                                      scottRaysNI Level 1

                                      Morning,

                                       

                                      Here is a screenshot of a selected compoundPath that I assume was created by "expanding" the text, and the pathItems.length is strangely equal to zero? So I am unable to query the fillColor property?

                                       

                                      I guess my question is, how does one query the fillColor of a selected compoundPath when the pathItems.length is zero?

                                       

                                      I am stumped, and this issue is causing issue if a few of my scripts, so any suggestions what is going on would be great. I am staring to think this path was authored outside Illustrator... and Illustrator is unable to populate the pathItems array inside the object.

                                       

                                      Thanks!

                                      Scott

                                       

                                      cmpPathLen0.JPG

                                      • 16. Re: Accessing the elements of a page item
                                        renél80416020 Level 3

                                        CompoundPathItem
                                        A compound path. These objects are composed of multiple intersecting paths, resulting in transparent
                                        interior spaces where the component paths overlap. The pathItems property provides access to the paths
                                        that make up the compound path.
                                        Paths contained within a compound path or group in a document are returned as individual paths when a
                                        script asks for the paths contained in the document. However, paths contained in a compound path or
                                        group are not returned when a script asks for the paths in a layer that contains the compound path or
                                        group.
                                        All paths within a compound path share property values. Therefore, if you set the value of a property of
                                        any one of the paths in the compound path, the properties of all other component paths are updated with
                                        the new value.

                                        • 17. Re: Accessing the elements of a page item
                                          Silly-V Adobe Community Professional

                                          You must have the situation where your compound path contains a group. This is a bug for all us scripters as per what you are dealing with. There were some threads here on this issue:

                                          Grouped paths inside of compound paths screws up script

                                          Re: Dealing with Compound Paths made of Groups

                                           

                                          So, one way to deal with this is to use a Flatten Transparency action which de-bugs all the items in such a way - if you have lots of stuff and don't care about what Flatten will do to the structure.

                                          Another way is to use the plugin mentioned inside "Re: Dealing with Compound Paths made of Groups "

                                          Or use the scripting work-arounds I talk about, etc.

                                          • 18. Re: Accessing the elements of a page item
                                            scottRaysNI Level 1

                                            HUGE thanks for everyone providing help! Glad to hear this is a known issue... and I am not crazy.

                                             

                                            Silly-V the issue can be resolved by running the Flatten Transparency, and will need testing to see if this will not cause any issue down stream. It appears Flatten Transparency isn't available on a path as an operation... I am guessing I need to use the app.executeMenuCommand()?

                                             

                                            Incase your curious...I wrote a HSV tool similar to Photoshop for my team, and this issue is the last bug I need to resolve.

                                             

                                            Scott

                                             

                                            shiftHueSatVal.JPG

                                            • 19. Re: Accessing the elements of a page item
                                              Silly-V Adobe Community Professional

                                              It's got to be a recorded action you play back, the good thing is that you can control a lot with it such as converting spots to process and stuff.

                                              • 20. Re: Accessing the elements of a page item
                                                CarlosCanto Adobe Community Professional & MVP

                                                I think it's more of an oversight rather than a bug, probably Adobe didn't think users would turn multiple compundPaths into one compound path. As mentioned above and in the links provided by silly v, the solution to query/set colors should be quite simple.

                                                 

                                                - move a copy of your compound path to top-most position

                                                - query/set color of document's 1st pathItem

                                                - move copy on above or below original compound path

                                                - remove original compound path

                                                • 21. Re: Accessing the elements of a page item
                                                  Silly-V Adobe Community Professional

                                                  I can't wait until my travels take me to your part of the world, because philosophizing on this and other issues over course of hours and hot tea would be an amazing dream come true..

                                                  But since we're here, I would say the oversight is synonymous with 'bug' because to me this is actual evidence of issues regarding the nomenclature and function behind the scenes.

                                                  1. Obviously, the software allows groups to be encased into compound paths without any detrimental effect to regular UI-effected operations.
                                                  2. The naming conventions of "compound" and "path" implies that it is only paths which are allowed inside this kind of item. How the scripting api refers to these via code (no compoundPathItem.groupItems) could be a result of whomever making the api following the naming conventions to produce their scripting conventions.
                                                  3. Therefore, in my opinion they created a bug through their oversight.. so I guess it's one in the same; if we were to follow the literal description of 'bug' as software acting against expected results, I am seeing it in the way of their internal conflict/mistake on what the expected results should be (ie: if they were aware,  they may have added groupItems to the compoundPathItem object - or the compoundPathItems in case of multiple compound paths)
                                                  • 22. Re: Accessing the elements of a page item
                                                    CarlosCanto Adobe Community Professional & MVP

                                                    really? you want to spend hours philosophizing about semantics? there are other more important topics to talk about, like the World Cup.

                                                    • 23. Re: Accessing the elements of a page item
                                                      CarlosCanto Adobe Community Professional & MVP

                                                      are you coming to MAX?

                                                      • 24. Re: Accessing the elements of a page item
                                                        Silly-V Adobe Community Professional

                                                        Sorry, I do not watch sports ball, but who knows you may awaken the old Russian spirit there.

                                                        No, not MAX - but I always come to the CreativePRO week which has a dev summit on Fridays, next one is 2019 in Seattle!

                                                        • 25. Re: Accessing the elements of a page item
                                                          CarlosCanto Adobe Community Professional & MVP

                                                          it's ok, we can talk about bugs vs oversights we're developers anyways we have our fare share of them in our programs.

                                                           

                                                          CreativePRO is getting closer to home