Copy link to clipboard
Copied
Hi all
Been a long time since I looked at JS - the scripts I produced with the help of this forum have been running well (and saving us lots if time) but I have something new I need to do and I am quite rusty. Any help would be appreciated.
I have a few saved GREP expressions that I want to run on some particular text in my document but not others. The saved GREPs are run using this code;
app.loadFindChangeQuery ("change all text to black", SearchModes.grepSearch);
app.activeDocument.changeGrep();
My plan was to use a GREP search to find all paras and then go through all the paras and only apply the saved GREP expression to paras not styled with a specific para style. Unfortunately the GREP is applied to all all text regardless of the para style - the same as if it was run manually using the F/C interface. My code;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "^.+$";
f = app.activeDocument.findGrep ();
for (i = 0; i < f.length; i++) {
if (f.appliedParagraphStyle.name!="References")
{
app.loadFindChangeQuery ("change all text to black", SearchModes.grepSearch);
app.activeDocument.changeGrep();
}
}
How can I get these GREP expressions to run only on the text I want it to run on? Do I have to first remove any instances of paragraphs with the offending para style from my variable f before my for loop?
thanks,
Iain
in the last line, try this:
f.findGrep()
I haven't tested it but f is an array of text objects found by your first f/c operation, so you simply limit your second of/c to those chunks of text.
I also wonder if the second f/c is needed at all. Not sure what your saved query is doing, but if it's just changing the text color to black, maybe you can just do that to each f
Copy link to clipboard
Copied
in the last line, try this:
f.findGrep()
I haven't tested it but f is an array of text objects found by your first f/c operation, so you simply limit your second of/c to those chunks of text.
I also wonder if the second f/c is needed at all. Not sure what your saved query is doing, but if it's just changing the text color to black, maybe you can just do that to each f
Copy link to clipboard
Copied
Thanks Robert. I changed my last line to f.changeGrep(); and that did it. I just couldn't make that leap myself - as I said, I haven't looked at this stuff for a while! In the if loop I'm going to run a whole slew of saved GREPs we already hold as this will be easier for me than doing them as JS.
Thanks again,
Iain
Copy link to clipboard
Copied
What is your saved query? "^.+?$" will find the whole para.
Why not search directly for your para with the specific style?
app.findGrepPreferences.appliedParagraphStyle = "test";
var res = app.activeDocument.findGrep();
Kai
Copy link to clipboard
Copied
Kai - thanks for your response. I expect the saved query (which was just an example - there will be many executed here) applies to all the text (".+") hence me trying to restrict it using JS to most of the different paragraph styles (quite a lot) but exclude one or two. These are saved queries we use a lot so I didn't want to mess around with them either (plus there are going to be a lot).
best wishes,
Iain