2 Replies Latest reply on Mar 27, 2014 7:25 AM by Green4ever

    Bug?: layers.add() is not working properly when invoked from menu (ID/CS6/Win7)

    Green4ever Level 3

      Hello Devs,

       

      I am facing a mysterious issue with the layers.add() method.

       

      I am trying to create n number of layers for my document. So I have decided to create a menu for this.

       

      I have creatd two scripts

      1. For creating menu & menu action,

      2. Actual script that creates n number of layers.

       

      When I run the script #2 direclty from ESTK it works fine.

       

      Now when I try to invoke the same script from the Menu it is not working fine. Only the last layer is created.

       

      Script 1: LayerPopupMenuAction

       

       

      #target indesign
      #targetengine createLayerset
      var layerTemplateScript = File(File(getActiveScriptPath()).parent.fsName+"/IntializeTemplate.jsx"); 
      
      var initTemplateHandler = {
          'beforeDisplay' : function(ev)
              {
                  ev.target.enabled = (app.documents.length>0);
              },
          'onInvoke' : function()
              {
                  app.doScript(layerTemplateScript, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Initialize Script");
              }
          };
      
      var layerMenuAction = app.scriptMenuActions.add("&Create Layer Set");
      
      for(var init_ev in initTemplateHandler) {
          layerMenuAction.eventListeners.add(init_ev, initTemplateHandler[init_ev]);
          }
      
      var refMenuItem = app.menus.item("$ID/LayerPanelPopup");
      refMenuItem.menuItems.add(layerMenuAction, LocationOptions.AFTER, refMenuItem.menuItems.item(2));
      
      function getActiveScriptPath() {
          // This function returns the path to the active script, even when running from ESTK
          try { 
              return app.activeScript; 
          } catch(e) { 
              return e.fileName; 
          }
      }
      

       

      Script 2: IntializeTemplate

       

       

      #target indesign
      if (app.documents.length == 0){
          alert("Please open a document and try again", "Document Error", true);
          exit();
          }
      
      var templateDoc = app.activeDocument;
      
      var layerNameArray = ["Fixed_Static_Assets", "Absolute_User_Assets", "Relative_User_Assets"];
      CreateLayers(layerNameArray);
      
      alert("Initialization Successful");
      
      
      //--------------------------------FUNCTIONS ARE DEFINED HERE--------------------------------
      function CreateLayers(LayerNameList){
          var layerListLength = LayerNameList.length;
          var layerObject;
          for (var ln=0; ln<layerListLength; ln++){
              layerObject = templateDoc.layers.itemByName(LayerNameList[ln]);
              if (layerObject.isValid==false){
                  layerObject = templateDoc.layers.add({name:LayerNameList[ln]});
                  //$.writeln('Layer Creation: ' + layerObject.isValid + ' for '+layerObject.name);
                  }
              }
          //Merger Default Layer with the bottom layer:
          var defaultLayer = templateDoc.layers.itemByName("Layer 1");
          if(defaultLayer.isValid==true){
              //$.writeln('Default is Valid and will be merged with top most layer');
              layerObject.merge([defaultLayer]);
              }
          }
      

       

      Any help / guidance on this regard will be much helpful.

       

      @Marijan Tompa [tomaxxi]: I googled for the solution and noticed that you have already worked on similar type of script. Please help me if you can.

       

      Thanks

      Green4ever