10 Replies Latest reply on May 9, 2016 6:32 AM by Arnis Gubins

    Finding text with variables

    Ch. Efstathios

      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?

        • 1. Re: Finding text with variables
          Russ Ward Level 4

          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

          • 2. Re: Finding text with variables
            frameexpert Level 4

            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[i].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.

            • 3. Re: Finding text with variables
              Ch. Efstathios Level 1

              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.

              • 4. Re: Finding text with variables
                frameexpert Level 4

                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.

                • 5. Re: Finding text with variables
                  Ch. Efstathios Level 1

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

                  • 6. Re: Finding text with variables
                    Russ Ward Level 4

                    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

                    • 7. Re: Finding text with variables
                      Ch. Efstathios Level 1

                      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.

                      • 8. Re: Finding text with variables
                        Russ Ward Level 4

                        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";

                        • 9. Re: Finding text with variables
                          Ch. Efstathios Level 1

                          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.

                          • 10. Re: Finding text with variables
                            Arnis Gubins Adobe Community Professional & MVP

                            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.