Problem creating sublayer ("not a function")
Hello. I came up with an ideia of a script to read a set of data and use it to manipulate objects inside AI. The data is a simple table with several key:values, one which is the identifier of a "row set". I stored these values in an Array of Objects, where each Object is a row.
First I wanted to create a layer strucuture, where 1) each key of these data Objects would create a sub layer inside a main layer. 2) each value of a key would create a sub layer inside the previous created key sub layer. For now I am bypassing the problem of duplication, because the object will share several keys and values, and I don't want duplicates of those.
At the moment I can't complete the task (2) coz of the error: "parent.add(); is not a function". Can't find out why, but I must declare that my programming skills are low. Hope you can help me out on this ![]()
var doc = app.activeDocument;
var layerPrincipal=doc.layers.getByName("RES");
var subLayers=layerPrincipal.layers;
//loteObj's are omitted for brevity.
var lotesArrCom=[loteObj1,loteObj2];
//Snipped that from the forum.
function doesLayerExist(layers, name) {
for (i=0; i<layers.length; i++) {
if (layers.name==name) return true;
}
return false;
}
//Snipped that from the forum.
function createLayer(parent,nome){
var lay = parent.add();
lay.name = nome;
}
//Loop trough an Array of Objects storing pairs of data.
for(var i=0;i<lotesArrRes.length;i++){
var loteObj=lotesArrRes;
for (var key in loteObj) {
if (loteObj.hasOwnProperty(key)&&key!="nome") {
var keyValue=loteObj[key];
if(!doesLayerExist(subLayers, key)){
createLayer(subLayers,key){
var keyValue=loteObj[key];
var sLayers=subLayers.getByName(key);
//It seems to stuck here. The previous function is executed as expected, but the following triggers the error in the 'CreateLayer' function declaration that "parent.add(); is not a function".
createLayer(sLayers,keyValue){
}
}