Skip to main content
Inspiring
November 7, 2012
Answered

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

  • November 7, 2012
  • 2 replies
  • 1266 views

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;

            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;

                        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;

                           

                            };

                       

                        };

                   

                    };

               

                };

           

            };

This topic has been closed for replies.
Correct answer Jongware

Changing a layer invalidates the entire graphics set, so you cannot rely on the value of any of your (cached!) variables.

See http://indesignsecrets.com/forum/indesign-add-ons-scripts-scripting-and-plug-ins/object-layer-options-scripting for more information.

2 replies

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
November 7, 2012

Changing a layer invalidates the entire graphics set, so you cannot rely on the value of any of your (cached!) variables.

See http://indesignsecrets.com/forum/indesign-add-ons-scripts-scripting-and-plug-ins/object-layer-options-scripting for more information.

booyajamoAuthor
Inspiring
November 8, 2012

Thanks Jongware!

I can see that the code is working when integrated into my script, but I don't understand why it's working. Any explanation you can provide is greatly appreciated, or can you point me in the right direction to read up? (I'm a newbie).

I guess I was using cached variables?

The solution you provided, does it use a specific programming concept I can research more?

Thanks again!

Jump_Over
Legend
November 7, 2012

Hi,

Is it possible that some of yours

clName.match (/^(temp).+/)

graphics aren't .psd or .pdf type?