Copy link to clipboard
Copied
I'm writing a script to copy selected items onto another layer, then change the fill and stroke of the resulting items, so that they all have no fill, and the same stroke colour. It's mostly working – the duplication part works fine, but looping through an array of the resulting items in order to alter the properties of each item only seems to affect the last item in the array.
main();
function main() {
//Check if there is a document
if (app.documents.length == 0) {
return;
} else {
//Check Cutter layer exists, get reference to it if it does
var doc = app.activeDocument;
var currentLayer = doc.activeLayer;
if (existCheck(doc.layers, "Cutter")) {
var cutterLayer = doc.layers.getByName("Cutter");
//Get reference to selection (as Array)
var mySelection = app.selection;
//Iterate through array of selected objects, duplicating each one onto Cutter layer
for (var i = 0; i < mySelection.length; i++) {
mySelection.duplicate(cutterLayer, ElementPlacement.PLACEATEND);
}
currentLayer.hasSelectedArtwork = false;
var myPathItems = cutterLayer.pathItems;
for (var i = 0; i < myPathItems.length; i++) {
myPathItems.filled = false;
myPathItems.stroked = true;
myPathItems.strokeWidth = 1;
myPathItems.strokeColor = doc.swatches.getByName("Cutter").color;
}
}
}
}
//Function to check if a named item exists in an array
function existCheck(myArray, name) {
for (i = 0; i < myArray.length; i++) {
if (myArray.name == name) {
return true;
} else {
return false;
}
}
}
The code assumes there is already a layer called "Cutter" (and a swatch called "Cutter"), and that some items on the current layer (that we're duplicating from) are selected.
I would have expected all of the referenced pathItems to have a stroke colour of the swatch "Cutter", and no fill – but only the last item does. The other items have no fill or stroke.
Copy link to clipboard
Copied
your script works fine here, win 10, CC2019.
try restarting Illustrator
Copy link to clipboard
Copied
Thanks. I've restarted Illustrator, but still get the same result. This is using Illustrator CC 2019 23.0.4 on OSX 10.14.5.
The statement affecting the fill is working correctly, affecting all the items in the array, but the statements affecting the stroke only affect the last item, and I'm not sure why.
Copy link to clipboard
Copied
Are you sure, all items are pathItems?
If they are compoundPathItems it's not getting them, as you are only selecting pathItems.
Had similar problem while was preparing technical drawings.
App.selection returns collection of pageItems, not only pathItems, so all selected items no matter what type, are copied
But then you catch layer.pathItems and it's not equal with what app.selection might return.
Copy link to clipboard
Copied
essayez d'insérer en fin de boucle ceci,
myPathItems.strokeColor = doc.swatches.getByName("Cutter").color;
redraw();alert(myPathItems.strokeColor);
}
afin de voir ce qui se passe?
Copy link to clipboard
Copied
You can upload file to somewhere?
Copy link to clipboard
Copied
Thanks for the replies, everyone.
I'm sorry that I've not replied sooner, but I've been away on holiday. Bizarrely, since I got back, the script has now started working correctly, without me having changed either the script or the documents I was testing it on. Again, I've no idea why it wasn't working, or why it now is.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more