Skip to main content
jesuale
Participant
May 11, 2022
Answered

Don't search inside tables or skip tables

  • May 11, 2022
  • 1 reply
  • 329 views

Hi, I'm new to scripts.
Performing searches I can apply styles to specific words, so far so good, but the problem is that my document has many tables where that searched word is also present, is there a way to apply the style only in the document and not in the tables? Maybe using script?
I would appreciate your help. And sorry for my writing, I don't speak English well.

 

I am attaching a picture so you can better understand what I want.

This topic has been closed for replies.
Correct answer Laubender

Hi Jesuale,

the basic algorithm could be this:

[1] Run a GREP Find action and store the found texts to an array.

[2] Loop that array of results and check the parent of the found text snippets.

If the parent is a cell, do nothing and continue to the next found item.

If the parent is not a cell do a change to that found text.

 

Look up this discussion we recently had:

 

[JS] Problem with change grep with look behind/ahead and /K
Kasyan Servetsky, Mar 16, 2022

https://community.adobe.com/t5/indesign-discussions/js-problem-with-change-grep-with-look-behind-ahead-and-k/m-p/12818708#M469736

 

In a nutshell the code below should work for you with GREP Find/Change. Not intensively tested, though. That's your job. To make that work do the following:

[1] Do your settings in InDesign's GREP Find/Change normal user interface.

[2] Then as a second step run this ExtendScript (JavaScript) code:

 

 

/*
Posted by Uwe Laubender at the InDesign User Forum:
https://community.adobe.com/t5/indesign-discussions/don-t-search-inside-tables-or-skip-tables/m-p/12937086#M476815
*/

( function()
{

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

app.doScript
(
	ignoreTables, 
	ScriptLanguage.JAVASCRIPT, 
	[],
	UndoModes.ENTIRE_SCRIPT, 
	"Find/Replace GREP; Leave out Tables | SCRIPT"
);

function ignoreTables()
{
	var txt, changed,
	doc = app.activeDocument,
	found = doc.findGrep(false);
	
	for (var i = 0; i < found.length; i++)
	{
		// IGNORE TEXT IN TABLE CELLS:
		if( found[i].parent.constructor.name == "Cell" ){ continue };
		
		// TEXT OUTSIDE TABLE CELLS:
		
		txt = found[i];
		app.findGrepPreferences.findWhat = txt.contents;
		changed = txt.changeGrep();
	};

};

}() )

 

 

If the result is not to your liking you can undo the whole thing with one single undo that is reading:

"Find/Replace GREP; Leave out Tables | SCRIPT"

 

Thanks Kasyan Servetsky for providing the core part of the code.

 

Regards,
Uwe Laubender

( ACP )

1 reply

LaubenderCommunity ExpertCorrect answer
Community Expert
May 11, 2022

Hi Jesuale,

the basic algorithm could be this:

[1] Run a GREP Find action and store the found texts to an array.

[2] Loop that array of results and check the parent of the found text snippets.

If the parent is a cell, do nothing and continue to the next found item.

If the parent is not a cell do a change to that found text.

 

Look up this discussion we recently had:

 

[JS] Problem with change grep with look behind/ahead and /K
Kasyan Servetsky, Mar 16, 2022

https://community.adobe.com/t5/indesign-discussions/js-problem-with-change-grep-with-look-behind-ahead-and-k/m-p/12818708#M469736

 

In a nutshell the code below should work for you with GREP Find/Change. Not intensively tested, though. That's your job. To make that work do the following:

[1] Do your settings in InDesign's GREP Find/Change normal user interface.

[2] Then as a second step run this ExtendScript (JavaScript) code:

 

 

/*
Posted by Uwe Laubender at the InDesign User Forum:
https://community.adobe.com/t5/indesign-discussions/don-t-search-inside-tables-or-skip-tables/m-p/12937086#M476815
*/

( function()
{

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

app.doScript
(
	ignoreTables, 
	ScriptLanguage.JAVASCRIPT, 
	[],
	UndoModes.ENTIRE_SCRIPT, 
	"Find/Replace GREP; Leave out Tables | SCRIPT"
);

function ignoreTables()
{
	var txt, changed,
	doc = app.activeDocument,
	found = doc.findGrep(false);
	
	for (var i = 0; i < found.length; i++)
	{
		// IGNORE TEXT IN TABLE CELLS:
		if( found[i].parent.constructor.name == "Cell" ){ continue };
		
		// TEXT OUTSIDE TABLE CELLS:
		
		txt = found[i];
		app.findGrepPreferences.findWhat = txt.contents;
		changed = txt.changeGrep();
	};

};

}() )

 

 

If the result is not to your liking you can undo the whole thing with one single undo that is reading:

"Find/Replace GREP; Leave out Tables | SCRIPT"

 

Thanks Kasyan Servetsky for providing the core part of the code.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
May 11, 2022

Note: EDITED my post above, because I did some comments to the code.

 

Regards,
Uwe Laubender

( ACP )

jesuale
jesualeAuthor
Participant
May 11, 2022

Thank you very much Laubender the code worked perfectly, I appreciate it very much, I will review the link you gave me to learn much more.

 

Greetings from Bolivia
Jesus Aguilar