Applying patterns via script damages file

Explorer ,
Nov 04, 2020

Copy link to clipboard


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 ='~/'));

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.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){

    //saves the document 
    if (app.documents.length > 0) {
        var path1 = ("/~/Desktop/" + Model)
        var file1 = new File(path1 + "/" + Model + "_" + + "_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]

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:

Error Message.jpg

Also does this part of the script


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) 








Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Have something to add?

Join the conversation