Skip to main content
Inspiring
September 23, 2016
Answered

counting changes in a find change grep

  • September 23, 2016
  • 2 replies
  • 433 views

I've got a script which performs multiple find change greps and alerts how many of each grep search were made.

This is the code:

function seeSection () { 

    app.findGrepPreferences=app.changeGrepPreferences=null; 

   app.findGrepPreferences.findWhat= "the (section\\s+?\\d+?[\\.\\d+]?)"; 

    var changed = app.changeGrepPreferences.changeTo="$1";

    changed.appliedCharacterStyle="cross reference";

    var changes1 = app.activeDocument.changeGrep();

    if (changes1.length=1){

alert(changes1.length+" instance of 'the section' was changed to 'section'.");

}else{

   alert(changes1.length+" instances of 'the section' were changed to 'section'.");

  }

    };

No matter how many changes I know it has made it returns 1.

It also does not apply the character style to the whole reference. The character style is supposed to underline the word section \d.

It only underlines half of the word section:

section 5

​Not sure what is happening.

This topic has been closed for replies.
Correct answer tpk1982

1) Make sure you have the same name "cross reference"

2) When checking a condition use double ==

use    if (changes1.length==1){  and NOT     if (changes1.length=1){

2 replies

Inspiring
September 23, 2016

That's perfect, thanks so much. I need to remember that = is an assignment operator!

tpk1982
tpk1982Correct answer
Legend
September 23, 2016

1) Make sure you have the same name "cross reference"

2) When checking a condition use double ==

use    if (changes1.length==1){  and NOT     if (changes1.length=1){

tpk1982
Legend
September 23, 2016

this two, lines also not correct

    var changed = app.changeGrepPreferences.changeTo="$1";

    changed.appliedCharacterStyle="cross reference";

use this

    app.changeGrepPreferences.changeTo="$1";

    app.changeGrepPreferences.appliedCharacterStyle="cross reference";