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

Javascript - Find-Replace with GREP

Engaged ,
Oct 19, 2016 Oct 19, 2016

Okay, it is my first Javascript, not true: it´s my second one.

Working on the first one I figured out how to apply different paragraph-stypes to the paragraphs in a selected text frame.

Now, I actually would like to do the previous step: to search for a particular word and to replace it in special form.

And, because I am a js-greenhorn, it doesn´t work, of course...

Here the code:

var doc = app.activeDocument; 

var Auswahl = app.selection[0];

var AlleAbsaetze = Auswahl.paragraphs.everyItem();

app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren

app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren

// Voreinstellung für das GREP

app.findChangeGrepOptions.includeFootnotes = true;

app.findChangeGrepOptions.includeHiddenLayers = false;

app.findChangeGrepOptions.includeLockedLayersForFind = false;

app.findChangeGrepOptions.includeLockedStoriesForFind = false;

app.findChangeGrepOptions.includeMasterPages = false;

AlleAbsaetze.findGrepPreferences.findWhat = "Art\.\-Nr\.";

AlleAbsaetze.changeGrepPreferences.changeTo = "Art\.";

doc.changeGrep();

app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren

app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren

The engine stops at row 15 and nothing happens.

I'm sure it's a stupid beginner's mistaker, but I do not know where.

The error-message is: "Object does not support the property or method 'findGrepPreferences'."

I suppose it´s not possible to assign the GREP-search-function neither to a paragraph nor to a text frame. Isn´t it?

Any idea?

TOPICS
Scripting
3.2K
Translate
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

Guide , Oct 19, 2016 Oct 19, 2016

Not a big issue .. change like below...

var doc = app.activeDocument;   

var Auswahl = app.selection[0]; 

var AlleAbsaetze = Auswahl.paragraphs.everyItem(); 

 

app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren 

app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren 

// Voreinstellung für das GREP 

app.findChangeGrepOptions.includeFootnotes = true; 

app.findChangeGrepOptions.includeHiddenLayers = false; 

app.findChangeGrepOptions.includeLockedLayersForFind = fals

...
Translate
Guide ,
Oct 19, 2016 Oct 19, 2016

Not a big issue .. change like below...

var doc = app.activeDocument;   

var Auswahl = app.selection[0]; 

var AlleAbsaetze = Auswahl.paragraphs.everyItem(); 

 

app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren 

app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren 

// Voreinstellung für das GREP 

app.findChangeGrepOptions.includeFootnotes = true; 

app.findChangeGrepOptions.includeHiddenLayers = false; 

app.findChangeGrepOptions.includeLockedLayersForFind = false; 

app.findChangeGrepOptions.includeLockedStoriesForFind = false; 

app.findChangeGrepOptions.includeMasterPages = false; 

 

 

app.findGrepPreferences.findWhat = "Art\.\-Nr\."; 

app.changeGrepPreferences.changeTo = "Art\."; 

AlleAbsaetze.changeGrep(); 

 

app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren 

app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren

Translate
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 ,
Oct 19, 2016 Oct 19, 2016

It can be that easy... if one knows how it works.

thank you tpk1982

Translate
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 ,
Oct 20, 2016 Oct 20, 2016

But:

The Indesign doc is like this, partially

Art.-Nr.

There is a space and a break (return) at the end of the line.

If I search for "Art\.\-Nr\.\s~b" there is no hit. Without the space (\s) it works (of course just if there is no space)

Translate
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
Guide ,
Oct 20, 2016 Oct 20, 2016

To check Art.-Nr., use square brackets..

for space issue, remove trailing space is useful.. but run first this then the code.. if you need to include this then the final code like..

var doc = app.activeDocument;     

var Auswahl = app.selection[0];   

var AlleAbsaetze = Auswahl.paragraphs.everyItem();   

   

app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren   

app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren   

// Voreinstellung für das GREP   

app.findChangeGrepOptions.includeFootnotes = true;   

app.findChangeGrepOptions.includeHiddenLayers = false;   

app.findChangeGrepOptions.includeLockedLayersForFind = false;   

app.findChangeGrepOptions.includeLockedStoriesForFind = false;   

app.findChangeGrepOptions.includeMasterPages = false;   

app.findGrepPreferences.findWhat = "\s+$";   

app.changeGrepPreferences.changeTo = "";   

app.changeGrep();

   

app.findGrepPreferences.findWhat = "Art[.]-Nr[.]";   

app.changeGrepPreferences.changeTo = "Art\.";   

AlleAbsaetze.changeGrep();   

   

app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren   

app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren  

Translate
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 ,
Oct 20, 2016 Oct 20, 2016

Okay, now it works. But why the conventional regex doesn´t???

The same with the artikel number. In one magazine it is like this: 00 0000, in the other one: Art. 000 000.

So I search for: "^(\d{2})(\s)(\d)(\d{3})";

And change to: "Art\.\s$1$3\s$4";

Without effect. Is something wrong here as well?

Translate
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
Guide ,
Oct 20, 2016 Oct 20, 2016

^ means the beginning of paragraph.. is it every number only start at beginning of paragraph?

also simply you can say..

^\d+\s+\d+

Translate
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 ,
Oct 20, 2016 Oct 20, 2016

Right, each of them shold be at the beginning.

If I search with "^\d+\s+\d+" I can´t replace it like I want... I need the braces.

Translate
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
Guide ,
Oct 20, 2016 Oct 20, 2016

Hmm..for 00 0000.. use like below

So I search for: "^(\d{2})(\s)(\d{3})";

And change to: "Art. $1$2$3";

Translate
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 ,
Oct 20, 2016 Oct 20, 2016

No, it found nothing. What could be wrong, hmmmm.....

Translate
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
Guide ,
Oct 20, 2016 Oct 20, 2016

please share the screen shot of your document..

Translate
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 ,
Oct 20, 2016 Oct 20, 2016

screenshot2.JPG

the last two lines are different. I try to make a replacement for each eventuallity (with and without a space after the second digit)

Translate
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
Guide ,
Oct 20, 2016 Oct 20, 2016

Are using those in script or in indesign itself?

For indesign means..like below

Screen Shot 2016-10-20 at 2.30.46 PM.png

for script means we need to add two slashes...

^(\\d{2})(\\s)(\\d{4})

Translate
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 ,
Oct 20, 2016 Oct 20, 2016
LATEST

in a script.

!!that was the problem!! thanks

Can you please tell me WHY we need two slashes? :S

Translate
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 ,
Oct 20, 2016 Oct 20, 2016

I would like to know, again: why do the conventional regex not work? It was hard enough for me to learn it wothin two weeks, now it not very helpful!

Translate
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