Copy link to clipboard
Copied
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
}
But how does setting an attribute value work?
Thanks,
- Michael
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Vikash,
Thanks a lot.
What is the difference between the AttributesEx and the Attributes arrays? In the documentation they seem to look identical.
- Michael
Copy link to clipboard
Copied
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
lvAttrs
lvElem.Attributes = lvAttrs;
break;
}
}
HTH,
- Michael
Copy link to clipboard
Copied
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;
}