14 Replies Latest reply on Sep 15, 2016 1:34 PM by Trevorׅ

    Find and replace script Question.

    cbishop01 Level 2

      Hi all,

       

      I have been looking on here for a way to show the amount of changes made?  My script for find and replace is working however i would like it to display if there is 0 changes or 100 so that way we know how many if any corrections were made.  I basically want it to show the same kind of information as the (Native Find Replace in Indesign.) If there is a Link that talks about this please point me in that direction.

        • 1. Re: Find and replace script Question.
          Trevorׅ Adobe Community Professional

          Hi Chris,

           

          Maybe your looking for

          changes = doc.changeText();

          numberOfChanges = changes.length;

           

          You just have to be careful that the array is flat

           

          HTH

           

          Trevor

          1 person found this helpful
          • 2. Re: Find and replace script Question.
            cbishop01 Level 2

            I cant get that to display the amount of changes.  I'm coding it wrong somewhere.  Here's what i'm getting. Screen Shot 2016-09-12 at 2.56.08 PM.png

            I added the code above to the find/change preferences part of the code then in the Alert I added the changes, numberOfChanges.

            • 3. Re: Find and replace script Question.
              Skemicle Level 3

              Remove the quotation marks from around 'changes, numberOfChanges' (If there are any), and replace the comma with-- + ", " + --That looks like it would be the problem. Putting the alert in quotations tell InDesign to use the text "changes, numberOfChanges" rather than the variables, of course having the changes in the alert would show all the text that has been changed, so it probably isn't best to put that in the alert.. I would use something like:

              alert(numberOfChanges + " changes have been made.")

              1 person found this helpful
              • 4. Re: Find and replace script Question.
                cbishop01 Level 2

                First thank you Skemicle.  that was my problem.  That Fixed part of it.  Now it displays the Changes/NUMBer but it stays as , 0 no matter if it changes anything or not.  Its not displaying the Change results.

                • 5. Re: Find and replace script Question.
                  cbishop01 Level 2
                  alert("Results:""  "+numberOfChanges +"  "+"Changes have been made")
                  

                   

                  This is how i have mine. Mostly like yours.  But I still get 0 as a result even if i use yours.  I have my Code place in the Below let me know if its correct.

                  function RTXT(target, findProps, changeProps){
                     app.findChangeTextOptions.caseSensitive = true;  
                     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;  
                     app.findTextPreferences.properties = findProps;  

                     app.changeTextPreferences.properties = changeProps;

                  //  target.changeText();

                     change = target.changeText();     // added

                     numberOfChanges = changes.length;// added

                     }

                   

                  Is that the correct place to put it?  Or am i supposed to place it in the actual search Function itself?  I've tried it both ways.  And no luck.

                  • 6. Re: Find and replace script Question.
                    Skemicle Level 3

                    To be honest, i haven't really messed around with scripting grep changes. Could you provide the search function (or a sample would work) as well and i will see what i can work out for you?

                    • 7. Re: Find and replace script Question.
                      Skemicle Level 3

                      I have it figured out.. 'var changes = ' Should be set to the changeGrep() function line.. I have a sample grep script for you That changes the word "December" to "Month" and alerts as to how many changes were made.

                       

                      app.findGrepPreferences.findWhat = 'December';

                      app.changeGrepPreferences.changeTo = 'Month';

                      var changes = app.documents[0].changeGrep();

                      var numChange = changes.length;

                      alert(numChange + " changes were made.");

                      • 8. Re: Find and replace script Question.
                        Trevorׅ Adobe Community Professional

                        Try adapt this

                        var doc, tf, changes, numberOfChanges;
                        app.findGrepPreferences = app.changeGrepPreferences = null;
                        app.findGrepPreferences.findWhat = 'g+';
                        app.changeGrepPreferences.changeTo = '@@@@@@';
                        doc = app.documents.add();
                        tf = doc.pages[0].textFrames.add({geometricBounds:['10mm', '10mm', '200mm', '200mm']});
                        tf.insertionPoints[0].contents = 'asikg jas gioas gas dglasjhngas lojhrgtioudsfg  ggg  akldsf gasdfg oer';
                        changes = doc.changeGrep();
                        numberOfChanges = changes.length;
                        alert(numberOfChanges);
                        
                        1 person found this helpful
                        • 9. Re: Find and replace script Question.
                          Trevorׅ Adobe Community Professional

                          Working now?

                          • 10. Re: Find and replace script Question.
                            cbishop01 Level 2

                            I can make your above work.  But when i have it in my code it wont change the number of corrections from 0 to an actual amount.  I'm trying to get my double space's set to single space and I know there  are over 1000 it still displays 0.  I'm going to try a little longer I may just have a typo or something simple i'm missing.  If i cant get it soon I'll post the code i'm using and see where i'm going wrong. 

                            • 11. Re: Find and replace script Question.
                              cbishop01 Level 2

                              Ok I found something out.  Not sure why its like this but i can get it to work if i place my search code in its own check box.  when i add it to the Checkbox i'm wanting it to be added to it will not work.  Maybe you can tell me why. It will not work here. 

                              function cb1CallBack(target) {
                              var findProps;
                                  /*    findProps = {findWhat:"\\s{2, }",},       
                                         changeProps = { changeTo:" ",};       
                                  grep (target,  findProps, changeProps ); */ 
                                       
                                      findProps = {findWhat:" :",}     
                                          changeProps = { changeTo:":",}   
                                      RTXT(target, findProps, changeProps);   
                                      findProps = {findWhat:" .",}     
                                            changeProps = { changeTo:".",}   
                                      RTXT(target, findProps, changeProps);   
                                      findProps = {findWhat:"^p^p",}     
                                            changeProps = { changeTo:"^p",}   
                                      RTXT(target, findProps, changeProps);   
                                      findProps = {findWhat:"DISE—ADO",}     
                                            changeProps = { changeTo:"DISEÑADO",}   
                                      RTXT(target, findProps, changeProps);   
                                      findProps = {findWhat:"DA—O",}     
                                            changeProps = { changeTo:"DAÑO",}   
                                      RTXT(target, findProps, changeProps);   
                                         findWhat ="\r";     
                                            changeTo = " ";
                                      grepR(target,  findWhat, changeTo);     
                                        findProps = {findWhat:"Α",}     
                                            changeProps = { changeTo:"A",}   
                                      RTXT(target, findProps, changeProps); 
                                      findProps = {findWhat:"Capa de barniz",}     
                                            changeProps = { changeTo:"Capa transparente",}   
                                      RTXT(target, findProps, changeProps);
                                   // This will not work here.  Everything above works.  
                                      findWhat = "  ";
                                          changeTo = " ";
                                      countCH(target, findWhat, changeTo);
                                      
                                }
                              

                              It will work if its by itself. Below WORKS FINE>

                              function cb8CallBack(target) {
                                       findWhat = "  ";
                                      changeTo = " ";
                                      countCH(target, findWhat, changeTo);
                                      }
                              

                              Where am i going wrong?

                              • 12. Re: Find and replace script Question.
                                cbishop01 Level 2
                                var changes = app.documents[0].changeGrep();
                                

                                thank you for posting this i was stuck until you gave me this little gem lol.

                                • 13. Re: Find and replace script Question.
                                  cbishop01 Level 2

                                  They really need to allow for Multiple Correct answers.  I figured out what my problem was.  It was something stupid (on my part).

                                  in this sections it should look like this.

                                  app.findGrepPreferences.properties = findProps;
                                  

                                  And what i had was aapp.findGrepPreferences.properties = findProps;. one too many "a"'s..  Its the little things like that that make you feel dumb lol...

                                   

                                  Thank both of you for your help.

                                  • 14. Re: Find and replace script Question.
                                    Trevorׅ Adobe Community Professional

                                    cbishop01 wrote:

                                     

                                    And what i had was aapp.findGrepPreferences.properties = findProps;. one too many "a"'s.. Its the little things like that that make you feel dumb lol...

                                     

                                    Correctly so