• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Batch replace color with another color

New Here ,
Feb 05, 2010 Feb 05, 2010

Copy link to clipboard

Copied

Hi,

I would desperately need some assistance in trying to accomplish batch editing of ai-files via JavaScript.

What I would need to do, is go through a number (many!) of files, find items with certain swatch color (referenced by its name), and replace the item-fill with another swatch-color (also referenced by its name). Then save the document with another name.

Other parts I have managed to do, except find/compare/modify the fill color of an item.

I have gone through these forums and tried a number of things to a point where I am currently very confused and frustrated 

Some details (if it helps any), I'm using Illustrator CS3 and JavaScript.

Here's the closest where I got with trying to find a certain color and eventually changing it:

#target illustrator

var sourceFile, findColor, replaceColor;

sourceFile = new File().openDlg('Please select your Illustrator file…');

open(sourceFile);
var docRef = app.activeDocument;

findColor = docRef.swatches.getByName("vih");
// replaceColor not yet used anywhere
replaceColor = docRef.swatches.getByName("sin");

for(var obj = docRef.pageItems.length - 1; obj >= 0; obj--) {
    if(docRef.pageItems[obj].fillColor == findColor) {
        alert("Match found"); //It never seems to get here, so my comparison clearly is not working?
    }
    else {
        alert ("No specified color found!");
    }
}

docRef.close();

(and yes, I'm completely new with java-scripting, so even this is constructed from bits of sample-scripts and other scripts found from these forums)

Please, I would really appreciate any help, this can't really be such a difficult task to do... can it?

BR,

Johanna

TOPICS
Scripting

Views

9.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guru ,
Feb 05, 2010 Feb 05, 2010

Copy link to clipboard

Copied

I don't think you can do a color comparison like this in script. In the GUI you can select an path item and if its fill matches a swatch then its associated with it. Your 'findColor' will return a [swatch Object] which you can't compare to fillColor as this returns [color Object] . Im fairly new to using JavaScript but think you need to dig down deeper to there values to compare? I made only the simplest of test files to try this.

Try something like…

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

var findColor = swatches.getByName('vih').color;

var replaceColor = swatches.getByName('sin').color;

for (var i = 0; i < pathItems.length; i++) {

with (pathItems.fillColor) {

if (cyan == findColor.cyan && magenta == findColor.magenta && yellow == findColor.yellow && black == findColor.black) {

$.writeln('True');

cyan = replaceColor.cyan, magenta = replaceColor.magenta, yellow = replaceColor.yellow, black = replaceColor.black;

} else {

$.writeln('False');

}

}

}

//saveAs(filePath, saveOptions)

}

My example was just swapping on CMYK for another. You may want to check what pageItems you have as NOT all will return you a fillColor?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 05, 2010 Feb 05, 2010

Copy link to clipboard

Copied

Thank you Mark for answering, I tried the code you proposed, and it failed at the 'if' -statement with error "cyan is undefined". After that, I tried adding 'pathItems.fillColor.' in front of each undefined color. It resulted to the script running through, but not changing anything. So.. I still don't know if the comparison works or not

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2010 Feb 06, 2010

Copy link to clipboard

Copied

I only tried a very basic test in my case ALL path items were filled with various CMYK swatches and the swap did take place so the comparison did work. I would suspect that you have a path item that contains NO fill so any of its properties would be undefined. You can also include this in your code too. This should work for both filled and stroked path items in CMYK art.

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

var findColor = swatches.getByName('vih').color;

var replaceColor = swatches.getByName('sin').color;

for (var i = 0; i < pathItems.length; i++) {

if (pathItems.filled == true) {

with (pathItems.fillColor) {

if (cyan == findColor.cyan && magenta == findColor.magenta && yellow == findColor.yellow && black == findColor.black) {

$.writeln('True');

cyan = replaceColor.cyan, magenta = replaceColor.magenta, yellow = replaceColor.yellow, black = replaceColor.black;

} else {

$.writeln('False');

}

}

}

if (pathItems.stroked == true) {

with (pathItems.strokeColor) {

if (cyan == findColor.cyan && magenta == findColor.magenta && yellow == findColor.yellow && black == findColor.black) {

$.writeln('True');

cyan = replaceColor.cyan, magenta = replaceColor.magenta, yellow = replaceColor.yellow, black = replaceColor.black;

} else {

$.writeln('False');

}

}

}

}

//saveAs(filePath, saveOptions)

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

Hi,

I was tried this in Illustrator CS4 with Extendscript toolkit. I am getting some error pls find the attachment.

Thank you

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

And do you have a swatch in the current documents swatches pallet called 'vih'

getByName will fail like this if it does NOT exist…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 12, 2010 Feb 12, 2010

Copy link to clipboard

Copied

Hi Mark,

There is no such swatch in my palette. need to create this swatch or what (Vih, sin)? if yes what color to use for this? and what is the purpose of creating this. Can u explain me little bit clear.

Joe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 13, 2010 Feb 13, 2010

Copy link to clipboard

Copied

Joe, the following lines set a variable reference to the color of 2 swatches. Like I said using getByName() will fail if its NOT there. You can change the names 'vih' & 'sin' to the names of 2 colors that you do have in your swatches pallet. The rest of the script does a compassion of the CMYK values ONLY so both of your swatches need to be this.

var findColor = swatches.getByName('vih').color;

var replaceColor = swatches.getByName('sin').color;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 15, 2010 Feb 15, 2010

Copy link to clipboard

Copied

Hi mark,

The same script i have modified little bit to convert one pantone to another pantone but here i am getting some error PFA.

#target illustrator
var docRef = app.activeDocument;
with (docRef) {
var findColor = swatches.getByName('PANTONE 133 C').color;
var replaceColor = swatches.getByName('PANTONE 101 C').color;
for (var i = 0; i < pathItems.length; i++) {
if (pathItems.filled == true) {
with (pathItems.fillColor) {
if (PANTONE 133 C == findColor.PANTONE 133 C) {
$.writeln('True');
PANTONE 101 C = replaceColor.PANTONE 101 C;
} else {
$.writeln('False');
}
}
}
if (pathItems.stroked == true) {
with (pathItems.strokeColor) {
if (PANTONE 133 C == findColor.PANTONE 133 C) {
$.writeln('True');
PANTONE 101 C = replaceColor.PANTONE 101 C;
} else {
$.writeln('False');
}
}
}
}
//saveAs(filePath, saveOptions)
}
Pls give suggestion. Here i want to find PANTONE 133 C and replace with PANTONE 101 C.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 16, 2010 Feb 16, 2010

Copy link to clipboard

Copied

Im busy at work at the mo… but have a script that I was working on last night (learning JS @ home) that should do this for you. I'll post it L8R…

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 16, 2010 Feb 16, 2010

Copy link to clipboard

Copied

I would expect something like this to be close although I've NOT looked into colored placed items yet…

#target illustrator

var docRef = app.activeDocument;

with (docRef) {

var replaceColor = swatches.getByName('PANTONE 101 C').color;

for (var i = 0; i < pathItems.length; i++) {

with (pathItems) {

if (filled == true && fillColor instanceof SpotColor) {

if (fillColor.spot.name == 'PANTONE 133 C') fillColor = replaceColor;

}

if (stroked == true && strokeColor instanceof SpotColor) {

if (strokeColor.spot.name == 'PANTONE 133 C') strokeColor = replaceColor;

}

}

}

for (var j = 0; j < stories.length; j++) {

with (stories) {

for (var k = 0; k < characters.length; k++) {

with (characters.characterAttributes) {

if (fillColor instanceof SpotColor) {

if (fillColor.spot.name == 'PANTONE 133 C') fillColor = replaceColor;

}

if (strokeColor instanceof SpotColor) {

if (strokeColor.spot.name == 'PANTONE 133 C') strokeColor = replaceColor;

}

}

}

}

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 16, 2010 Feb 16, 2010

Copy link to clipboard

Copied

LATEST

Hi Mark,

Thank you its really working fine...

joe

Votes

Translate

Translate

Report

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