Hi,
I use a lot of Find/Change in my routine work to speed up my work. I also make use of ChainGREP JavaScript to collect all my GREP queries in one single JavaScript. The only problem with that Script is that it searches only once. So, if there’s something in the text that is occurring more than one time, then I have to run the script again and again until I guess that all the instances would have been replaced.
A similar thing happens with this code below. I have to run the JavaScript again and again until all the duplicate entries are gone.
This example script has only one GREP Find/Change Query. Three are other scripts with me with multiple GREP Find/Change Queries.
Can someone, add a loop to the JavaScript so that it can search again and again until the last occurrence is replaced.
// Query [[mc2001_Remove Duplicates]]
try {
app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
app.findGrepPreferences.properties = ({findWhat:"(^.+n.+?,)(.+r)1(.+r)"});
app.changeGrepPreferences.properties = ({changeTo:"$1$2", fillColor:"C=75 M=5 Y=100 K=0"});
changeObject.changeGrep();
}
catch (e) {alert(e)}
app.findChangeGrepOptions.properties = options;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
Complete JavaScript created using ChainGREP:
main();
function main() {
if (app.layoutWindows.length == 0) return;
if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) {
alert('Please select only one textframe or text range!');
return;
}
var changeObject = app.selection[0].parentStory;
if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
var doc = app.documents[0];
var style;
var options = app.findChangeGrepOptions.properties;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
// Query [[mc2001_Remove Duplicates]] -- If you delete this comment you break the update function
try {
app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
app.findGrepPreferences.properties = ({findWhat:"(^.+n.+?,)(.+r)1(.+r)"});
app.changeGrepPreferences.properties = ({changeTo:"$1$2", fillColor:"C=75 M=5 Y=100 K=0"});
changeObject.changeGrep();
} catch (e) {alert(e)}
app.findChangeGrepOptions.properties = options;
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
};
function getStyleByString(root, string, property) {
stringResult = string.match (/^(.*?[^]):(.*)$/);
var cStyleName = (stringResult) ? stringResult[1] : string;
cStyleName = cStyleName.replace (/:/g, ':');
remainingString = (stringResult) ? stringResult[2] : '';
var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
var cStyle = root[newProperty].itemByName(cStyleName);
if (remainingString.length > 0 && cStyle.isValid) cStyle = getStyleByString (cStyle, remainingString, property);
return cStyle;
};