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

Multiples Find Replaces with TRY CATCH errors in alert and console

Engaged ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

501

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 , Jun 12, 2018 Jun 12, 2018

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 " + i

...

Votes

Translate

Translate
Community Expert ,
Jun 12, 2018 Jun 12, 2018

Copy link to clipboard

Copied

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

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
Engaged ,
Jun 12, 2018 Jun 12, 2018

Copy link to clipboard

Copied

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!

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 ,
Jun 12, 2018 Jun 12, 2018

Copy link to clipboard

Copied

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

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
Engaged ,
Jun 12, 2018 Jun 12, 2018

Copy link to clipboard

Copied

LATEST

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);

    }

}

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