Skip to main content
Known Participant
December 28, 2023
Answered

"replace" command doesn't work.

  • December 28, 2023
  • 2 replies
  • 435 views

Hi; 

I find the paragraphs I want. I want to replace the number after the word "Key" with qq in the "Key qq" section. I wrote a simple js, but I think the replace command doesn't work.

Thanks....

 

 

#target indesign
redraw = app.scriptPreferences.enableRedraw;  
app.scriptPreferences.enableRedraw = 0;

app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Mehmet! …");

app.scriptPreferences.enableRedraw = redraw;  

function main()     
    {
var myDoc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat= "(?s)(Madde qq).+?\\r^\\d+\\r^(.+?\\r)";
//~ app.findGrepPreferences.appliedCharacterStyle = myCharStyle;
var   myParas = myDoc.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;

var P = myParas.length;
           
for ( var p = 0 ; p < P; p++ ) {

            var myMatch = myParas[p].contents.match(/Key\s*(\d+)/gi);
           
            alert (myMatch);
            
              var keyNumber = Number(myMatch[1]);
                  alert (keyNumber);
              myParas[p].contents.replace("qq", keyNumber);
    
}
}

 

 

 

This topic has been closed for replies.
Correct answer Peter Kahrel

When you use JavaScript's replace() like that you get problems with formatting, and when you change some content using InDesign's direct replace (as in myParagraph.characters.itemByRange(4,5).contents = '5') you often overwrite more than you want.

 

You'll have to find the paragraph pairs more or less the way you do (it can be simplified), then you use InDesign's GREP to replace qq with the key. Stripped down to the basics it goes like this:

 

 

 

app.findGrepPreferences = null;
// Collect just what you need
app.findGrepPreferences.findWhat= "Key qq.+\\r.+Key\\s\\d+";
var paras = myDoc.findGrep();
// Set up the find-Grep. Once only, outside the for-loop
app.findGrepPreferences.findWhat = 'Key\\s\\Kqq';
// Process the paragraph pairs back-to-front
// to avoide content-shift problems
for (i = paras.length-1; i >= 0; i--) {
  // Get the key
  key = paras[i].contents.match (/Key\s(\d+)/);
  // and replace 'qq' in with the key
  app.changeGrepPreferences.changeTo = key[1];
  paras[i].changeGrep();
}

 

 

2 replies

Robert at ID-Tasker
Legend
December 28, 2023

@uniq1 

 

@Peter Kahrel answer if of course more universal - but as you already have applied ParaStyles - you could loop through paragraphs - backwards of course - check for applied ParaStyle, if it's an "answer" then extract digits after word "Key", save this value, then go to the previous paragraph and check if "key" ParaStyle is applied - just in case - and replace "qq" with the value.

 

Of course both solutions will work correctly ONLY if you ALWAYS have exactly the same structure - always "key" before the "answer" Paragraph.

 

Mine might be a bit safer as if you won't have "key" paragraph - next "answer" paragraph will always give you correct value for the current "key" paragraph.

And to make it more robust - after each "key" paragraph you should clear digits variable and in case there is no "answer" paragraph between two "key" paragraphs - throw an error message that structure is wrong.

 

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
December 28, 2023

When you use JavaScript's replace() like that you get problems with formatting, and when you change some content using InDesign's direct replace (as in myParagraph.characters.itemByRange(4,5).contents = '5') you often overwrite more than you want.

 

You'll have to find the paragraph pairs more or less the way you do (it can be simplified), then you use InDesign's GREP to replace qq with the key. Stripped down to the basics it goes like this:

 

 

 

app.findGrepPreferences = null;
// Collect just what you need
app.findGrepPreferences.findWhat= "Key qq.+\\r.+Key\\s\\d+";
var paras = myDoc.findGrep();
// Set up the find-Grep. Once only, outside the for-loop
app.findGrepPreferences.findWhat = 'Key\\s\\Kqq';
// Process the paragraph pairs back-to-front
// to avoide content-shift problems
for (i = paras.length-1; i >= 0; i--) {
  // Get the key
  key = paras[i].contents.match (/Key\s(\d+)/);
  // and replace 'qq' in with the key
  app.changeGrepPreferences.changeTo = key[1];
  paras[i].changeGrep();
}

 

 

Robert at ID-Tasker
Legend
December 28, 2023

This GREP will find only numbers:

 

(?<=Key )(\d+)