Hey guys,
I am trying to write a script that loops through swatches from File B, apllies them to certain items in File A and then saves File A. That´s it (basically a patchwork of some other scripts):
#target illustrator
function main(){
var docRef = app.documents[0];
var docRef1 = app.open(File('~/RedBlackWhite.ai'));
for(var p=2; p<docRef1.swatches.length; p++){
//p=2 because of the two default swatches [None] and [Registration]
var Designcolor = docRef1.swatches[p];
// Add Swatch from docRef1 to docRef
var AddColor = docRef.swatches.add();
AddColor.name = Designcolor.name;
AddColor.color = Designcolor.color;
//apply swatch color to pieces
for (var i = 0; i < docRef.layers[0].groupItems.length; i++) {
var docItem = docRef.layers[0].groupItems[i];
var lastPathItem = docItem.pageItems[docItem.pageItems.length-1];
lastPathItem.fillColor = docRef.swatches[2].color; //[2] also because of the default swatches
}
//create a new folder
var Model = "Shirt"
var folder = new Folder ("/~/Desktop/" + Model)
if(!folder.exists == true){
folder.create()
}
//saves the document
if (app.documents.length > 0) {
var path1 = ("/~/Desktop/" + Model)
var file1 = new File(path1 + "/" + Model + "_" + Designcolor.name + "_group")
var saveOptions = new IllustratorSaveOptions();
saveOptions.embedLinkedFiles = true;
saveOptions.compatibility = Compatibility.ILLUSTRATOR24;
saveOptions.compressed = true;
saveOptions.pdfCompatible = true;
docRef.saveAs(file1, saveOptions);
}
//delete swatch[2]
docRef.swatches[2].remove();
}
}
main();
It works as long as the swatches in docRef1 only contain colors. But if the swatches conain patterns, it gets a little messed up. When I try to open one of the files this message shows up, and the items seem to be damaged:
Also does this part of the script
docRef.swatches[2].remove();
now remove swatches from docRef1 and changes some of their names to "unnamed pattern".
As I´m still very new to scripting I don´t know why this happens (maybe my script is complete garbage 😄 ). If any of you guys coulp help me with this problem I would once again really really appreciate it 🙂 🙂
Here are the files:
docRef ; docRef1 (file with only colors) ; docRef1 (file with patterns)