Translate metadata to field content

Niggemann, Bernd Bernd.Niggemann at
Fri Feb 21 05:34:39 EST 2020

Hi Jacque,

Jacque wrote:

 > put the styledText of fld 1 into tDataA
 > put 0 into tTotalChars
 > put 0 into tStartChar
  >repeat with i = 1 to the number of elements in tDataA
    >put tDataA[i]["runs"] into tRunsA
    >repeat with j = 1 to the number of elements in tRunsA
    > put tRunsA[j] into tRunA
      >add the num of chars in tRunA["text"] to tTotalChars
     > if tRunA["metadata"] is pTag then
        >if tStartChar = 0 then
         > put tTotalChars - len(tRunA["text"]) + 3 into tStartChar
       > end if
      >else if tStartChar > 0 then
        >put tTotalChars - len(tRunA["text"]) into tEndChar
        >select char tStartChar to tEndChar of fld 1
        >select empty
        >set the backcolor of char tStartChar to tEndChar of fld 1 to "yellow"
        >return tStartChar & comma & tEndChar
     >end if
    >end repeat
  >end repeat

the styledArray does not include the returns at the end of a line. You have to add them if you address chars/codeUnits of the whole text. Initializing tTotalChars with -1 lets you add 1 to tTotalChars in each iterations of the outer repeat loop. -1 because the first line is not has no preceding return.
Also add 1 to calculate tStartChar otherwise you point to the last char of preceding run.

  put -1 into tTotalChars -- note -1
  put 0 into tStartChar
  repeat with i = 1 to the number of elements in tDataA
    add 1 to tTotalChars -- account for returns
    put tDataA[i]["runs"] into tRunsA

-- note add 1
put tTotalChars - len(tRunA["text"]) +1 into tStartChar -- mark char 1 of target

Additionally in your implementation if the target run with the metadata you look for is the last run of the array nothing is returned.

Kind regards

More information about the use-livecode mailing list