Simple trick for this
var repeat = true
function myPluginCommand(selection, documentRoot) {
if (selection.hasArtboards) {
var rootnodes = selection.insertionParent.parent;
var nodes = selection.items[0];
//console.log(rootnodes);
rootnodes.children.forEach(element => {
//This is ArtBoard
let artboardname = element.name;
selection.items = [element];
let rect = new Rectangle();
rect.height = 10;
rect.width = 10;
rect.fill = new Color("red");
selection.insertionParent.addChild(rect);
var children = element.children;
console.log(children.length);
children.forEach(element => {
//This is an Artboard child
console.log(element);
selection.items = [element];
let height = element.globalDrawBounds.height;
let width = element.globalDrawBounds.width;
let x = element.globalDrawBounds.x;
let y = element.globalDrawBounds.y;
console.log("height : " + height + "width : " + width);
console.log("x : " + x + "y : " + y);
let rect = new Rectangle();
rect.height = height / 10;
rect.width = width / 10;
rect.fill = new Color("red");
selection.insertionParent.addChild(rect);
let xParent = rect.parent.globalDrawBounds.x;
let yParent = rect.parent.globalDrawBounds.y;
console.log("rect parent : " + rect.parent);
rect.moveInParentCoordinates(x - xParent, y - yParent);
})
});
} else if (!selection.hasArtboards && repeat) {
repeat = false;
console.log("Selected Otherthan Artboards");
//console.log(documentRoot.children);
console.log(documentRoot.children.forEach(element => {
console.log(element);
selection.items = [element]
myPluginCommand(element, 0);
})
);
}
}