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

Grep not the same result in javascript than Find/Change box ?

Community Beginner ,
Nov 20, 2021 Nov 20, 2021

Copy link to clipboard

Copied

Hello !
My idea is to remove the PlaceHolder text to have a clean text area. To do this, Grep should help me.

If I use the Find/Chnage box and do a search on

\KLorem\X+

to replace it with "" (nothing) : it does the job, all my texts that start with Lorem will be deleted (even if it's linked box, even if there is some overset text)

VS

If I use javascript :

app.findGrepPreferences.findWhat = '\\KLorem\\X+';

the result is partial if my text is linked in several text box.

 

So, how to select all strory in javascript, if the first word is Lorem ?

Here my actual javascript code :

var myDocument = app.documents.item(0);
try {
var myLayerTexts = myDocument.layers.itemByName("Texte").textFrames.everyItem().texts.everyItem();
app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options. 
app.findChangeGrepOptions.includeFootnotes = false; 
app.findChangeGrepOptions.includeHiddenLayers = false; 
app.findChangeGrepOptions.includeLockedLayersForFind = false; 
app.findChangeGrepOptions.includeLockedStoriesForFind = false; 
app.findChangeGrepOptions.includeMasterPages = false;

app.findGrepPreferences.findWhat = '\\KLorem\\X+';
app.changeGrepPreferences.changeTo = '';
myLayerTexts.changeGrep();

app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing;
    }
catch (e) {
    alert("Le calque Texte est vide ou n'existe pas");
    }

 

TOPICS
Scripting

Views

258

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

Community Expert , Nov 20, 2021 Nov 20, 2021

You could try the following

var myDocument = app.documents.item(0);
try {
var myLayerTexts = myDocument.layers.itemByName("Texte").textFrames.everyItem().parentStory
app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options. 
app.findChangeGrepOptions.includeFootnotes = false; 
app.findChangeGrepOptions.includeHiddenLayers = false; 
app.findChangeGrepOptions.includeLockedLayersForFind = false; 
app.findChangeGrepOptions.includeLockedS
...

Votes

Translate

Translate
Community Expert ,
Nov 20, 2021 Nov 20, 2021

Copy link to clipboard

Copied

Hi Frank,

to give some advice we need a minimum sample InDesign document for testing where you see the issue.

And a screenshot of your GREP Find/Replace settings when you run your expression in the UI.

 

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
Community Beginner ,
Nov 20, 2021 Nov 20, 2021

Copy link to clipboard

Copied

You're right! Sorry
Here my starter document.

starter_screen.png

 In this second screen, this is the result from the UI : (it's delete only the first box)

result_script.png

 And this one, is the selection from the Find/Change box : it's delete all the text

selctionFromFindChangeBox.png

Attached, an indesign example

 

Thanks in advance fo any help
Regards
Frank

 

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 ,
Nov 20, 2021 Nov 20, 2021

Copy link to clipboard

Copied

You could try the following

var myDocument = app.documents.item(0);
try {
var myLayerTexts = myDocument.layers.itemByName("Texte").textFrames.everyItem().parentStory
app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing;
//Set the find options. 
app.findChangeGrepOptions.includeFootnotes = false; 
app.findChangeGrepOptions.includeHiddenLayers = false; 
app.findChangeGrepOptions.includeLockedLayersForFind = false; 
app.findChangeGrepOptions.includeLockedStoriesForFind = false; 
app.findChangeGrepOptions.includeMasterPages = false;

app.findGrepPreferences.findWhat = '\\KLorem\\X+';
app.changeGrepPreferences.changeTo = '';
for(var i = 0; i < myLayerTexts.length; i++)
	myLayerTexts[i].changeGrep();

app.findGrepPreferences = NothingEnum.nothing; 
app.changeGrepPreferences = NothingEnum.nothing;
    }
catch (e) {
    alert("Le calque Texte est vide ou n'existe pas");
    }

The issue with your code is that you are executing the grep over the texts object. The texts object for each frame will contain only the text within that frame and hence the condition will fail for the linked frames. What I have done is executing the grep on all the stories corresponding to the frames in the layer of interest. This will work but is a bit unoptimised as the loop will be called for each frame in a threaded story when its sufficient to call it once.

-Manan

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 Beginner ,
Nov 20, 2021 Nov 20, 2021

Copy link to clipboard

Copied

LATEST

Ok, many many thanks for your help !
It works now. Even if the code has not been optimized: for 6 or 8 pages of a magazine, it's a good thing.

 

Frank

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