Skip to main content
K.Daube
Community Expert
Community Expert
August 17, 2021
Answered

ExtendScript: search for any character tag

  • August 17, 2021
  • 1 reply
  • 315 views

Dear experts,

I try to find any character format with the following script:

var aLocNames = [], j, jMax, oFindParms, oPgf, sTag, tRange, tRange, zz;
//
  tRange = new TextRange();
  oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
  tRange.beg.obj = tRange.end.obj = oPgf;   // set up the starting TR
  tRange.beg.offset = tRange.end.offset = 0; 
// Find any character format   iFS, iFV, sSearch, iMode, bWord, bCase, bBack (wildcard)
  oFindParms = KLD_F.GetFindParameters (5, 0, "*", 1, false, false, false);
  KLD_Z.InitFA_errno ();                  // reset - it is write protected
  tRange = oDoc.Find (tRange.beg, oFindParms);// initial find
  zz = FA_errno;
  oDoc.TextSelection = tRange;               // debug: indicate visually
  oDoc.ScrollToText(tRange);
$.bp(true);
  sTag   = tRange.beg.obj;     //<<<<<<< How to find the tag name?
  aLocNames.push(sTag);

  while(FA_errno === Constants.FE_Success) {
    tRange = oDoc.Find (tRange.beg, oFindParms);
    zz = FA_errno;                 // debug: 0 OK; -95 not found
    oDoc.TextSelection = tRange;   // debug: indicate visually
    oDoc.ScrollToText(tRange);
$.bp(true);
    sTag   = tRange.beg.obj;   //<<<<<<< How to find the tag name?
    aLocNames.push(sTag);
    tRange.beg.offset += 1;        // to continue search
  }

Text range is found and highlighted, if script is stopped.

However, tRange.beg.obj is a paragraph object, which does not have a property for the character format.

IMHO I need to do this:

Get the paragraph and search it's parts with textObj.GetText(Constants.FTI_CharPropsChange);

But how t  handle the flag Constants.FTF_CHARTAG ?

    This topic has been closed for replies.
    Correct answer frameexpert

    This may be useful to you Klaus:

    #target framemaker
    
    var doc, pgf, fmtsInUse;
    
    fmtsInUse = [];
    
    doc = app.ActiveDoc;
    pgf = doc.TextSelection.beg.obj;
    
    getPgfCharFmts (pgf, doc, fmtsInUse);
    
    alert (fmtsInUse);
    
    function getPgfCharFmts (pgf, doc, fmtsInUse) {  
      
        var end = Constants.FV_OBJ_END_OFFSET, begin = 0, textRange = 0, prop, charTag;  
          
        var textList = pgf.GetText(Constants.FTI_CharPropsChange);  
        for (var i = textList.length - 1; i >= 0; i -= 1) {  
           if (Constants.FTF_CHARTAG & textList[i].idata) {  
               begin = textList[i].offset;  
                if (begin !== end) {  
                    textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));  
                    prop = doc.GetTextPropVal (textRange.beg, Constants.FP_CharTag);  
                    if ((prop.propIdent.num) && (prop.propVal.sval !== "")) {  
                        // Get the character format that is applied to the text range.
                        charTag = prop.propVal.sval;
                        if (fmtsInUse.indexOf (charTag) === -1) {
                            fmtsInUse.push (charTag);
                        }
                    }  
                }  
                end = begin;  
            }  
        }  
    }
    

    1 reply

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    August 17, 2021

    This may be useful to you Klaus:

    #target framemaker
    
    var doc, pgf, fmtsInUse;
    
    fmtsInUse = [];
    
    doc = app.ActiveDoc;
    pgf = doc.TextSelection.beg.obj;
    
    getPgfCharFmts (pgf, doc, fmtsInUse);
    
    alert (fmtsInUse);
    
    function getPgfCharFmts (pgf, doc, fmtsInUse) {  
      
        var end = Constants.FV_OBJ_END_OFFSET, begin = 0, textRange = 0, prop, charTag;  
          
        var textList = pgf.GetText(Constants.FTI_CharPropsChange);  
        for (var i = textList.length - 1; i >= 0; i -= 1) {  
           if (Constants.FTF_CHARTAG & textList[i].idata) {  
               begin = textList[i].offset;  
                if (begin !== end) {  
                    textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));  
                    prop = doc.GetTextPropVal (textRange.beg, Constants.FP_CharTag);  
                    if ((prop.propIdent.num) && (prop.propVal.sval !== "")) {  
                        // Get the character format that is applied to the text range.
                        charTag = prop.propVal.sval;
                        if (fmtsInUse.indexOf (charTag) === -1) {
                            fmtsInUse.push (charTag);
                        }
                    }  
                }  
                end = begin;  
            }  
        }  
    }
    
    K.Daube
    Community Expert
    K.DaubeCommunity ExpertAuthor
    Community Expert
    August 17, 2021

    Thank You very much, Rick!

    I just had to put a loop around to get a complete list of the used character tags - and then applied a sort which removed duplicates:

     

      oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
      while (oPgf.ObjectValid()) {
        getPgfCharFmts (oPgf, oDoc, aLocNames);
        oPgf = oPgf.NextPgfInDoc
      }

     

    The catalogue may report more character formats as used than this function does. IMHO the catalogue is wrong, because the additionally flagged formats can not be found with the Find/Change dialogue…