Highlighted

colorFill certain pathItems and toggle visibility via script

Explorer ,
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!!! 🙂

Adobe Community Professional
Correct answer by DilliamWowling | Adobe Community Professional

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

TOPICS
Scripting

Views

107

Likes

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

colorFill certain pathItems and toggle visibility via script

Explorer ,
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!!! 🙂

Adobe Community Professional
Correct answer by DilliamWowling | Adobe Community Professional

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

TOPICS
Scripting

Views

108

Likes

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
Oct 05, 2020 1
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Oct 05, 2020 5
Explorer ,
Oct 06, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Oct 06, 2020 1
Adobe Community Professional ,
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.

Likes

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
Reply
Loading...
Oct 05, 2020 5
Explorer ,
Oct 06, 2020

Copy link to clipboard

Copied

Just wow!!!! Thank you very much for your time and effort @DilliamWowling . 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 🙂

Likes

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
Reply
Loading...
Oct 06, 2020 2
Adobe Community Professional ,
Oct 08, 2020

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Oct 08, 2020 4