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:
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!
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
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
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:
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);
}
}
Thanks so much!
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
Copy link to clipboard
Copied
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);
}
}