Copy link to clipboard
Copied
Dear all,
Colleague Thomas Becker used some of my scripts from the collection FMjsxLib to do this:
- Find the text-range with a particular character format ("window" - red)
- At the end of this range insert a closing quote and format it with "sCharFmtQuote"
- At the beginning of this range insert an opening quote and also format it with "sCharFmtQuote"
- 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:
→ 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):
- 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.
1 Correct answer
Towards a solution of the main problem
After much tinkering I have now a solution for all cases
- only one item in the whole text frame
- item at the beginning or end of the paragraph
- 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
...Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
The main probem - various findings
- 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;​
- This is t he result, if maxLoop is set to 2 (4 iterations):
- The search advances correctly, if nothing is inserted. Hence I must check the insertion process more carefully.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Towards a solution of the main problem
After much tinkering I have now a solution for all cases
- only one item in the whole text frame
- item at the beginning or end of the paragraph
- 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

