Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Getting and setting attribute values [ExtendScript]

Advocate ,
Nov 17, 2011 Nov 17, 2011

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

TOPICS
Scripting
4.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Nov 17, 2011 Nov 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 17, 2011 Nov 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 17, 2011 Nov 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Aug 19, 2013 Aug 19, 2013
LATEST

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines