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

colorFill certain pathItems and toggle visibility via script

Explorer ,
Oct 05, 2020 Oct 05, 2020

Copy link to clipboard

Copied

Hey guys,

 

as I´m still pretty new to scripting I once again reach out for your help. I got several files with the same structure and have to change the fillcolor of some layers and toggle their visibility. Here is a screenshot of one of those files (and the link TestFile ).

 

Pic1.jpg

 

What I would like to do is fill the very last PathItem (red circle) of each group in the layer "OuterDesign" with the color C=71, M=6, Y=30 and K=17. In the layer "InnerDesign" also the last PathItem of each group should be filled but now with the swatch named "Zebra". At last the visibility of the layers "NonAdjustable" and "Adjustable" should be toggled so that "NonAdjustable" is invisible and "Adjustable" is visible.

 

But kudos to anyone that will help!!! 🙂

TOPICS
Scripting

Views

333

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 2 Correct answers

Community Expert , Oct 05, 2020 Oct 05, 2020

Hi,

For your sample test file, please try following code

 

function main() {
    var doc = app.activeDocument;
    
    var outerDesignLayer = doc.layers.getByName('OuterDesign');
    var _groupItems = outerDesignLayer.groupItems;
    for (var i = 0; i < _groupItems.length; i++) {
        var _lastPathItem = _groupItems[i].pageItems[_groupItems[i].pageItems.length-1];
        var color = new CMYKColor();
        color.cyan = 71;
        color.magenta = 6;
        color.yellow = 30;
        color
...

Votes

Translate

Translate
Community Expert , Oct 05, 2020 Oct 05, 2020

//finding the last item in a list

groups and layers are "container objets" that list their children as an array. so if you wanted to get the first pageItem of a layer, you'd do so like this:

var myLayer = app.activeDocument.layers["OuterDesign"];
var firstItem = myLayer.pageItems[0]; //remember that arrays are zero indexed

if you wanted to get the 3rd item in the array, use

var thirdItem = myLayer.pageItems[2];

To get the last item in the array, you either need to know exactly how many elements

...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 05, 2020 Oct 05, 2020

Copy link to clipboard

Copied

Hi,

For your sample test file, please try following code

 

function main() {
    var doc = app.activeDocument;
    
    var outerDesignLayer = doc.layers.getByName('OuterDesign');
    var _groupItems = outerDesignLayer.groupItems;
    for (var i = 0; i < _groupItems.length; i++) {
        var _lastPathItem = _groupItems[i].pageItems[_groupItems[i].pageItems.length-1];
        var color = new CMYKColor();
        color.cyan = 71;
        color.magenta = 6;
        color.yellow = 30;
        color.black = 17;
        _lastPathItem.fillColor = color;
    }

    var innerDesignLayer = doc.layers.getByName('InnerDesign');
    var _groupItems = innerDesignLayer.groupItems;
    for (var i = 0; i < _groupItems.length; i++) {
        var _lastPathItem = _groupItems[i].pageItems[_groupItems[i].pageItems.length-1];    
        _lastPathItem.fillColor = doc.swatches.getByName('Zebra').color;
    }

    var nonAdjustableLayer = doc.layers.getByName('NonAdjustable');
    nonAdjustableLayer.visible = false;

    var adjustableLayer = doc.layers.getByName('Adjustable');
    adjustableLayer.visible = true;

}

main();

 

NOTE: For other documents, if the file is setup like this only, the script will work for all cases. But if there is any change in other document, (for eg: Change in layer name) it may possible script will not work. So just a suggestion, instead of changing script again and again its better to create a workflow so that it will works for all documents if you have more.

 

Best regards

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
Explorer ,
Oct 06, 2020 Oct 06, 2020

Copy link to clipboard

Copied

Thank you very much! Works perfect and I definitely will keep your suggestion in mind 🙂

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 ,
Oct 05, 2020 Oct 05, 2020

Copy link to clipboard

Copied

//finding the last item in a list

groups and layers are "container objets" that list their children as an array. so if you wanted to get the first pageItem of a layer, you'd do so like this:

var myLayer = app.activeDocument.layers["OuterDesign"];
var firstItem = myLayer.pageItems[0]; //remember that arrays are zero indexed

if you wanted to get the 3rd item in the array, use

var thirdItem = myLayer.pageItems[2];

To get the last item in the array, you either need to know exactly how many elements are there, or you can caculate it by using the "length" property of an array. length just meansthe number of elements. So if there are 10 items in your array, and you want to get the last item, instead of putting a specific number in the brackets like we did above, you can write a statement tht will evaluate to the correct number, like so:

var lastItem = myLayer.pageItems[myLayer.pageItems.length-1];

myLayer.pageItems.length is 10 in this case, but since arrays are zero indexed, you need to subtract one or you'll get an array out of bounds error becuse if you tried to access myLayer.pageItems[10] you'd be looking for a non-existent 11th element.

 

 

//setting fill color

There are a couple of ways to do this. the easiest way in my opinion is to apply a pre-existing swatch. but you can adjust the fill color manually by setting each color value by code. I'll show you both ways. Disclaimer: there are things to consider when changing fill colors of items, especially if you're accessing those items like we did above (by index in an array). You need to either be very confident that you're accessing the right item and the right type of item, or you need to build in error handling. I won't go into the error handling here, but if you want we can discuss it separately. For now I'll assume that we are accessing the correct pathItem (and it's not a group, raster image, compound path, symbol, or other such item that doesn't directly have a fillColor property). I used different variable names above to indicate which item we were getting, but below i'm just going to refer to the item in question as "myItem" since it doesn't matter whether it's the first item, last item, or anything in between.

 

var docRef = app.activeDocument;
var swatches = docRef.swatches; //save the document swatches to a variable
var myItem; //pretend there's logic here to get the correct item and save it to this variable

//now let's get the correct swatch from the library. you can do this by index since swatches are presented as an array, or you can do this by selecting a swatch by name

//var mySwatch = swatches[5]; //this gets the 6th swatch in the swatch library
var mySwatch = swatches["Zebra"]; //this gets the swatch in the library called zebra. but be careful, if that swatch doesn't exist, you'll error out and the script will stop.

//now let's apply the swatch to the item
myItem.fillColor = mySwatch.color;

//and here's what it looks like if you wanted to just set the color values manually
myItem.fillColor.cyan = 52; //number between 0 and 100
myItem.fillColor.magenta = 26; //number between 0 and 100
//etc for all 4 colors (or 3 if you're using RGB).

 

 

 

//toggling layer visibility

This one is a piece of cake.

var myLayer = layers["Adjustable"];

//to make a layer invisible
myLayer.visible = false;

//to make a layer visible
myLayer.visible = true;

//now of course that only helps if you know exactly which state you want the layer to be in. but that's not really the case when you're toggling, so you can just add a condition to make it toggle on and off like this:

if(myLayer.visible)
{
    myLayer.visible = false;
}
else
{
    myLayer.visible = true;
}

 

 

Welp anyway. that's the basics. If you have any questions, concerns or if anything just doesn't make any sense, feel free to come on back with any questions you may have.

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
Explorer ,
Oct 06, 2020 Oct 06, 2020

Copy link to clipboard

Copied

Just wow!!!! Thank you very much for your time and effort @Disposition_Dev . THIS is exactly what somebody needs to get a better understanding of coding. Especially "tricks" like

var lastItem = myLayer.pageItems[myLayer.pageItems.length-1];

 I would have never come up with such a smart move 😄
Thanks again, you helped me a lot 🙂

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 ,
Oct 08, 2020 Oct 08, 2020

Copy link to clipboard

Copied

LATEST

I have faith that you would have figured it out. But if ever I can help, I'm ready and rarin' to do so.

 

As I tell everyone I encounter here, I learned everything I know from the helpful folks on this forum, and I've turned that knowledge into a career. I'm always gleeful at the opportunity to help teach someone else who wants to learn. Many folks come around just demanding custom scripts. But some folks are hungry for knowledge, and you seem to be in that second camp. I owe a ton to the people on this forum and I'm always ready to pay it forward. 

 

Please come on back at any time with your questions. Either in a post here or in a DM.

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