简体   繁体   中英

ExtendScript search/replace loop

I am using ExtendScript with InDesign to have some simple search and replace to change a GREP .

If I write them out one by one everything works fine.

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findChangeGrepOptions = NothingEnum.nothing;
app.findGrepPreferences.findWhat = '^~8 ';
app.changeGrepPreferences.changeTo = `~8\\t`;
myDoc.changeGrep();

But I want to tidy things up and create a loop that runs two arrays containing the search and the find.

var findWhat = [ '^~8 ', '^·( |\\t)', '~S', '^ +', ' +$', '  +', '~P', '^\\t+(?=\\r)', '\\n', '\\r\\r\\r+'];

var changeTo = ['~8\\t','~8\\t',' ', '','',' ','\\r','','\\r','\\r\\r'];

for(var i = 0; i < findWhat.lenght; i++){
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findChangeGrepOptions = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = findWhat[i];
    app.changeGrepPreferences.changeTo = changeTo[i];
    myDoc.changeGrep();
}

This doesn't give me an error but it doesn't change anything either. Is there a way to have some kind of error handling of notation set that can provide me with some information?

I'm edditing the code in ExtendScript Toolkit

I found my own embarrassing solution.

The loop doesn't loop because it doesn't understand what lenght means.
Changed it to length and it runs fine......

var findWhat = [ '^~8 ', '^·( |\\t)', '~S', '^ +', ' +$', '  +', '~P', '^\\t+(?=\\r)', '\\n', '\\r\\r\\r+'];

var changeTo = ['~8\\t','~8\\t',' ', '','',' ','\\r','','\\r','\\r\\r'];

for(var i = 0; i < findWhat.length; i++){
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findChangeGrepOptions = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = findWhat[i];
    app.changeGrepPreferences.changeTo = changeTo[i];
    myDoc.changeGrep();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM