3 Replies Latest reply on Jun 20, 2017 8:55 AM by williamadowling

    Color Filling PathItems based on name containing PathItems

    markusj4146625

      I'm new to Illustrator and scripting solutions regarding Illustrator therefore my knowledge and explanations might be somewhat lacking.

      Basically I want to select a PathItem based on name and fill it with color. This works for "single" PathItems, but when the PathItem contains Pathitems it doesnt work. Picture:

      lager.PNG

      As seen the "single" PathItems are correcrly colorized, but not the one containing PathItems.

      Relevant code:

       

           docRef = app.activeDocument;

           try{

           frontPath = docRef.pathItems.getByName(kommunKoder[index]);

           frontPath.filled = true;

           frontPath.fillColor = col;       

           }catch(err) { 

           }     

       

      kommunKoder is just an array containing the name correlating to the name of the PathItems. If I iterate through every PathItem based on index I can fill them all, but since I want a specific color for a specific PathItem I want to do this by name. Any easy solution?

        • 1. Re: Color Filling PathItems based on name containing PathItems
          williamadowling Level 4

          So, first off, a pathItem by definition cannot have any children elements. The item you've shown in your screenshot is a group.

           

          When you reach a groupItem, you need to start a new loop that loops the children of that group.

           

          Try out this snippet. I was unable to test it because i don't have the file you're working on or access to the data in the kommunKoder array. But perhaps this will at least give you some insight. GroupItems don't have a fillColor property and you didn't have any code in your catch block so the script attempted to change the color of a group and then failed silently. It's ALWAYS a good idea to put some code in your catch block so you know if something went wrong. Otherwise a script can be nearly impossible to debug. Anyway, here's that snippet. Hope it helps.

           

          function test()
          {
              var docRef = app.activeDocument;
              try{
                  frontPath = docRef.pageItems.getByName(kommunKoder[index]);
                  if(frontPath.typename == "GroupItem")
                  {
                      //item is a group, loop the children
                      for(var x=0;x<frontPath.pageItems.length;x++)
                      {
                          var thisChild = frontPath.pageItems[x];
                          if(thisChild.typename == "PathItem")
                          {
                              thisChild.filled = true;
                              thisChild.fillColor = col;
                          }
                      }
                  }
                  else
                  {
                      frontPath.filled = true;
                      frontPath.fillColor = col;       
                  }
              }catch(err) {
                  alert("Error: " + err + ".");
                  alert(kommunKoder[index] + " was not properly re-colored."); 
              }     
          }
          test();
          
          • 2. Re: Color Filling PathItems based on name containing PathItems
            markusj4146625 Level 1

            Thank you, it works well!

             

            I suspected I needed to loop through it but I didn't know for sure what type I should consider it and was unable to find a way to detect it. While I tried to treat it as a groupItem, I still see, in hindsight with your code, I had other faults that resulted in errors.

            • 3. Re: Color Filling PathItems based on name containing PathItems
              williamadowling Level 4

              Glad i could help.

               

              I'm glad this code works for you, but i would encourage you to think about beefing things up considerably. There are still a ton of vulnerabilities in the above snippet that could cause the script to fall in it's face without any meaningful error messages. Since I changed the frontPath variable to find "pageItems" instead of "pathItems" you will have to contend with the possibility of finding textFrames, compoundPathItems, placedItems etc.

               

              Any time you have questions, come on back and we'll gladly help out. Good luck. =)