2 Replies Latest reply on May 22, 2016 6:32 PM by pedropsb

    Problem creating sublayer ("not a function")

    pedropsb

      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[i].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[i];
        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){
        }
        }
        
      
        • 1. Re: Problem creating sublayer ("not a function")
          Alexander Ladygin Level 2

          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 );
          
          • 2. Re: Problem creating sublayer ("not a function")
            pedropsb Level 1

            Well... Little ashamed for this. Thank you