Skip to main content
Participant
May 22, 2016
Answered

Problem creating sublayer ("not a function")

  • May 22, 2016
  • 1 reply
  • 568 views

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){

  }

  }

 

This topic has been closed for replies.
Correct answer Alexander Ladygin

In the first case, you call "createLayer (subLayers, key);" - Where subLayers = object [Layers], in the second case where you call "createLayer (sLayers, keyValue);" - Where sLayers = object [Layer] and layer property "add ()" is not available.

I.e in the first case, you call "createLayer (doc.layers.getByName (" RES "). Layers, key)", in the second case, you call "createLayer ( doc.layers.getByName (" RES "). Layers.getByName (key), keyValue) "- but in this case you forgot to add ".layers " - "createLayer(

doc.layers.getByName ( "RES") layers.getByName (key) .layers)"..

//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.layers, keyValue );

1 reply

Alexander Ladygin
Alexander LadyginCorrect answer
Inspiring
May 22, 2016

In the first case, you call "createLayer (subLayers, key);" - Where subLayers = object [Layers], in the second case where you call "createLayer (sLayers, keyValue);" - Where sLayers = object [Layer] and layer property "add ()" is not available.

I.e in the first case, you call "createLayer (doc.layers.getByName (" RES "). Layers, key)", in the second case, you call "createLayer ( doc.layers.getByName (" RES "). Layers.getByName (key), keyValue) "- but in this case you forgot to add ".layers " - "createLayer(

doc.layers.getByName ( "RES") layers.getByName (key) .layers)"..

//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.layers, keyValue );

pedropsbAuthor
Participant
May 23, 2016

Well... Little ashamed for this. Thank you