Once you can access an element's object, you can affect how it is displayed in the structure view.
By updating the walkTree() function with a single line of code, your script can collapse, as is the case here, or uncollapse elements in the tree.
function walkTree(elem, count) {
var child, name;
name = getElementName(elem);
elem.ElementIsCollapsed = true; // collapse element
Console(count + ": " + name);
child = elem.FirstChildElement; //get first child if any
while (child.ObjectValid()) {
count = count + 1; //upate element count
count = walkTree(child, count); //traverse subtree with child root
child = child.NextSiblingElement; //get the siblings
}
return count;
}
Just be sure to redisplay the document when you are done to make your changes visible to the user.
doc.Redisplay();
Before the script was run, the structure view appears as follows:
After the running the script, it appears as shown here:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbHpI8viortvfcZc1zRUc87oapVUt2GhAcmaWXlZ0lv-g2c8BiDOOiWfwY1WGSl76vs01ITjcGcXZkq2KmiYDNImJOvRxy3WJqk_x666ZeLz4FfSRvFB8tzGwEmDI5vJPO59MqFhSyZzE/s320/collapse.png)
Just to verify that all elements in the tree were collapsed, I manually uncollapsed the root element to reveal that the script works as expected.
By updating the walkTree() function with a single line of code, your script can collapse, as is the case here, or uncollapse elements in the tree.
function walkTree(elem, count) {
var child, name;
name = getElementName(elem);
elem.ElementIsCollapsed = true; // collapse element
Console(count + ": " + name);
child = elem.FirstChildElement; //get first child if any
while (child.ObjectValid()) {
count = count + 1; //upate element count
count = walkTree(child, count); //traverse subtree with child root
child = child.NextSiblingElement; //get the siblings
}
return count;
}
Just be sure to redisplay the document when you are done to make your changes visible to the user.
doc.Redisplay();
Before the script was run, the structure view appears as follows:
After the running the script, it appears as shown here:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbHpI8viortvfcZc1zRUc87oapVUt2GhAcmaWXlZ0lv-g2c8BiDOOiWfwY1WGSl76vs01ITjcGcXZkq2KmiYDNImJOvRxy3WJqk_x666ZeLz4FfSRvFB8tzGwEmDI5vJPO59MqFhSyZzE/s320/collapse.png)
Just to verify that all elements in the tree were collapsed, I manually uncollapsed the root element to reveal that the script works as expected.
No comments:
Post a Comment