Skip to main content
Alan Houser
Community Expert
Community Expert
January 2, 2015
Answered

applying fcode to a text range?

  • January 2, 2015
  • 2 replies
  • 830 views

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

This topic has been closed for replies.
Correct answer frameexpert

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());

    }

}

2 replies

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
January 2, 2015

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());

    }

}

www.frameexpert.com
Alan Houser
Community Expert
Community Expert
January 2, 2015

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

Alan Houser
Community Expert
Community Expert
January 2, 2015

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


Wow ... thank you!

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

frameexpert
Community Expert
Community Expert
January 2, 2015

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

www.frameexpert.com