Skip to main content
Ch__Efstathios
Inspiring
May 4, 2016
Answered

Finding text with variables

  • May 4, 2016
  • 10 replies
  • 1482 views

I am facing the following problem:

I have a paragraph that I want to replace some text from it.

Say that I want to search for text "Hello world my friend Jonathan Foyl" and replace it with some other text.

When I use the function F_ApiFind in order to find the text it does not work because the "Jonathan Foyl" is a Frame Maker variable!

If it is just simple text then the F_ApiFind works successfully and it finds the text.

How I can handle this?

This topic has been closed for replies.
Correct answer Arnis Gubins

All of the variables are defined in the FM documents catalog. You could parse that variable list first and assign the variables unique ( &random) strings. Then iteratively do the replacements as Russ suggests. Finish off with another pass that replaces the unique strings with their corresponding original variables.

10 replies

Ch__Efstathios
Inspiring
May 9, 2016

I think it is not so easy. Look how my application works. I extract the text of the current paragraph as a single text. In this case I have just a single text from the paragraph. In my application is just a string. I have no other information. In FM this paragraph actually represents some parts of the text as variables. I have no information in my application which parts are variables. Just a single string. So, for example in my application I have: This is a paragraph which contains some text. However, In FrameMaker the "paragraph" and "some text" are variables. Now, I cannot replace "is a paragraph" with "just anything" since the substring contains variables.

Arnis Gubins
Arnis GubinsCorrect answer
Inspiring
May 9, 2016

All of the variables are defined in the FM documents catalog. You could parse that variable list first and assign the variables unique ( &random) strings. Then iteratively do the replacements as Russ suggests. Finish off with another pass that replaces the unique strings with their corresponding original variables.

Ch__Efstathios
Inspiring
May 5, 2016

I really didn't also find a way how to do it. It seems very difficult.

Legend
May 6, 2016

I was thinking, there is what we might call the "shotgun" approach. You could go through first and replace every variable with some unique text. Like, replace each variable with "ThisIsSomeCompleteBullJiveTextHere", then you could do the search based on text only, searching for "Hello world my friend ThisIsSomeCompleteBullJiveTextHere".  After you are done, go through and restore the variables. This is a wild, overblown approach, but it should work if the search/replace is reliable. It might also be easier than sifting through text items, paragraph by paragraph.

Russ

Ch__Efstathios
Inspiring
May 6, 2016

Hi, when I have "Hello world my friend ThisIsSomeCompleteBullJiveTextHere" I have it like a text and I don't know which part is a variable.

Legend
May 5, 2016

Interesting question. I think this maybe is not possible with F_ApiFind()? I don't know of a way to do it in the GUI, so I don't think there is any way to do it with F_ApiFind(). If you knew how to do it in the GUI, you could model that in your code.

I think you may have to search for "Hello world my friend ", then test the text location at 1 character offset from the range for the variable. Unless somebody else can come up with something more clever, which is possible.

Russ

frameexpert
Community Expert
Community Expert
May 5, 2016

One possibility is to get all of paragraph's text into a string and then test the contents of the string. This is how I do it with ExtendScript:

function getText (textObj, doc) {

    // Gets the text from the text object.

    var text = "";

    // Get a list of the strings in the text object or text range.

    if (textObj.constructor.name !== "TextRange") {

        var textItems = textObj.GetText(Constants.FTI_String);

    } else {

        var textItems = doc.GetTextForRange(textObj, Constants.FTI_String);

    }

    // Concatenate the strings.

    for (var i = 0; i < textItems.len; i += 1) {

        text += (textItems.sdata);

    }

    return text; // Return the text

}

You would have to adapt this with FDK code but conceptually it should work. If there is a match, things might be a bit tricky to actually get the required text range, but let me think about it and see if I can come up with a solution.

www.frameexpert.com
Ch__Efstathios
Inspiring
May 5, 2016

Hi frameexpert!

What I really want is to implement find/replace feature.

I recognize the current paragraph. Move the selection point to the beginning of the paragraph. And repeatedly start finding matches and replace these until the paragraph ends. Find/replaces only in current paragraph.

I have done it but when I try to find text which contains variables the F_ApiFind does not find the text at all.