Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
10

"replace" command doesn't work.

Participant ,
Dec 28, 2023 Dec 28, 2023

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);
    
}
}

 

 

Screenshot_1.jpg

 

TOPICS
Scripting
337
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 28, 2023 Dec 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 
...
Translate
Community Expert ,
Dec 28, 2023 Dec 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();
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 28, 2023 Dec 28, 2023

This GREP will find only numbers:

 

(?<=Key )(\d+)

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 28, 2023 Dec 28, 2023
LATEST

@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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines