Skip to main content
Michael_Müller-Hillebrand
Legend
November 17, 2011
Question

Getting and setting attribute values [ExtendScript]

  • November 17, 2011
  • 2 replies
  • 3914 views

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.name + '="' + lvAttrs.values[0] + '"');

}

But how does setting an attribute value work?

Thanks,

- Michael

This topic has been closed for replies.

2 replies

Legend
August 19, 2013

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.name == attrName ||

            i == attrIndex)

        {

            attrs.values = new Strings();

            attrs.values.push(newValue);

           

            elem.Attributes = attrs;

            i = attrs.length;

            returnVal = true;

        }

    }

    return returnVal;

}

Inspiring
November 17, 2011

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

Michael_Müller-Hillebrand
Legend
November 17, 2011

Vikash,

Thanks a lot.

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

- Michael

Michael_Müller-Hillebrand
Legend
November 17, 2011

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.name == lvNewAttr.name) {

      lvAttrs = lvNewAttr;

      lvElem.Attributes = lvAttrs;

      break;

   }

}

HTH,

- Michael