6 Replies Latest reply on Sep 27, 2012 5:47 AM by wimkees

    Object Layers

    wimkees Level 1

      Hello,

       

      Is it possible to script the visibility of Object Layers (linked AI-files).

      It would be handy if a script will take care of switching the visibility of Object Layers for more than hundred linked AI-files in a ID document.

        • 1. Re: Object Layers
          csm_phil Level 4

          Hi Wimkees,

           

          Please try the below js code.

           

           

          var myDoc = app.activeDocument;
          var myGraphics = myDoc.links;
          for (var i = myGraphics.length-1; i >= 0; i--){
              var myArt = myGraphics[i]; 
              var myStry = myArt.parent;
              app.select(myStry);
              var mySel = app.selection[0];
              try{
                  var myGraphs  = mySel.parent.graphics[0].graphicLayerOptions.graphicLayers;
                  for (a=myGraphs.length-1; a>=0; a--){
                      if(myGraphs[a].name =='sample'){//here you can as the layer name you want switch off the visibility.
                          myGraphs[a].currentVisibility=false;
                          }
                      }
                  }catch(e){}
                  app.selection = null;
                  }
              
          

           

           

          thx

          csm_phil

          • 2. Re: Object Layers
            wimkees Level 1

            The script does now half the job... All the NL Layers are set to invisible.

            But: how do I make the English (EN) Layers visible.

            I edited it (see the italics txt) but that doesn't work :-(

             

            var myDoc = app.activeDocument;

            var myGraphics = myDoc.links;

            for (var i = myGraphics.length-1; i >= 0; i--){

                var myArt = myGraphics[i];

                var myStry = myArt.parent;

                app.select(myStry);

                var mySel = app.selection[0];

                try{

                    var myGraphs  = mySel.parent.graphics[0].graphicLayerOptions.graphicLayers;

                    for (a=myGraphs.length-1; a>=0; a--){

                        if(myGraphs[a].name =='NL'){//here you can as the layer name you want switch off the visibility.

                            myGraphs[a].currentVisibility=false;

                            }

                        if(myGraphs[a].name =='EN'){//here you can as the layer name you want switch on the visibility.

                            myGraphs[a].currentVisibility=true;

                            }

                        }

                    }catch(e){}

                    app.selection = null;

                    }

            • 3. Re: Object Layers
              csm_phil Level 4

              Hello wimkee,

               

              wimkees wrote:

               

              The script does now half the job... All the NL Layers are set to invisible.

              But: how do I make the English (EN) Layers visible.

              I edited it (see the italics txt) but that doesn't work :-(

               

              var myDoc = app.activeDocument;

              var myGraphics = myDoc.links;

              for (var i = myGraphics.length-1; i >= 0; i--){

                  var myArt = myGraphics[i];

                  var myStry = myArt.parent;

                  app.select(myStry);

                  var mySel = app.selection[0];

                  try{

                      var myGraphs  = mySel.parent.graphics[0].graphicLayerOptions.graphicLayers;

                      for (a=myGraphs.length-1; a>=0; a--){

                          if(myGraphs[a].name =='NL'){//here you can as the layer name you want switch off the visibility.

                              myGraphs[a].currentVisibility=false;

                              }

                         else if(myGraphs[a].name =='EN'){//here you can as the layer name you want switch on the visibility.

                              myGraphs[a].currentVisibility=true;

                              }

                          }

                      }catch(e){}

                      app.selection = null;

                      }

               

              Do you have debug the above script. Just I add the else condition thats all. If you need help in forum you always post your js code what you have tried. and what you get. so that is easy to help any one don't expect full code. This will not improve your future development growth.

               

              thx,

              csm_phil

              • 4. Re: Object Layers
                wimkees Level 1

                Thx for your help.

                Script still doesn't work but I will look for another solution.

                • 5. Re: Object Layers
                  [Jongware] Most Valuable Participant

                  Changing a layer somehow changes the object itself, and may invalidate you original array. See this for a working example:

                  http://indesignsecrets.com/forum/indesign-add-ons-scripts-scripting-and-plug-ins/object-la yer-options-scripting

                  • 6. Re: Object Layers
                    wimkees Level 1

                    That did it!

                     

                    I adjusted the script to my own layerset and chached the filetype to AI (which is PDF).

                     

                    Main();

                     

                    function Main()

                    {

                    var page, placedFile, links, layer, i, j, k,

                    doc = app.activeDocument,

                    pages = doc.pages;

                    var checklist = [];

                     

                    var layernames = [ "GE", "EN", "NL", "Layer 1" ];

                     

                    var ilDialog = app.dialogs.add({name:"Image Layers on/off", canCancel:true});

                    with (ilDialog)

                    {

                    with(dialogColumns.add())

                    {

                    with(dialogRows.add())

                    {

                    staticTexts.add ({staticLabel:"Check to show, uncheck to hide"});

                    for (i=0; i<layernames.length; i++)

                    {

                    with(dialogRows.add())

                    checklist.push (checkboxControls.add({staticLabel:layernames[i], checkedState:false}));

                    }

                    }

                    }

                    }

                    var numChanges = 0;

                    var imageChange;

                    if (ilDialog.show() == true)

                    {

                    links = doc.links;

                    for (i = links.length-1; i>=0; i--)

                    {

                    placedFile = links[i].parent;

                    // show the internal type of this image:

                    //alert (placedFile.constructor.name+" / "+placedFile.itemLink.linkType);

                    if (placedFile.constructor.name == "PDF" && placedFile.itemLink != null && placedFile.itemLink.linkType == "Adobe Portable Document Format (PDF)")

                    {

                    imageChange = false;

                    for (k=0; k<layernames.length; k++)

                    {

                    layer = links[i].parent.graphicLayerOptions.graphicLayers.item(layernames[k]);

                    if (layer.isValid && layer.currentVisibility != checklist[k].checkedState)

                    {

                    layer.currentVisibility = checklist[k].checkedState;

                    imageChange = true;

                    }

                    }

                    if (imageChange)

                    numChanges++;

                    }

                    }

                    alert("Done, made changes in "+numChanges+" images");

                    } else

                    {

                    ilDialog.destroy();

                    }

                    }

                     

                    Thx for your help!