12 Replies Latest reply on Jun 3, 2016 7:26 AM by Lynne A. Price

    Rename ElementDef

    ethanb10986590

      In a structured document, is there a way to remane an ElementDef. I want to search for some specific names and rename them.

      Example code:

      var doc = app.ActiveDoc
      var elem = doc.MainFlowInDoc.HighestLevelElement;
      if (elem.ElementDef.Name == "some_string"){
           //Change the name of the ElementDef here
      }
      

       

      I am assuming I need to use the SetProps function, something like this.

      var props = elem.ElementDEf.GetProps();
      var i = GetPropIndex(props, /*Constants.something_to_get_the_name*/ ) //need help here I think.
      props[i].propVal = "some_new_string"
      elem.ElementDEf.SetProps(props)
      

       

      Is it possible to do this?

      Thanks

      Ethan

        • 1. Re: Rename ElementDef
          4everJang Level 3

          Ethan,

           

          You ask for renaming the element definition, but from your code I suspect you want to retag an element instead. Those are two very different things. And there are different ways of doing this, depending on what exactly you want to achieve.

           

          Jang

          • 2. Re: Rename ElementDef
            ethanb10986590 Level 1

            I guess retaging is what I want.

             

            I have a structured FM file that I want to export to xml, but there are some tags in the FM doc that begin with a number (ex. "2column"), and xml doesnt allow for tags to start with numbers. I want to find those tags and change them.

            • 3. Re: Rename ElementDef
              4everJang Level 3

              No need for scripting then. Make a full list of the elements to be renamed, then open the read-write rules for the structured application you will be using to save to XML and retag the elements in those rules. One of those rules would look like this:

               

              element "column" is fm element "2column".

               

              Then, when the names were corrected in the XML, use another structured application to read the file back into FrameMaker, which uses the default read-write rules. This effectively retags the elements in your FM file.

               

              Will this work for you?

              • 4. Re: Rename ElementDef
                ethanb10986590 Level 1

                That sounds like it should work. I will give it a try.

                 

                Will those new rules be maintained after I close FrameMaker?

                Also will those rules be applied if I save the document using a script. I am doing a lot of this xml conversion with a script because I have hundreds of documents that need to be converted.

                • 5. Re: Rename ElementDef
                  frameexpert Level 4

                  If you decide to use scripting instead of Read/Write Rules, here is code that you can modify for your use. In this example, I am renaming Title elements to Title2.

                   

                  #target framemaker
                  
                  var doc = app.ActiveDoc;
                  var elementDef = doc.GetNamedElementDef ("Title");
                  var props = elementDef.GetProps ();
                  var i = GetPropIndex (props, Constants.FP_Name);
                  props[i].propVal.sval  = "Title2";
                  elementDef.SetProps (props);
                  

                   

                  -Rick

                  • 6. Re: Rename ElementDef
                    4everJang Level 3

                    Hi Ethan,

                     

                    Yes, the read-write rules are in a text file that is used by the structured application which saves the FM file to XML.

                     

                    You set up your own structured application (go to Edit Application Definitions) and name it whatever you want. Link your read-write rules to it. Then in a script use the Save method and call out your structured application. Takes a bit of trying to make it work, but with 100s of files to process, this is worth it.

                    • 7. Re: Rename ElementDef
                      Russ Ward Level 4

                      Rick, you are smarter than me about this stuff. Why doesn't this work? How do I know when I have to use GetProps()/SetProps()?

                       

                      var doc = app.ActiveDoc;  
                      var elementDef = doc.GetNamedElementDef ("Title");  
                      elementDef.Name  = "Title2";  
                      

                       

                      Russ

                      • 8. Re: Rename ElementDef
                        frameexpert Level 4

                        Hi Russ, It is funny that you should ask. I tried your approach with FrameScript and it does work, even though it doesn't work with ExtendScript. So, the less-than-technical answer is: I try the simple approach and if it doesn't work, I use the GetProps/SetProps method. I am curious about how this would be done in the FDK. Would the "simple" approach work? -Rick

                        • 9. Re: Rename ElementDef
                          Lynne A. Price Level 4

                          Jang,

                             R/w rules need not be text files. They can be structured or unstructured FM files as well. I have a structured template for r/w rules, although it needs to be updated to recent releases of FM. I'll try to get that done in the next week or two and make it available.

                             The structured template prevents syntax errors. Its Element Catalog and Smart Catalog make editing easy just as they do in any structured document. While the Structured Application Designer that is available on the StructureTools menu also prevents syntax errors and reminds the user about available rules, the structured template offers significant advantages over the Structured Application Designer. In particular:

                          • To create multiple similar rules, the user can copy an existing rule and modify it.
                          • Users can see multiple completed rules when editing the file.
                          • Comments are permitted.
                          • Rules can be divided into sections. Section titles are formatted as comments and hence are ignored when FM processes the rules.

                                        --Lynne

                          • 10. Re: Rename ElementDef
                            4everJang Level 3

                            Hi Lynne,

                             

                            I am aware of that, but I did not want to make things too complicated for Ethan, as he appears to be a beginner in structured FM. In fact, I have already created modular sets of structured FM files for conversion tables, so doing that for read-write rules would be just as straightforward.

                             

                            Do you have the read-write rule app available for geeks like me to download and test it ?

                             

                            Thanks

                            • 11. Re: Rename ElementDef
                              ethanb10986590 Level 1

                              Thanks for the code. I will give both of the methods a try and see which one works better in the process.

                               

                              Thanks for keeping it simple for me. I am new to this, just started with framemaker about a week ago.

                              • 12. Re: Rename ElementDef
                                Lynne A. Price Level 4

                                Jang,

                                   I haven't updated the structured rules template since FM 9. That template works for later versions of FM but does not support FM properties added to the rules since. I will try to find time to update it soon.

                                   --Lynne