Copy link to clipboard
Copied
I'm trying to run this script to select text if it is found but it says there no text even though I've wrapped it in an if statement.
function seeSection () {
app.findGrepPreferences=app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat= "the (section\\s+?\\d+?[\\.\\d+]?)";
app.changeGrepPreferences.changeTo="$1";
app.changeGrepPreferences.appliedCharacterStyle="cross reference";
found.select();
};
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'.");
}
};
Is your post at find/change script highlight found text not the same question??
Copy link to clipboard
Copied
Javascript execute line by line. So after the changed text it wont find. I just commented the changeTo line. Try this:
function seeSection () {
app.findGrepPreferences=app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat= "the (section\\s+?\\d+?[\\.\\d+]?)";
//~ app.changeGrepPreferences.changeTo="$1";
app.changeGrepPreferences.appliedCharacterStyle="cross reference";
if (app.activeDocument.findText()){
found.select();
};
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'.");
}
};
Copy link to clipboard
Copied
also using greppreference should be
app.activeDocument.findGrep()
not app.activeDocument.findText()
Copy link to clipboard
Copied
Isn't changeTo just a specifier? It hasn't been resolved yet so it shouldn't do anything with that information until I say changeGrep() or findGrep()?
Also,
Copy link to clipboard
Copied
Ah..yes i missed..but did you tried this one
app.activeDocument.findGrep()
not app.activeDocument.findText()
Copy link to clipboard
Copied
What does 'found' mean in your function? It's not bound in any way.
You set the Grep options, then you do findText()
You probably want something along the lines of
found = app.activeDocument.findText();
if (found.length > 0) {
found[0].select();
}
findText() and findGrep() return an array.
Peter
Copy link to clipboard
Copied
Is your post at find/change script highlight found text not the same question??
Copy link to clipboard
Copied
Yes you're right, now that I understand the problem, my other post answers this question.
I was trying to access the found text in the findWhat field!
Thanks for all your comments, I'm starting to understand the InDesign DOM more