Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Need help creating an Indesign Javascript to find and replace colors

New Here ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Hello!

 

After scouring the internet for a few hours, I have come to the conclusion I need help. 

 

I am attempting to find a script that will find an unnamed color in Indesign (C:82, M:100, Y:11, K:0) and replace it with an existing swatch named "GGS purple"

 

I then need the same script to find GGS purple and replace it with the existing Pantone 186 C.

 

I also am looking for a find and replace script for text and the findchangebylist is baffling me as well.

 

This isn't something we do every day, but we have hundreds of documents to process and the find/change function in Indesign is great, but scripts will cut down on time, which matters with this many documents. 

 

Thank you so much in advance!

TOPICS
Scripting

Views

707

Likes

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

correct answers 1 Correct answer

Advocate , Jun 21, 2021 Jun 21, 2021
Hello Amanda! Below are two different scripts,  the first one is for changing the swatches per your requirements and the second one is for "looking for text and changing to" you'll just to modify the "What you want to find" and the "Change to" for your needs. You could combine the two scripts together if you wanted. re: This isn't something we do every day, but we have hundreds of documents to process and the find/change function in Indesign is great, but scripts will cut down on time, which mat...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

those features already are available without a script, have you tried this:?

https://www.tech4pub.com/2020/11/14/indesign-tip-find-and-change-colors/#:~:text=As%20with%20any%20F....

 

Likes

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 ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Thank you, @JonathanArias! Yes, we are aware of this function, but wanted a way to do this with "one click" instead of using the swatches find/replace option.

Likes

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
Advocate ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Hello Amanda!

 

Below are two different scripts,  the first one is for changing the swatches per your requirements and the second one is for "looking for text and changing to" you'll just to modify the "What you want to find" and the "Change to" for your needs. You could combine the two scripts together if you wanted.

 

re: This isn't something we do every day, but we have hundreds of documents to process and the find/change function in Indesign is great, but scripts will cut down on time, which matters with this many documents. 

You could use Peter Kahrel's batch_convert.jsx to run either of the scripts I posted to run on all the Indesign documents that need to be proessed in one shot.

 

https://creativepro.com/files/kahrel/indesign/batch_convert.html

Screen Shot 2021-06-21 at 8.42.46 PM.png

Note: all three swatches must exist in the document(s) for this script to work.

 

var doc = app.activeDocument;

doc.colors.item("C=82 M=100 Y=11 K=0").remove("GGS purple");

doc.colors.item("GGS purple").remove("PANTONE 186 C");

 

 

 

 

var doc = app.activeDocument;

//Clear the find/change text preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

//Set the GREP find options (adjust to as needed)
app.findChangeGrepOptions.includeFootnotes = false;
app.findChangeGrepOptions.includeHiddenLayers = true;
app.findChangeGrepOptions.includeLockedLayersForFind = true;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = true;

//Look for the text and change to
app.findGrepPreferences.findWhat = "What you want to find";
app.changeGrepPreferences.changeTo = "Change to";
doc.changeGrep();

//Clear the find/change text preferences.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

 

 

Regards,

Mike

Likes

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 Community Professional ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Assuming the initial purple is unnamed as indicated, you'll need to name it to find it by Adding All Unamed Colors from the menu. And if the Pantone is also unnamed, might as well roll your own. Here it is put together with Mike's. Still assumes GGS purple is named, but if it's not, you can do the same I did with the Pantone: 

var doc = app.activeDocument;
app.menuActions.item("$ID/Add All Unnamed Colors").invoke();
doc.colors.item("C=82 M=100 Y=11 K=0").remove("GGS purple");
var c = doc.colors.item("PANTONE 186 C");
if (!c.isValid) {
    c = doc.colors.add({
        name: "PANTONE 186 C",
        colorValue: [0, 100, 80, 5],
        model: ColorModel.PROCESS,
        space: ColorSpace.CMYK,
    });
};
doc.colors.item("GGS purple").remove("PANTONE 186 C");

Likes

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Thank you so much! This is exactly what I needed!

Likes

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Thank you, Mike! Your scripts work perfectly and were just what I was looking for! Thank you!

Likes

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

@mikeb41294032 @brianp311 Is there a way to have the color change only run on that page? we don't want it to change the master pages but currently that script does the whole document.

Likes

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 Community Professional ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

You'd need to use the FindChangeColorOptions script fuctionality. This would do it on the whole doc, but skip master pages.

 

var doc = app.documents[0];
app.menuActions.item("$ID/Add All Unnamed Colors").invoke();
app.findColorPreferences = app.changeColorPreferences = null;
app.findChangeColorOptions.includeMasterPages = false;
app.findColorPreferences.findWhat = app.documents[0].swatches.itemByName("C=82 M=100 Y=11 K=0");
app.changeColorPreferences.changeTo = app.documents[0].swatches.itemByName("GGS purple");
doc.changeColor();
app.findColorPreferences.findWhat = doc.swatches.itemByName("GGS purple");
var c = doc.swatches.itemByName("PANTONE 186 C");
if (!c.isValid) {
    c = doc.colors.add({
        name: "PANTONE 186 C",
        colorValue: [0, 100, 80, 5],
        model: ColorModel.PROCESS,
        space: ColorSpace.CMYK,
    });
}
app.changeColorPreferences.changeTo = c;
doc.changeColor();

 

Likes

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Perfect! Thank you, @brianp311 that worked flawlessly!

Likes

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 Community Professional ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Amanda,

is there an appropriate item on a page where that unnamed color is applied to?

An object that could be selected where a script could start to read out the fill color?

 

Mike,

to find an unnamed color one could loop the colors of the document and look for an empty string for the name.

If an item is found check the colorValue array. If the values are close* to the values Amanda indicates, store the color to a variable and use that variable with remove().

 

* I said: "close" to the values, because InDesign's UI will never reveal the super-eaxct values with 13 digits after the dot if necessary. So at best a script could start with a selected object like a rectangle or some text where this color was applied to as fill color. Then it's easy to determine the unnamed color.

 

Below two screenshots with a sample CMYK color with quite some different values revealed by name in the UI, by color value in the UI and by the "true" color value revealed by a script alert:

 

Name: C=3 M=0 Y=90 K=0

Values revealed: 3.12, 0, 89.86, 0

 

You see, InDesign is able do do some rounding when it comes to naming a color after the color values that are revealed in the UI:

 

ColorName-vs-ColorValue-UI.PNG

Even more, the exact values of the color's colorValue array can only be shown with a script:

 

ColorName-vs-ColorValue-SCRIPT.PNG

 

Cyan

3.1199999153614
Magenta

0
Yellow

89.8599982261658
Black

0

 

Regards,
Uwe Laubender

( ACP )

Likes

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Thanks for that thorough response, @Laubender. We don't need to find colors that exactly. The previous answers worked great for our needs!

Likes

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 Community Professional ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Amanda said:

"Is there a way to have the color change only run on that page?"

 

Hi Amanda,

on what page exactly?

 

Brian's script still has the document as scope:

doc.changeColor();

Masters are not included, but still all document pages and all document pasteboards are accessed.

 

Regards,
Uwe Laubender

( ACP )

Likes

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 ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Hey @Laubender

 

Each of these documents has only one page, but if there were more, we would want it to apply to all pages except the masters. So it works for our needs. What would the script be if I needed it specifically only for page 1?

 

Thanks,

Amanda

Likes

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 Community Professional ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

LATEST

Amanda said: "… What would the script be if I needed it specifically only for page 1"

 

Well, scripting method changeColors() is only known to the document.

 

A possible algorithm could be to set the findChangeColorOptions.includeLockedLayersForFind to false and lock all objects on all document spreads that should not be found and changed. After changeColor() is done to the document unlock all the objects that were locked before.

 

Regards,
Uwe Laubender

( ACP )

Likes

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