4 Replies Latest reply on Jun 12, 2018 5:19 AM by manuelb27477138

    Multiples Find Replaces with TRY CATCH errors in alert and console

    manuelb27477138 Level 1

      Hello!

       

      In my script I find and replaces many words:

          replaceTextUsingFIND ("Article 1.", "Article ONE"); 
          replaceTextUsingFIND ("Article 2.", "Article TWO"); 
          replaceTextUsingFIND ("Article 3.", "Article THREE"); 
          replaceTextUsingFIND ("Article 4.", "Article FOUR");
      
      
          function replaceTextUsingFIND (input, output)  
          {  
              app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;  
              app.findTextPreferences.findWhat = input;  
              app.changeTextPreferences.changeTo = output;  
              app.activeDocument.changeText();  
              app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;   
      }
      

       

       

      This is my document:

      Captura de pantalla 2018-06-11 a las 22.23.24.png

       

       

      In this case, the words Article 2. and Article 4. do not exist, then I need to get one alert per each word.

       

      Is possible get the errors by alert and in the Javascript console? I try with try/catch but didn't work 

       

      Thanks for help me!

        • 1. Re: Multiples Find Replaces with TRY CATCH errors in alert and console
          BarlaeDC Adobe Community Professional

          HI,

           

          It is not possible to get errors as such, as no error is thrown if the find doesn't find anything, but you can catch that the find didn't find anything.

           

          if you change the following line

          app.activeDocument.changeText();
          

          to

          var changedResults = app.activeDocument.changeText();
          

           

          Then you can check the length of changedResults to see if anything was changed something like

          if ( changedResults === 0)
          {
               // I have put it into a alert, but you could put it to the console.
               alert ( "No results for " + input);
          }
          

           

          Hpoe this helps

           

          Malcolm

          1 person found this helpful
          • 2. Re: Multiples Find Replaces with TRY CATCH errors in alert and console
            manuelb27477138 Level 1

            Hi Malcolm!

             

            I needed a change in the if conditional this === to ==

            And now is working!

            Here is the final Script:

            Download files

             

                replaceTextUsingFIND ("Article 1.", "Article ONE");    
                replaceTextUsingFIND ("Article 2.", "Article TWO");  
                replaceTextUsingFIND ("Article 3.", "Article THREE");  
                replaceTextUsingFIND ("Article 4.", "Article FOUR"); 
            
                function replaceTextUsingFIND (input, output)   
                {   
                    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;   
                    app.findTextPreferences.findWhat = input;   
                    app.changeTextPreferences.changeTo = output;   
                    var changedResults = app.activeDocument.changeText();   
                    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;    
            
                    if ( changedResults == 0)  {
                        alert ( "No results for " + input);
                    } 
            }
            

             

            Captura de pantalla 2018-06-12 a las 13.51.32.png

             

            Captura de pantalla 2018-06-12 a las 13.51.08.png

             

             

            Captura de pantalla 2018-06-12 a las 13.51.18.png

             

            Thanks so much!

            • 3. Re: Multiples Find Replaces with TRY CATCH errors in alert and console
              BarlaeDC Adobe Community Professional

              Hi,

               

              The === matches type as well as value, == just matches value.

               

              I think this is because I missed out the .length inside the if, so it should look like this

               

               

              if ( changedResults.length === 0)
              

               

              but as long as it is working which is the main thing.

               

              Regards

               

              Malcolm

              • 4. Re: Multiples Find Replaces with TRY CATCH errors in alert and console
                manuelb27477138 Level 1

                You true, I change and also is working.

                Thanks a lot for your continuos help

                 

                replaceTextUsingFIND ("Article 1.", "Article ONE");   
                replaceTextUsingFIND ("Article 2.", "Article TWO"); 
                replaceTextUsingFIND ("Article 3.", "Article THREE"); 
                replaceTextUsingFIND ("Article 4.", "Article FOUR"); 
                
                function replaceTextUsingFIND (input, output)   
                {   
                    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;   
                    app.findTextPreferences.findWhat = input;   
                    app.changeTextPreferences.changeTo = output;   
                    var changedResults = app.activeDocument.changeText();   
                    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;   
                
                    if ( changedResults.length === 0)  {
                        alert ( "No results for " + input);
                    } 
                }