6 Replies Latest reply on Jan 2, 2015 5:41 PM by frameexpert

    applying fcode to a text range?

    Alan Houser Level 1

      Hi all,

       

      I'm trying to apply an fcode to a text range.

       

      If I manually select text, this code makes the text bold:

      fcodes = new Array();

      fcodes[0] = FCodes.TXT_BOLD;

      Fcodes(fcodes);

       

      However, if I try to programmatically select the text, the fcode has no effect:

      var tr = new TextRange (); 

      tr.beg.obj = tr.end.obj = pgf; 

      tr.beg.offset = 0; 

      tr.end.offset = 5;

      fcodes = new Array();

      fcodes[0] = FCodes.TXT_BOLD;

      Fcodes(fcodes);

       

      I fear I'm missing something simple or obvious. Must my code explicitly toggle the text range as "selected"?

       

      -Alan

        • 1. Re: applying fcode to a text range?
          frameexpert Level 4

          Hi Alan,

           

          Yes, FCodes work on the active document and mimic what you do interactively. So to use this FCode, you need to actually have the text selected. You should be able to add this before the Fcodes(fcodes) command:

           

          app.ActiveDoc.TextSelection = tr;

           

          Please let me know if you have any questions or comments.

           

          Rick

          • 2. Re: applying fcode to a text range?
            frameexpert Level 4

            I am not a big fan of Fcodes and rarely use them in my scripts. They only work on the active document and there are usually better, more reliable ways of accomplishing things. For example, here is how you can use code to apply a Bold character format to a text range, assuming that the "Bold" character format exists in the active document. The nice thing is that the applyCharFmt function could be used in any script and will always work the same way. And you can pass it any valid text range, even in an invisible document.

             

            #target framemaker
            
            // Get the active document.
            var doc = app.ActiveDoc;
            // Get the paragraph containing the insertion point.
            var pgf = doc.TextSelection.beg.obj;
            // Make a text range.
            var tr = new TextRange (); 
            tr.beg.obj = tr.end.obj = pgf; 
            tr.beg.offset = 0; 
            tr.end.offset = 5;
            
            // Apply the Bold character format.
            applyCharFmt (tr, "Bold", doc);
            
            function applyCharFmt (textRange, name, doc) {
                
                var charFmt = 0;
                
                charFmt = doc.GetNamedCharFmt (name);
                if (charFmt.ObjectValid()) {
                    doc.SetTextProps (textRange, charFmt.GetProps());
                }
            }
            
            • 3. Re: applying fcode to a text range?
              Alan Houser Level 1

              Thanks, Rick. Setting the text selection via "app.ActiveDoc.TextSelection = tr;" was what I needed.

               

              Apologies ... I pulled the "bold" example out of the air. I actually want to invoke the Init Caps operation, presented by the GUI in the Text Formatting toolbar.

               

              I think it's defensible to use the fcode for this (I'm changing character case, not properties), although I may need to drop back and do it by regular expression.

               

              Now that I've gotten this far, "TXT_INITCAPS" (the apparent fcode for this operation, from fcodes.h) has no effect, on a manually or programmatically-selected text range.

               

              If anybody has any insight into why TXT_INITCAPS has no effect, I would be grateful.

               

              -Alan

              • 4. Re: applying fcode to a text range?
                frameexpert Level 4

                Hi Alan, Here is the FCode you need: KBD_INITCAP. -Rick

                • 5. Re: applying fcode to a text range?
                  Alan Houser Level 1

                  Wow ... thank you!

                   

                  Did you know that by other than finding it in fcodes.h and trying it?

                  • 6. Re: applying fcode to a text range?
                    frameexpert Level 4

                    Believe it or not, I looked in the FrameScript reference manual. I searched on "cap" and located it.