Skip to main content
Inspiring
January 2, 2019
Question

find the text based on language

  • January 2, 2019
  • 3 replies
  • 1573 views

Hi,

     how to find the text based on specific language.. is there any mistake in following code

findParams = AllocatePropVals(2);

findParams[0].propIdent.num = Constants.FS_FindCharFmt;

findParams[0].propVal.valType = Constants.FTF_LANGUAGE;

findParams[0].propVal.sval =Constants.FV_LANG_ENGLISH;

var foundText = doc.Find(tloc, findParams);

This topic has been closed for replies.

3 replies

Inspiring
January 4, 2019

Hi all,

Thanks for you reply.. i am new in framemaker scripting.. your's suggestion and code very helpful to me

yes i have made mistake in my code and i think my question is not clear

ok, oPgf.Language returns hyphenation language right?

in clear,

oPgf.Language returns 1(English) for arabic content also..

in above document how to i find arabic content and english content

please help

4everJang
Legend
January 4, 2019

OK, in this case there is an override for the language for part of the paragraph. You will have to get the text strings from the paragraph and check which ones have the language setting you are looking for. This must be done for each paragraph.

var oaTextItems = oPgf.GetText( Constants.FTI_CharPropsChange );

for( i = 0; i < oaTextItems.length; i++ )

{

     if( oaTextItems.idata & Constants.FTI_LANGUAGE )

     {

          /*  This is a text item for which the character language has changed.

               You need to find the language at this point and check it for the target language */

               var oTLoc = new TextLoc( oPgf, oaTextItems.offset );

               var oProp = oDoc.GetTextPropVal( oTLoc, Constants.FP_Language );

               /*  Now you have the propVal that shows the language at the current location.

                    Check this against your target language. The text item starts at oaTexItem.offset

                    and ends at the offset of the next oaTextItem or the end of the paragraph if the

                    oaTextItem is the last one  */

     }

}

Good luck.

Inspiring
January 4, 2019

sorry,

var oDoc = app.ActiveDoc;

var oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

var oaTextItems = oPgf.GetText( Constants.FTI_CharPropsChange );

alert(oaTextItems )

it returns empty for above document..

4everJang
Legend
January 3, 2019

The Find method is notoriously hard to script. If Russ does not find a way to make a search for text in a particular language work, I am sure there will be no such option in the script method either.

If your content has full paragraphs in a particular language, it would probably be easier to run through all the paragraphs and checking their Language property. Something like this:

var oDoc = app.ActiveDoc;

var oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

var oaTargetPgfs = [ ];

while( oPgf.ObjectValid( ) )

{

     if( oPgf.Language == Constants.FV_LANG_GERMAN )

     {

          oaTargetPgfs.push( oPgf );

     }

     oPgf = oPgf.NextPgfInFlow;

}

/* Now you have an array of all the paragraphs that have the German language set. */

I have not tested the above code, so you will have to debug it if I misspelled some of the properties. But I think it should work. Note however that this does not cover the paragraphs in tables - you will have to cycle through all the paragraphs in tables in a bunch of nested loops:

for every table in the document:

     for every cell h the table:

          for every pgf in the cell

               check the language and add to array if there is a match

Good luck with this

4everJang

Legend
January 3, 2019

Indeed, I agree that the Find() method can be very tricky. What really confuses me this time is that I can't get the UI Find tool to work either. That is, I can't find text with a particular language. It does seem that the UI and my script are operating the same, though, so that's why I suspect a bug. But with the complexities and odd behavior of Find(), I really can't be sure.

Russ

frameexpert
Community Expert
Community Expert
January 3, 2019

From my earliest FrameScript and FDK days, I have never trusted the "Find" function that mimics the UI. So I have always used a "long" approach: I loop through all of the paragraphs in the document, and for each paragraph, loop through the character property changes. For each character property change, I test for what I am looking for. Below is a basic shell for doing this on a single paragraph. Note that I run the loop backwards in case I want to do something with each text range that may affect the offsets of my text items.

There are ways to specify exactly which changes you are looking for, but this general way is the simplest.

#target framemaker

var doc = app.ActiveDoc;

var pgf = doc.TextSelection.beg.obj;

processPropertyChanges (pgf, doc);

function processPropertyChanges (pgf, doc) {

    var end = Constants.FV_OBJ_END_OFFSET - 1, begin = 0, textRange;

   

    var textList = pgf.GetText (Constants.FTI_CharPropsChange);

    for (var i = textList.length - 1; i >= 0; i -= 1) {

        begin = textList.offset;

        if (begin !== end) {

            textRange = new TextRange (new TextLoc (pgf, begin), new TextLoc (pgf, end));

            doc.TextSelection = textRange;

            alert ("test the beginning of the text range for the property you are looking for.");

            end = begin;

        }

    }

    if (end > 0) {

        textRange = new TextRange (new TextLoc (pgf,0), new TextLoc (pgf,end));

        doc.TextSelection = textRange;

        alert ("test the beginning of the text range for the property you are looking for.");

    }

}

-Rick

www.frameexpert.com
Legend
January 3, 2019

Hi,

There are definitely some errors in your code. I can point them out; however, I am not able to get the code to work myself. Neither can I get it to work in the UI (FM2019). So I do not have the final answer. I suspect a bug, but perhaps I am using the tool incorrectly in both the script and the UI.

I think all three lines where you define the findParams are wrong. The first is close, except according to the FDK doc, FS_FindCharFmt doesn't do anything. And I do know that the valType should be the datatype of the parameter, and that a language parameter is an integer. So you need to use ival. Basically, here is what I think should work, but does not.

      var doc = app.ActiveDoc;

      var textRange = doc.TextSelection;

      var findParams = AllocatePropVals(2);

     

      findParams[0].propIdent.num = Constants.FP_Language;   

      findParams[0].propVal.valType = Constants.FT_Integer;   

      findParams[0].propVal.ival = Constants.FV_LANG_ENGLISH; 

     

      findParams[1].propIdent.num = Constants.FS_FindWrap;   

      findParams[1].propVal.valType = Constants.FT_Integer;   

      findParams[1].propVal.ival = false; 

    

     textRange = doc.Find(textRange.beg, findParams);

In my test document, this code just finds all the text in the flow. The same thing happens when I attempt the same search in the UI. So I don't know what is going on. Maybe this can help you some.

Russ