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

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

4.8K

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

Advisor , 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

...

Votes

Translate

Translate
Guru ,
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....

 

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 ,
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.

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
Engaged ,
Feb 17, 2024 Feb 17, 2024

Copy link to clipboard

Copied

LATEST

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
Advisor ,
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

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
Community Expert ,
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");

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
Enthusiast ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

Hello, @brian_p_dts.

I'm trying some options and none is working.
I also tryied manually and an object (rectangle) with manual applied color (not by style), doesn't change. Is this feature bugged in 2023?

Both colors are inside groups (find color in group "a", change color in group "b"). The dialog alerts the change (manual operation) but the color is not effectivelly changed. Any idea?

 

var doc = app.activeDocument;
var a = doc.colorGroup.itemByName("a");
var b = doc.colorGroup.itemByName("b");

changeSw(doc,a,b)

function changeSw(doc,a,b) {
	app.findChangeColorOptions.includeFootnotes = true;
	app.findChangeColorOptions.includeHiddenLayers = true;
	app.findChangeColorOptions.includeLockedLayersForFind = true;
	app.findChangeColorOptions.includeLockedStoriesForFind = true;
	app.findChangeColorOptions.includeMasterPages = true;
	app.findChangeColorOptions.objectType = ObjectTypes.ALL_FRAMES_TYPE;
	
	var match = [];
	for (var i=0; i<a.colorGroupSwatches.length; i++) {
		var swa = a.colorGroupSwatches[i].swatchItemRef;
		var aname = swa.name;
		var anmreg = aname.replace(/^.+?_/,"");
		for (var j=0; j<b.colorGroupSwatches.length; j++) {
			var swb = b.colorGroupSwatches[j].swatchItemRef;
			var bname = swb.name;
			if ( RegExp(anmreg,"i").test(bname) ) match.push([swa,swb]);
			}
		}
	for (var i=0; i<match.length; i++) {
		a = match[i][0];
		b = match[i][1];
		if (a.isValid && b.isValid) {
			app.findColorPreferences = app.changeColorPreferences = NothingEnum.NOTHING;
			app.findColorPreferences.findWhat = a;
			app.findColorPreferences.tintLowerLimit = 0;
			app.findColorPreferences.tintUpperLimit = 100;
			app.changeColorPreferences.changeTo = b;
			doc.changeColor();
			app.findColorPreferences = app.changeColorPreferences = NothingEnum.NOTHING;
			}
		}
	}

 

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
Community Expert ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

Don't know what else could be going on, but doc.colorGroup.itemByName should be doc.colorGroups.itemByName. But I imagine you would have seen an error?

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
Enthusiast ,
Feb 25, 2023 Feb 25, 2023

Copy link to clipboard

Copied

Even with the fix, it's still buggy.

Now the behavior changed: the colors are duplicated outside the groups.

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
Community Expert ,
Feb 25, 2023 Feb 25, 2023

Copy link to clipboard

Copied

Hi @lfcorullon ,

did not test your code at all, but I know that there are some unresolved bugs with findColor in all non-English versions of InDesign. So for Brian the code could work, but e.g. not for my German version of InDesign.

Could you provide a sample InDesign document that we can test with? Just attach it to your reply here in the forum.

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

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
Enthusiast ,
Feb 25, 2023 Feb 25, 2023

Copy link to clipboard

Copied

Both colors "verde" and "amarelo" are the problematic ones. Same value, different names. Now they are duplicated outside the groups. Very weird.

 

app.doScript(main, ScriptLanguage.javascript, undefined, UndoModes.entireScript, "change swatches by group");
//========================================================================================
//========================================================================================
function main() {
	var doc = app.activeDocument;
	var a = doc.colorGroups.itemByName("a");
	var b = doc.colorGroups.itemByName("b");
	changeSw(doc,a,b);
	alert("Done." , "Script by LFCorullón");
	}
//========================================================================================
//========================================================================================
function changeSw(doc,a,b) {
	app.findChangeColorOptions.includeFootnotes = true;
	app.findChangeColorOptions.includeHiddenLayers = true;
	app.findChangeColorOptions.includeLockedLayersForFind = true;
	app.findChangeColorOptions.includeLockedStoriesForFind = true;
	app.findChangeColorOptions.includeMasterPages = true;
	app.findChangeColorOptions.objectType = ObjectTypes.ALL_FRAMES_TYPE;
	
	var match = [];
	for (var i=0; i<a.colorGroupSwatches.length; i++) {
		var swa = a.colorGroupSwatches[i].swatchItemRef;
		var aname = swa.name;
		var anmreg = aname.replace(/^.+?_/,"").replace(/\scopy$/gi,"");
		for (var j=0; j<b.colorGroupSwatches.length; j++) {
			var swb = b.colorGroupSwatches[j].swatchItemRef;
			var bname = swb.name;
			if ( RegExp(anmreg,"i").test(bname) ) match.push([swa,swb]);
			}
		}
	for (var i=0; i<match.length; i++) {
		a = match[i][0];
		b = match[i][1];
		if (a.isValid && b.isValid) {
			app.findColorPreferences = app.changeColorPreferences = NothingEnum.NOTHING;
			app.findColorPreferences.findWhat = a;
			app.findColorPreferences.tintLowerLimit = 0;
			app.findColorPreferences.tintUpperLimit = 100;
			app.changeColorPreferences.changeTo = b;
			doc.changeColor();
			app.findColorPreferences = app.changeColorPreferences = NothingEnum.NOTHING;
			}
		}
	}
//========================================================================================
//========================================================================================

 

 

Thanks for trying to help, @brian_p_dts and @Laubender  

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
Community Expert ,
Feb 25, 2023 Feb 25, 2023

Copy link to clipboard

Copied

@lfcorullon said:
"Both colors "verde" and "amarelo" are the problematic ones. Same value, different names. Now they are duplicated outside the groups. Very weird."

 

Hi @lfcorullon ,

not with my German InDesign 2023 version 18.1. Your code is working for me.

 

Before:

BEFORE-test_replace swatches by groups.indd.PNG

 

After the script run:

AFTER-test_replace swatches by groups_AFTER_SCRIPT_RUN.indd.PNG

 

Attached the result InDesign document:

test_replace swatches by groups_AFTER_SCRIPT_RUN.indd

 

Regards,
Uwe Laubender
( Adobe Community Expert )

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
Enthusiast ,
Feb 25, 2023 Feb 25, 2023

Copy link to clipboard

Copied

Very weird. I'm on same InDesign version. Windows.

Look my result:

lfcorullon_0-1677360821629.png

 

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
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Hi @lfcorullon ,

did my tests on Windows 10. With the German version of InDesign 2023 v 18.1.

Ran your code from the ESTK.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

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
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Worked fine for me too on English 18.1 x64, Windows 11 Home 10.0.22621

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
Enthusiast ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Weird. Thanks too, Brian, for test it and letting me know.

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
Enthusiast ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Yes, I tried running from ESTK and from Scripts Panel. Same weird behavior.

Thanks for trying help.

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

Copy link to clipboard

Copied

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

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 ,
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!

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

Copy link to clipboard

Copied

@Mike Bro @brian_p_dts 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.

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
Community Expert ,
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();

 

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

Copy link to clipboard

Copied

Perfect! Thank you, @brian_p_dts that worked flawlessly!

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
Community Expert ,
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 )

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 ,
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!

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
Community Expert ,
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 )

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