Copy link to clipboard
Copied
Hello All,
I have written the script to change the color of the selected path items, but its changing only the objects that are "path" type only to desired color. If the selected items are grouped or compound means its not changing to the desired color.
Please help me to ovecome this situation. Also I am attaching the script written by me.
var docRef = app.activeDocument;
var SelectedObjects = docRef.selection;
var col = prompt("Either you want change to Keyline Blue or White");
if (col == 'Keyline Blue') {
try
{
var color1 = new CMYKColor();
color1.cyan = 100;
color1.magenta = 0;
color1.yellow = 0;
color1.black = 0;
var spot = docRef.spots.add();
spot.color = color1;
spot.colorType = ColorModel.SPOT;
spot.name = "Keyline Blue";
}
catch (e) {}
var keyline = docRef.swatches.getByName('Keyline Blue');
for (var a = 0; a < SelectedObjects.length; a++) {
if (SelectedObjects[a].stroked & SelectedObjects[a].filled ){
SelectedObjects[a].strokeColor = keyline.color;
SelectedObjects[a].fillColor = keyline.color;
}
}
for (var a = 0; a < SelectedObjects.length; a++) {
if (SelectedObjects[a].stroked){
SelectedObjects[a].strokeColor = keyline.color;
}
}
for (var a = 0; a < SelectedObjects.length; a++) {
if (SelectedObjects[a].filled){
SelectedObjects[a].fillColor = keyline.color;
}
}
}
if (col == 'White')
{
var color1 = new CMYKColor();
color1.cyan = 0;
color1.magenta = 0;
color1.yellow = 0;
color1.black = 0;
for (var a = 0; a < SelectedObjects.length; a++) {
if (SelectedObjects[a].stroked & docRef.pathItems[a].filled ){
SelectedObjects[a].strokeColor = color1;
SelectedObjects[a].fillColor = color1;
}
}
for (var a = 0; a < SelectedObjects.length; a++) {
if (SelectedObjects[a].stroked){
SelectedObjects[a].strokeColor = color1;
}
}
for (var a = 0; a < SelectedObjects.length; a++) {
if (SelectedObjects[a].filled){
SelectedObjects[a].fillColor = color1;
}
}
}
See if this works. I've used recursion to pass through nested items. I've removed the spot color part for my benefit.
var docRef = app.activeDocument;
var selectedObjects = docRef.selection;
var col = prompt("Either you want change to CMYK Cyan or White");
if (col == 'CMYK Cyan') {
var color1 = new CMYKColor();
color1.cyan = 100;
color1.magenta = color1.yellow = color1.black = 0;
colorise(selectedObjects, color1)
} else if (col == 'White') {
var color1 = new CMYKColor();
...
defaultFillColor (and defaultStrokeColor) property applies the supplied color to the selected objects
app.activeDocument.defaultFillColor = idoc.swatches[3].color; // the 4th swatch
//app.activeDocument.defaultFillColor = color1; // don't need to make a spot or a swatch if not needed
// app.activeDocument.defaultFillColor = idoc.swatches["Keyline Blue"].color; // a spot swatch
Copy link to clipboard
Copied
See if this works. I've used recursion to pass through nested items. I've removed the spot color part for my benefit.
var docRef = app.activeDocument;
var selectedObjects = docRef.selection;
var col = prompt("Either you want change to CMYK Cyan or White");
if (col == 'CMYK Cyan') {
var color1 = new CMYKColor();
color1.cyan = 100;
color1.magenta = color1.yellow = color1.black = 0;
colorise(selectedObjects, color1)
} else if (col == 'White') {
var color1 = new CMYKColor();
color1.cyan = color1.magenta = color1.yellow = color1.black = 0;
colorise(selectedObjects, color1)
}
function colorise(items, color1) {
var array = [];
function push(items) {
for (var i = 0; i < items.length; i++) {
if (items[i].typename == "PathItem") {
array.push(items[i]);
} else if (items[i].typename == "GroupItem") {
push(items[i].pageItems);
} else if (items[i].typename == "CompoundPathItem") {
push(items[i].pathItems);
}
}
}
push(items);
for (var i = 0; i < array.length; i++) {
if (array[i].stroked) array[i].strokeColor = color1;
if (array[i].filled) array[i].fillColor = color1;
}
}
Copy link to clipboard
Copied
defaultFillColor (and defaultStrokeColor) property applies the supplied color to the selected objects
app.activeDocument.defaultFillColor = idoc.swatches[3].color; // the 4th swatch
//app.activeDocument.defaultFillColor = color1; // don't need to make a spot or a swatch if not needed
// app.activeDocument.defaultFillColor = idoc.swatches["Keyline Blue"].color; // a spot swatch