• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Finding text with variables

Participant ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 09, 2016 May 09, 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.

Votes

Translate

Translate
Mentor ,
May 05, 2016 May 05, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2016 May 05, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 05, 2016 May 05, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2016 May 05, 2016

Copy link to clipboard

Copied

I probably won't be able to help you with this. My expertise is in ExtendScript and FrameScript. I have written some FDK plugins over the years, but am not really interested in FDK development.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 05, 2016 May 05, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 06, 2016 May 06, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 06, 2016 May 06, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 06, 2016 May 06, 2016

Copy link to clipboard

Copied

You would know by the unique string that you replaced it with. I assume that you don't want to change any text that is inside the variable. If that is the case, then you are involved in something that is more complex than just coding technicalities. It is easy to change the text of a variable, but you would change the text for all instances of that variable.

I figured that you only want to make changes within the "Hello world my friend" substring. If you want to make changes within the variable text too, that is easy. Just retrieve the variable definition and change it.

var varDef = GetNamedVarFmt("<Definition name>");

varDef.Fmt = "Something other than Jonathan Foyl";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 09, 2016 May 09, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 09, 2016 May 09, 2016

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines