5 Replies Latest reply on Oct 17, 2016 7:46 AM by philippep2776167

    wrap text with element




      I'm using FM12 with structured docs. I'm looking to convert some Framescript into Extendscript language.

      I finish my first Extendscript this morning (too me 3 days) : searching Element and saving the attributes value into a text file.

      But I'm stuck converting my second one.


      In a structured doc, i want to wrap a selected text with a element "PRESS" into a element "PARA" (the element PRESS is allowed in the element PARA by the DTD/EDD).


      My Framescript (FS 6.0)

      Set vCurrentDoc = ActiveDoc;

      Set vSelect = vCurrentDoc.TextSelection;

      // Get the Element Def

      Get Object NewVar(vEltDef) Type(ElementDef) Name('PRESS');

      // Wrap the selected text

      Wrap ElementDef(vEltDef);

      // Select the new PRESS element

      Set vPRESS = ElementSelection.begin.child;

      // Create the attribs

      New AttributeList NewVar(vPRESSatt) AttrName('SMALLCAPS') Value('1');

      // Assign the Attributes to the tag

      Set vPRESS.Attributes = vPRESSatt;

      In Extendscript, how do i write it?


      var vCurrentDoc = app.ActiveDoc;

      var vSelect = vCurrentDoc.TextSelection;

      var vElem = vCurrentDoc.GetNamedElementDef("PRESS");

      var attrs = vElem.AttributeDefs;

      attrs[1].values = "ITAL";

      vElem.Attributes = attrs;


      The script add the PRESS element but the attribute is not fill. Why? and how to correct it?


      Many thanks for considering my request.


      Ce message a été modifié par : Philippe Pinpin Add info

        • 1. Re: wrap text with element
          Russ Ward Level 4

          Hi philippep2776167,


          It is a bit tricky to set an attribute. Below is a function that demonstrates how to do it. You can send the function either the name of the attribute you want to set or the index. I think this example should give you the information that you need.




          //Sets an attribute value by name or by index.
          //Send an index or an attribute name, not both.
          //The name is case sensitive.
          //Returns true if successful, false otherwise
          //It overwrites whatever is already there.
          function SetAttribute(elem, attrName, attrIndex, newValue)
              if(elem == null || !elem.ObjectValid()) return false;
              var returnVal = false;
              var attrs = elem.Attributes;
              for(var i = 0 ; i < attrs.length; i++) 
                  if (attrs[i].name == attrName ||
                      i == attrIndex) 
                      attrs[i].values = new Strings();
                      elem.Attributes = attrs;
                      i = attrs.length;
                      returnVal = true;
              return returnVal;
          • 2. Re: wrap text with element
            frameexpert Level 4

            Here is my version of a similar function.


            function setAttributeValue (element, name, value) {
                // Sets the value of an attribute on the element.
                var attrList = element.Attributes, i = 0;
                for (i = 0; i < attrList.length; i += 1) {
                    if (attrList[i].name === name) {
                        attrList[i].values[0] = value;
                        element.Attributes  = attrList;
            • 3. Re: wrap text with element
              philippep2776167 Level 1

              Hello Russ and frameexpert,


              I started my code with that function. But I had an "undefined is not an objet" error.

              So i decide to make my code as simply as possible to find my error.


              I think the problem come from my var declaration.

              When I set vElem = vCurrentDoc.GetNamedElementDef("PRESS") he create a vElem [Object ElementDef] but I think i need an [Object Element].


              So when I want to get the attribute i need to use the "AttributeDefs" instead of "Attributes". But i get some error with it.


              So how i can get [Object Element] from an [Object ElementDef] ?

              (Sorry if my question it's so obviously but i juste started javascript with Extendscript last week and my OOP knowledge is poor)

              • 4. Re: wrap text with element
                frameexpert Level 4

                For my function, you need an element object, the attribute name, and the value you want to set for the attribute. You can test it with the selected element by doing this:


                var doc = app.ActiveDoc;
                var element = doc.ElementSelection.beg.child;


                Now call the function, using element and the attribute names and values.



                • 5. Re: wrap text with element
                  philippep2776167 Level 1



                  lol Rick ... i was trying the same when i look your last code (your code look prettiest)


                  Thanks very much both of you

                  It work just fine.


                  PS: Juste made a mistake trying to set the attribute before creating the tag, but needed to set after