3 Replies Latest reply on Nov 7, 2012 5:37 PM by booyajamo

    [JS][CS5.5] - Graphic layers loop error "object no longer exists"

    booyajamo Level 1

      I'm trying to loop through graphic layers of a specific graphic and set the current visibility based on the name of the layer.

       

      Currently the script is erroring on the 2nd time though the loop.

       

      I run the code and only the first graphic layer turns off then I get an error, "the requested action could not be completed because the object no longer exists". If I run it again the 2nd graphic layer turns off and the 3rd time through the loop errors with the same message, etc. etc.

       

      I've included all the code for reference but feel free to concentrate on the graphic layer loop (j index).

       

      Any ideas on what is happening? Thanks!

       

              var doc = app.activeDocument;
              var textFind = RegExp (/(text)|(type)|(title)|(legend)/i);
      
              //count layers
              var layerCount = doc.layers.length;
              
              //loop through layers
              for (i=0; i < layerCount; i++) {
              
                  //set current layer
                  var currentLayer = doc.layers[i];
                  var clName = currentLayer.name;
                  
                  //count layer graphics
                  var clGraphicCount = currentLayer.allGraphics.length;
                  
                  //check layer name & # of graphics
                  if (clName.match (/^(temp).+/) && clGraphicCount == 2) {
                      
                      //set  raster instance
                      var rasterInst = currentLayer.allGraphics[0];
                      
                      //count raster instance graphic layers
                      var rasterInstGraphicLayerCount = rasterInst.graphicLayerOptions.graphicLayers.length;
                      
                      //check raster instace graphic layers
                      if (rasterInstGraphicLayerCount > 1) {
                          
                          //loop through graphic layers
                          for (j=0; j < rasterInstGraphicLayerCount; j++) {
                              
                              //set current graphic layer
                              var riGLlayer = rasterInst.graphicLayerOptions.graphicLayers[j];
                              var riGLname = riGLlayer.name;
                              var riGLvis = riGLlayer.currentVisibility;
                              
                              //check visibility for non-text
                              if (riGLname.match (textFind)) {
                                  
                                  //turn off text graphic layers
                                  riGLlayer.currentVisibility = false;
                                  
                                  };
                              
                              };
                          
                          };
                      
                      };
                  
                  };