Copy link to clipboard
Copied
Hi Scripters, I know from Russ Ward's samples how to delete an attribute value. But how do I delete the attribute itself from the element? The attribute is undefined in the document's EDD, that is, it shows as red in the structure view. FrameScript has a command for deleting an "undefined" attribute from an element, but I don't see a way to do this in ExtendScript. Any help will be appreciated. -Rick
I see that the FDK has an F_ApiDeleteUndefinedAttribute() function, but I don't see an equivalent in ExtendScript.
Copy link to clipboard
Copied
Hi Rick,
Apologies for the delay in response... I was on vacation for a while. I do see an equivalent ES method and it works OK for me, for example:
doc.DeleteUndefinedAttribute ("UndefinedAttrName", Constants.FV_Element, elem);
...where elem is the element object. Did you try this?
Russ
Copy link to clipboard
Copied
This is not working for me. More important, there is no warning or error report that says I'm doing this wrong.
I really appreciate if you could provide some guideline. Thanks!
I'm using FM 2017 Version: 14.0.0.361
Both FA_errno and the return value are zero after the function call.The code is pretty simple:
#target framemaker
var flow, root;
doc = app.ActiveDoc;
flow = doc.MainFlowInDoc;
root = flow.HighestLevelElement;
$.writeln("root="+ root.ElementDef.Name + " Attrs#=" + (root.ElementDef.GetAttributeDefs()).length);
doc.DeleteUndefinedAttribute("UndefinedAttrName", Constants.FV_Element, root);
Copy link to clipboard
Copied
Hi depaul, you need to put the actual attribute name in the method. For example:
doc.DeleteUndefinedAttribute("Id", Constants.FV_Element, root);
I just tested it and it works fine for me on FM2019.
Russ
Copy link to clipboard
Copied
Hi Russ,
doh! 🙂 I've considered "UndefinedAttrName" literally.
Thanks! It worked for me as well.
Are you aware of other 'hidden' functions which I could use to
1- query whether an element has an undefined attribute (suppose you have a large document with possibly many different invalid attributes which you want to delete)
2- get the value of the undefined attribute ( suppose you want to get this value and assign it to a valid attribute)
BTW, which ESTK version do you use? I'm on Adobe Extendscript Toolkit CC 4.0.0.1, ExtendScript 4.5.5, ScriptUI 6.2.2 which came with FM 2017. My autocomplete doesn't show me DeleteUndefinedAttribute.
Copy link to clipboard
Copied
I don't know of any single-command method to check for invalid attributes. The best I can think of is something like this, which might be a bit expensive for a long doc. It returns any invalid attribute names. You would have to run it on every element, walking through the tree with NextElementDFS, etc.
function getInvalidAttrs(elem)
{
var attrs = elem.Attributes;
var attrDefs = elem.ElementDef.AttributeDefs;
var invalidAttrs = new Array(0);
for(i = 0; i < attrs.length; i++)
{
var valid = false;
for(j = 0; j < attrDefs.length; j++)
{
if(attrs[i].name == attrDefs[j].name)
valid = true;
}
if(!valid) invalidAttrs.push(attrs[i].name);
}
return invalidAttrs;
}
You can retrieve the value of an invalid attribute just like any other attribute. Here is a function I use, where you should send either the attribute name or its index within the internal attributes array.
function elem_GetAttributeVal(elem, attrName, attrIndex)
{
var returnVal = "";
if(!ov(elem)) return returnVal;
if(attrName == null) attrName = "";
if(attrIndex == null) attrIndex = -1;
var attrs = elem.Attributes;
for(var i = 0 ; i < attrs.length; i++)
{
if (attrs[i].name == attrName ||
i == attrIndex)
{
if(attrs[i].values.length > 0)
returnVal = attrs[i].values[0];
i = attrs.length;
}
}
return returnVal;
}
I'm using toolkit 3.5.0.52 / ExtendScript 4.1.23 / ScriptUI 5.1.37. But this isn't what I was using when I wrote the original post. Nowadays, my autocomplete doesn't really work at all. I never really know what makes it work or not.
Hope this helps.
Russ
Copy link to clipboard
Copied
Russ,
I see. I was mixing some concepts (Attributes, AttributesDef) using the GetAttributes function vs accessing the Attributes directly like you did in both functions above.
Again, Thanks!!
PS: Regarding the autocomplete, it's helpful to know that your experience is the same.
Copy link to clipboard
Copied
I have reported a documentation bug as the doc.DeleteUndefinedAttribute has a Syntax heading but no detail in the scripting guide for 2015.
Copy link to clipboard
Copied
Yes, just to clarify, I did not find this in the manual. The ESTK autocomplete feature showed it as an available method.