Skip to main content
Participant
August 31, 2022
Answered

Step & Repeat with color changing action

  • August 31, 2022
  • 2 replies
  • 1122 views

Hi all,

I would like to know how to create a script with the following. Usually, we will get one file for production and needs to create two different colors and needs to step and repeat the same as attached. This one I'm doing manually it would be helpful if someone help me with some actions.

 

Final output required.

This topic has been closed for replies.
Correct answer femkeblanco

The script below provides two ways to choose the colors of the second and third columns.  Either way you will need to select the items to color individually.  I've made some assumptions, such that coloring is applied to fills only.  The first two lines of the script contain the numbers of the rows and columns, which can be changed.  Please note that I've not tested it thoroughly or thought beyond the basics. 

 

The first way needs you to select swatches to color with.  I suggest holding down the CTRL key, selecting (1) the items to color in the layers panel and (2) the swatches, and then running the script. 

 

 

var columnns = 3;
var rows = 12;
var doc = app.activeDocument;
// get colors, first way
var swatches = doc.swatches.getSelected();
var colors = [];
for (var i = 0; i < swatches.length; i++) {
    colors.push(swatches[i].color);
}
// // get colors, second way
// var input1 = prompt("Enter hex codes (without #) separated by comma.", "");
// input2 = input1.split(",");
// var color1 = hexToRGB(input2[0]);
// var color2 = hexToRGB(input2[1]);
// var colorA = new RGBColor();
// colorA.red = color1.red;
// colorA.green = color1.green;
// colorA.blue = color1.blue;
// var colorB = new RGBColor();
// colorB.red = color2.red;
// colorB.green = color2.green;
// colorB.blue = color2.blue;
// function hexToRGB(hex) {
//    return {red: parseInt(hex.substr(0, 2), 16), 
//            green: parseInt(hex.substr(2, 2), 16), 
//            blue: parseInt(hex.substr(4, 2), 16)};
// }
// var colors = [colorA, colorB];
// tag selected items, i.e. items to be colored
for (var i = 0; i < app.selection.length; i++) {
    var tag1 = app.selection[i].tags.add();
    tag1.name = "tag1";
    tag1.value = "true";
}
// select & groups all items on layer
app.selection[0].layer.hasSelectedArtwork = true;
var object = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
    doc.selection[i].moveToEnd(object);
}
// duplicate, translate and colorise
var w = object.width;
var h = object.height;
var x = object.position[0];
for (var i = 0; i < columnns; i++) {
    var y = object.position[1];
    for (var j = 0; j < rows; j++) {
        var replica = object.duplicate(doc.layers[0], ElementPlacement.PLACEATEND);
        replica.position = [x, y];
        if (i > 0) {
            colorise(replica.pageItems);
            function colorise(items) {
                for (var z = 0; z < items.length; z++) {
                    if (items[z].typename == "PathItem") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].fillColor = colors[i - 1];
                    } else if (items[z].typename == "TextFrame") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].textRange.fillColor = colors[i - 1];
                    } else {
                        colorise(items[z].pageItems);
                    }
                }
            }
        }
        y = y - h;
    }
    x = x + w;
}

 

 

 

The second way needs you to enter hex codes.  Select the items to color in the layers panel, and then run the script.  There will probably be color loss this way, so the first way is preferred. 

 

 

 

 

var columnns = 3;
var rows = 12;
var doc = app.activeDocument;
// // get colors, first way
// var swatches = doc.swatches.getSelected();
// var colors = [];
// for (var i = 0; i < swatches.length; i++) {
//     colors.push(swatches[i].color);
// }
// get colors, second way
var input1 = prompt("Enter hex codes (without #) separated by comma.", "");
input2 = input1.split(",");
var color1 = hexToRGB(input2[0]);
var color2 = hexToRGB(input2[1]);
var colorA = new RGBColor();
colorA.red = color1.red;
colorA.green = color1.green;
colorA.blue = color1.blue;
var colorB = new RGBColor();
colorB.red = color2.red;
colorB.green = color2.green;
colorB.blue = color2.blue;
function hexToRGB(hex) {
   return {red: parseInt(hex.substr(0, 2), 16), 
           green: parseInt(hex.substr(2, 2), 16), 
           blue: parseInt(hex.substr(4, 2), 16)};
}
var colors = [colorA, colorB];
// tag selected items, i.e. items to be colored
for (var i = 0; i < app.selection.length; i++) {
    var tag1 = app.selection[i].tags.add();
    tag1.name = "tag1";
    tag1.value = "true";
}
// select & groups all items on layer
app.selection[0].layer.hasSelectedArtwork = true;
var object = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
    doc.selection[i].moveToEnd(object);
}
// duplicate, translate and colorise
var w = object.width;
var h = object.height;
var x = object.position[0];
for (var i = 0; i < columnns; i++) {
    var y = object.position[1];
    for (var j = 0; j < rows; j++) {
        var replica = object.duplicate(doc.layers[0], ElementPlacement.PLACEATEND);
        replica.position = [x, y];
        if (i > 0) {
            colorise(replica.pageItems);
            function colorise(items) {
                for (var z = 0; z < items.length; z++) {
                    if (items[z].typename == "PathItem") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].fillColor = colors[i - 1];
                    } else if (items[z].typename == "TextFrame") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].textRange.fillColor = colors[i - 1];
                    } else {
                        colorise(items[z].pageItems);
                    }
                }
            }
        }
        y = y - h;
    }
    x = x + w;
}

 

 

 

2 replies

femkeblanco
femkeblancoCorrect answer
Legend
September 4, 2022

The script below provides two ways to choose the colors of the second and third columns.  Either way you will need to select the items to color individually.  I've made some assumptions, such that coloring is applied to fills only.  The first two lines of the script contain the numbers of the rows and columns, which can be changed.  Please note that I've not tested it thoroughly or thought beyond the basics. 

 

The first way needs you to select swatches to color with.  I suggest holding down the CTRL key, selecting (1) the items to color in the layers panel and (2) the swatches, and then running the script. 

 

 

var columnns = 3;
var rows = 12;
var doc = app.activeDocument;
// get colors, first way
var swatches = doc.swatches.getSelected();
var colors = [];
for (var i = 0; i < swatches.length; i++) {
    colors.push(swatches[i].color);
}
// // get colors, second way
// var input1 = prompt("Enter hex codes (without #) separated by comma.", "");
// input2 = input1.split(",");
// var color1 = hexToRGB(input2[0]);
// var color2 = hexToRGB(input2[1]);
// var colorA = new RGBColor();
// colorA.red = color1.red;
// colorA.green = color1.green;
// colorA.blue = color1.blue;
// var colorB = new RGBColor();
// colorB.red = color2.red;
// colorB.green = color2.green;
// colorB.blue = color2.blue;
// function hexToRGB(hex) {
//    return {red: parseInt(hex.substr(0, 2), 16), 
//            green: parseInt(hex.substr(2, 2), 16), 
//            blue: parseInt(hex.substr(4, 2), 16)};
// }
// var colors = [colorA, colorB];
// tag selected items, i.e. items to be colored
for (var i = 0; i < app.selection.length; i++) {
    var tag1 = app.selection[i].tags.add();
    tag1.name = "tag1";
    tag1.value = "true";
}
// select & groups all items on layer
app.selection[0].layer.hasSelectedArtwork = true;
var object = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
    doc.selection[i].moveToEnd(object);
}
// duplicate, translate and colorise
var w = object.width;
var h = object.height;
var x = object.position[0];
for (var i = 0; i < columnns; i++) {
    var y = object.position[1];
    for (var j = 0; j < rows; j++) {
        var replica = object.duplicate(doc.layers[0], ElementPlacement.PLACEATEND);
        replica.position = [x, y];
        if (i > 0) {
            colorise(replica.pageItems);
            function colorise(items) {
                for (var z = 0; z < items.length; z++) {
                    if (items[z].typename == "PathItem") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].fillColor = colors[i - 1];
                    } else if (items[z].typename == "TextFrame") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].textRange.fillColor = colors[i - 1];
                    } else {
                        colorise(items[z].pageItems);
                    }
                }
            }
        }
        y = y - h;
    }
    x = x + w;
}

 

 

 

The second way needs you to enter hex codes.  Select the items to color in the layers panel, and then run the script.  There will probably be color loss this way, so the first way is preferred. 

 

 

 

 

var columnns = 3;
var rows = 12;
var doc = app.activeDocument;
// // get colors, first way
// var swatches = doc.swatches.getSelected();
// var colors = [];
// for (var i = 0; i < swatches.length; i++) {
//     colors.push(swatches[i].color);
// }
// get colors, second way
var input1 = prompt("Enter hex codes (without #) separated by comma.", "");
input2 = input1.split(",");
var color1 = hexToRGB(input2[0]);
var color2 = hexToRGB(input2[1]);
var colorA = new RGBColor();
colorA.red = color1.red;
colorA.green = color1.green;
colorA.blue = color1.blue;
var colorB = new RGBColor();
colorB.red = color2.red;
colorB.green = color2.green;
colorB.blue = color2.blue;
function hexToRGB(hex) {
   return {red: parseInt(hex.substr(0, 2), 16), 
           green: parseInt(hex.substr(2, 2), 16), 
           blue: parseInt(hex.substr(4, 2), 16)};
}
var colors = [colorA, colorB];
// tag selected items, i.e. items to be colored
for (var i = 0; i < app.selection.length; i++) {
    var tag1 = app.selection[i].tags.add();
    tag1.name = "tag1";
    tag1.value = "true";
}
// select & groups all items on layer
app.selection[0].layer.hasSelectedArtwork = true;
var object = doc.groupItems.add();
for (var i = 0; i < doc.selection.length; i++) {
    doc.selection[i].moveToEnd(object);
}
// duplicate, translate and colorise
var w = object.width;
var h = object.height;
var x = object.position[0];
for (var i = 0; i < columnns; i++) {
    var y = object.position[1];
    for (var j = 0; j < rows; j++) {
        var replica = object.duplicate(doc.layers[0], ElementPlacement.PLACEATEND);
        replica.position = [x, y];
        if (i > 0) {
            colorise(replica.pageItems);
            function colorise(items) {
                for (var z = 0; z < items.length; z++) {
                    if (items[z].typename == "PathItem") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].fillColor = colors[i - 1];
                    } else if (items[z].typename == "TextFrame") {
                        if (items[z].tags.length > 0 && 
                            items[z].tags["tag1"].value == "true")
                            items[z].textRange.fillColor = colors[i - 1];
                    } else {
                        colorise(items[z].pageItems);
                    }
                }
            }
        }
        y = y - h;
    }
    x = x + w;
}

 

 

 

Participant
September 6, 2022

REALLY THANK YOU SO MUCH FOR YOUR EFFORT AND THIS IS WHAT WE REQUIRED. BIG SALUTE TO YOU!!!

femkeblanco
Legend
August 31, 2022

Further details are required regarding the anatomy of your item (as a minimum, you will need to show your fully expanded layers panel for your item) and the colors (for example, how you intend to choose them).  

Participant
August 31, 2022

This one-up file will come by production as a single layer (attached). Also, I need  14 colors of each color.

color codes:

48C8B7, 005794, 233856.

 

Actually, these designs are from our website and customers will design online our backend team will generate the base design with one color and I need to step and repeat manually and needs to add two more colors as per the color code provided. please help me out if you need further information I will share you the one up .ai file.

Participant
August 31, 2022