Copy link to clipboard
Copied
I have a script that needs to perform a variety of functions, and one part I am stuck on is getting the script to delete anything that's not black. Everything else in the script seems to be doing what it needs to but heres the part that keeps getting messed up:
var myDoc=app.activeDocument;
app.executeMenuCommand ('selectall');
var sel = myDoc.Selection
for (var i = 0; i < sel.length; i++) {
var item = sel.pathItems;
if (item.fillColor.black = 100)
{}
else {app.executeMenuCommand('clear');}
}
so im wanting it to cycle through each path in the selection, identify if its fill is black and delete it if its not.
Any Thoughts?
Thanks!
Copy link to clipboard
Copied
This isn't isn't an inherently straightforward process because of the different objects you may have in your selection array.
Consider that fillColor is not a property of groupItem, compoundPathItem, or placedItem. You need to have some kind of check to make sure you're looking at something that actually has a fillColor property associated with it. And because of the potential complexities that come along with a virtually unlimited ability to nest items, you invariably need some recursion to properly traverse the layer structure to identify all of the black items or all of the items that are not black.
Another less involved option is to draw a temporary shape and fill it with the color that you want to keep (in this case, black).
then (assuming you're using CS6 or newer) you can use the executeMenuCommand() method to select same fill, like so:
var mySwatch = app.activeDocument.swatches["Black"];
var tmpBlock = app.activeDocument.pathItems.rectangle(0,0,10,10);
tmpBlock.fillColor = mySwatch.color;
//select everything that has a fill color that matches mySwatch
app.executeMenuCommand("Find Fill Color menu item");
//select inverse to get everything that is NOT filled with black.
app.executeMenuCommand("Inverse menu item");
//remove the selected artwork.
for(var x= app.activeDocument.selection.length-1;x>=0;x--)
{
app.activeDocument.selection
}
tmpBlock.remove();
Copy link to clipboard
Copied
Salut William,
IL manque tmpBlock.selected;
LR
Copy link to clipboard
Copied
good call. thanks!
Copy link to clipboard
Copied
Salut !
Puisque tu as écrit (var item = sel.pathItems😉 je suppose que tu veux examiner les objets de type PathItem.
Je corrige strictement ce que tu proposes sans rien ajouter pour que ça fontionne.
Remarque : Color.black --> "CMYKColor" sinon résultat "undefined"
//-----------------------------------
var myDoc=app.activeDocument;
var sel = myDoc.pathItems; // A collection of PathItem objects (myDoc)
for (var i = sel.length-1; i >= 0; i--) {
var item = sel;
if (item.fillColor.typename == "CMYKColor") {
if (item.fillColor.black == 100) {
alert('myDoc.pathItems['+i+'] black = 100')
}
else {item.remove();}
}
}
//------------------------------------
de LR
Copy link to clipboard
Copied
Thank you both for taking a look at this. I have not had a chance to play with either solution just yet, probably wont be able to get to it until later this week. However I was using the selection as the templates use some things for screen printing that are CMYK registration colors. I have the script lock those layers and then select the layers it needs.
Do you think that your method using "sel mydoc.pathitems" will see and grab things that are hiding in the locked layers?
Merci!
Copy link to clipboard
Copied
You cannot access items of a locked layer with myDoc.selection. that's the main reason for having a locked property.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now