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

What's wrong with this find-replace script?

Community Expert ,
Nov 04, 2021 Nov 04, 2021

Dear all,

Colleague Thomas Becker used some of my scripts from the collection FMjsxLib to do this:

  1. Find the text-range with a particular character format ("window" - red) text-at-start.pngexpand image
  2. At the end of this range insert a closing quote and format it with "sCharFmtQuote"
  3. At the beginning of this range insert an opening quote and also format it with "sCharFmtQuote"text-after-insert,find.pngexpand image
  4. Advance the text location behind the closing quote and find the next occurrence of "window".

Both the test document and the script are  here (reduced to the minimum by me): enclose_with_double_quotes.zip

The main probem is here:

var trFind = new TextRange();
    trFind.beg.obj = trFind.end.obj = tr.beg.obj;
    trFind.beg.offset = tr.end.offset + sQuoteOpen.length + sQuoteClose.length;
    trFind.end.offset = trFind.beg.offset;
$.bp(true);
    tr = oDoc.Find(trFind.beg, findParams);

At he break point the variables have the status as shown to the left and after executing the next statement (find) the status depicted on t he right:

Variables-before-after-Find.pngexpand image

→ Although trFind points to the location after the closing quote, the object is found at the old location.

I have explicitly set up an independent object trFind, as I know that o2 = o1 makes not a copy of object o1, but an 'alias'. What's going wrong here?

The second problem i see is this

  • The closing quote is inserted and formatted correctly (black)
  • The opening quote is inserted, but not formatted correctly (red)

While inspecting this situation with the cursor and looking into the status line i discover this (the yellow higlight is just an idication):

Char-format-of-closingQuote.pngexpand image

  • Moving the cursor from left to right via "ht " to the opening quote and selecting it reports no character format.
  • Moving the cursor from right to left via "mmiZ" to the opening quote and selecting it reports f "window". But this obviously overrun by colour red - not indicated by a *

Side note

The green wiggles after t he closing German quote are the result of an old bug, which was claimed to be fixed.

TOPICS
Scripting
495
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 , Nov 08, 2021 Nov 08, 2021

Towards a solution of the main problem

After much tinkering I have now a solution for all cases

  1. only one item in the whole text frame
  2. item at the beginning or end of the paragraph
  3. multiple items in the paragraph

A persky problem was to find a termination condition for the while loop. I had a number of conditions for the cases 2 and 3, but case 1 caused a loop. So I ended up with a brute force condition to stop the while loop: if a terminating quote already exists, we are done.

There is not muc

...
Translate
Community Expert ,
Nov 04, 2021 Nov 04, 2021

Actually this is my problem, and as I had used Klaus' script library as a resource, I had asked Klaus for help.

I have only little experience with ExtendScript and would not be able to check all variables and properties.

Klaus and all others, thank you very much for your help and engagement!

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
Community Expert ,
Nov 04, 2021 Nov 04, 2021

Solution to the second problem

The second problem was caused  by an incomplete character format quote (colour = AsIs).

The closing quote appers in black, because that's the colour of the following characters in the string. See this discussion.

For the same reason the opening quote gets colour red from the following characters...

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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

The main probem - various findings

  1. Even if I set the position to start the next find explicitly outside the first range, I get the same bad result:
    trFind.end.offset = trFind.beg.offset = 35;​
  2. This is t he result, if maxLoop is set to 2 (4 iterations): Find-repl-loop.pngexpand image
  3. The search advances correctly, if nothing is inserted. Hence I must check the insertion process more carefully.

 

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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

Towards a solution of the mainproblem

I have now changed the order of insertion: first the opening, then the closing quote.

  while(FA_errno === Constants.FE_Success) { // && nLoopCnt++ < 2*loopMax
    oDoc.TextSelection = oTR;
    oPfg = oTR.beg.obj;
    lFound = oTR.end.offset- oTR.beg.offset
//  insert the opening quote at the beginning of the text range and apply character tag
    oDoc.AddText(oTR.beg, sQuoteOpen);
    oTL1 = new TextLoc(oTR.beg.obj, oTR.beg.offset);
    oTL2 = new TextLoc(oTR.beg.obj, oTR.beg.offset + sQuoteOpen.length);
    oTRQuoteOpen = new TextRange (oTL1, oTL2);
  	ApplyCharFmt (oTRQuoteOpen, sCharFmtQuote, oDoc);
//  insert the closing quote at the end of the text range and apply character tag
    oTL1 = new TextLoc(oTR.beg.obj, oTL2.offset + lFound);;
    oTL2 = new TextLoc(oTR.beg.obj, oTL2.offset + lFound + sQuoteClose.length);
    oDoc.AddText(oTL1, sQuoteClose);
    oTRQuoteClose = new TextRange (oTL1, oTL2);
    ApplyCharFmt (oTRQuoteClose, sCharFmtQuote, oDoc);

    oTRnext = new TextRange();         // advance behind the quoted string & find again
    oTRnext.end.obj    = oTRnext.beg.obj = oPfg;
    oTRnext.beg.offset = oTR.end.offset + sQuoteOpen.length + sQuoteClose.length;
    oTRnext.end.offset = oTRnext.beg.offset;
$.bp(true);
    oTR = oDoc.Find (oTR.beg, oFindParams);   // oTRnext is not honoured!
$.bp(true);
  }

This works correctly as long as the found string is not terminated by the ¶ mark. For this effect I need to dig deeper.

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
Community Expert ,
Nov 08, 2021 Nov 08, 2021
LATEST

Towards a solution of the main problem

After much tinkering I have now a solution for all cases

  1. only one item in the whole text frame
  2. item at the beginning or end of the paragraph
  3. multiple items in the paragraph

A persky problem was to find a termination condition for the while loop. I had a number of conditions for the cases 2 and 3, but case 1 caused a loop. So I ended up with a brute force condition to stop the while loop: if a terminating quote already exists, we are done.

There is not much use of the Do-not-Wrap setting in the Find parameters. This works only for searching within a text range - not when the search is 'restarted' in a loop. See Markus Wiedenmaier 2012.

The now working script and test document are here: Enclose-with-quotes.zip

 

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