4 Replies Latest reply on Aug 19, 2013 11:34 AM by Russ Ward

    Getting and setting attribute values [ExtendScript]

    Michael Müller-Hillebrand Level 4

      Hi colleagues,

       

      I got some code to get attribut values but have a hard time setting the value. The function SetAttributes() which I would expect seemingly does not exist.

       

      Getting an attribute value is similar to this:

       

      lvElem = pvDoc.ElementSelection.beg.child;

      lvAttrs = lvElem.GetAttributes();

      for(j=0; j<lvAttrs.len; j++) {

        Console(j + ': ' + lvAttrs[j].name + '="' + lvAttrs[j].values[0] + '"');

      }

       

      But how does setting an attribute value work?

       

      Thanks,

       

      - Michael

        • 1. Re: Getting and setting attribute values [ExtendScript]
          Chintu_123 Level 1

          Michael,

           

          Getting and setting the attributes have been made quite easier in the ExtendScript.

           

          One can get the attribute structure by querying the <AttributresEx> property of extend script. It returns the AttributesEx structure where we can change the desired attribute value and set it back. Sample snippet is below:

           

           

          attrs = ele.AttributesEx   //here ele is the element object whose attribute we want to get and attrs is "AttributesEx" structure

           

          attrs[0].values[0] = "Sample"  //here we are setting the string "Sample" to the first attribute of the element

           

          ele.AttributesEx = attrs  //Now we are setting back the modified AttributesEx strcuture to the element object

           

           

          Regards,

          Vikash

          1 person found this helpful
          • 2. Re: Getting and setting attribute values [ExtendScript]
            Michael Müller-Hillebrand Level 4

            Vikash,

             

            Thanks a lot.

             

            What is the difference between the AttributesEx and the Attributes arrays? In the documentation they seem to look identical.

             

            - Michael

            • 3. Re: Getting and setting attribute values [ExtendScript]
              Michael Müller-Hillebrand Level 4

              As AttributesEx did not work for me as I hoped it would, I came up with this using Attributes:

               

              // new attribute value

              lvNewAttr = new Attribute;

              lvNewAttr.name = 'attr_name'; // attribute name

              lvNewAttr.values.push('new string value'); // attribute value

              lvNewAttr.allow = false;

               

              lvAttrs = lvElem.Attributes;

              for(j=0; j<lvAttrs.length; j++) {

                 if (lvAttrs[j].name == lvNewAttr.name) {

                    lvAttrs[j] = lvNewAttr;

                    lvElem.Attributes = lvAttrs;

                    break;

                 }

              }

               

              HTH,

               

              - Michael

              • 4. Re: Getting and setting attribute values [ExtendScript]
                Russ Ward Level 4

                Hi,

                 

                I know this is an old thread but I ran across the same question and was helped by it. I came up with a different way that I think might be a little bit cleaner, more closely modeled to the FDK methodology. So, I thought I'd post it in case it might help anyone.

                 

                The following function sets an attribute value by name or index. Send an empty string to look by index or a negative index to look by name. Attribute indexes start at zero and follow the sequence in which the attributes appear in the Structure View.

                 

                It will replace any current attribute contents. It returns true if it thinks the operation was successful.

                 

                function elem_SetAttribute(elem, attrName, attrIndex, newValue)

                {

                    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();

                            attrs[i].values.push(newValue);

                           

                            elem.Attributes = attrs;

                            i = attrs.length;

                            returnVal = true;

                        }

                    }

                 

                    return returnVal;

                }