• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Color Filling PathItems based on name containing PathItems

New Here ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

382

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 20, 2017 Jun 20, 2017

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 ca

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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;

                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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2017 Jun 20, 2017

Copy link to clipboard

Copied

LATEST

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. 😃

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines