I have a series of Illustrator files placed on different layers in an Indesign document.

I would like to set the visibility of the layers contained in the Illustrator files basing on the name of the Indesign layers to which they belong (Eg.: I want to activate only the "ES", "quote", and "disegno" layers in my Illustrator file if this one is on the "ES" layer of my Indesign file).

Here is my first attempt (which gives me an error):

var myDoc = app.activeDocument;var totRectangles = myDoc.rectangles;

for(i=0; i<totRectangles.length; i++){ var myRectangle = totRectangles[i]; var myRectangleLevel = myRectangle.itemLayer.name; // the name of the Indesign's layer to which the rectangle belongs

// execution error: Adobe InDesign CS5 got an error: The property is not applicable in the current state.

This is because it seems InDesign invalidates the reference to the current image and the list of all other images, as soon as you change a layer's visiblity state. You can work around it by not using intermediate variables ...

Hi Jongware, thanks for your support. I tried your script, but it seems that it turns on and off the same image layers on all Indesign layers: what I would like to do instead, is to turn on / off specific image layers depending on the Indesign layer to which they belong (Eg: turn on image layer "ES" on Indesign layer "A"; turn on image layer "EN" on Indesign layer "B"; and so on). I don't know if I was able to let you understand my aim..

I had a similar issue and it totally puzzled me. What I found is that when you change the currentVisibility of a graphicLayer, its specifier becomes instantly invalid! As a matter of fact, it seems that the Graphic container (e.g. myPDF) gets a new id so the whole chain is disabled: myPDF, myPDF.graphicLayerOptions, etc.

I have not AI installed at home, so I can't test my theory with actual files. Anyway, I believe this is a very special case where we need to use unresolved specifiers in order to recreate a valid access to the graphic object each time the state of a graphic layer is changed within. In other words, I recommend you do not use resolved arrays (myRectangle.allGraphics). Instead, try to re-access the element by its index from the collection myRectangle.graphics. Using indices rather than ids should work [remainder: a resolved specifier is based on ids.]

Of course my code should be refined if you have nested graphics (?) and/or nested graphic layers (?), but in basic situations this may be a good starting point. So, try something like this:

I wanted to make a blog post a few months ago as I was facing that exact same issue for a AI/ID script workflow. Once I changed layer visibility, the reference vanished. I had to redefine my variable every time in the loop to get it working.