6 Replies Latest reply on Sep 9, 2013 9:05 AM by ratsurfer1

    How can I batch turn on/turn off layers in a large group of files

    ratsurfer1

      I lifted this code from another discussion that purports to say it works, but it doesn't.

       

      I keep getting the Error: Object does not support the property or method 'length'

       

      I've looked at other batch process scripts that that line of code seems to be correct.

       

      I know this is a very simple script, and that the turn on/turn off layer part of the script works perfectly fine (on individuall pages), but for some reason I can't get it to batch process.

       

      I have a multi-version set of files with layers such as "de" for germany, "us" for United States, etc. that I need to batch turn on so that I can print.

       

      Here is what I have:

       

      var myFolder = Folder.selectDialog("Select Indesign Folder");

      var myIndsnFiles = myFolder.getFiles("*.indd");

       

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

      {

         var myIndsnFiles = app.open(myIndsnFiles[k]);

         

      var myIndsnFiles = app.documents[0];

       

      var answersLayer = myIndsnFiles.layers.item("us");

      answersLayer.visible = false;

       

          app.activeDocument.close(SaveOptions.yes);

       

          }

       

      Can you fix/explain what I'm doing wrong?

        • 2. Re: How can I batch turn on/turn off layers in a large group of files
          RobertKyle Level 2

          Without testing anything, I'd guess that it's not a good idea to keep reusing the variable name "myIndsnFiles." First it's an array of files in a folder, then it becomes a reference to a document. So on the second pass through your loop, javascript is trying to get the length of the document instead of the array.

          • 3. Re: How can I batch turn on/turn off layers in a large group of files
            Sergey Anosov Level 1

            Hi!

            Try this version, I hope it will do:

             

            var myFolder = Folder.selectDialog("Select Indesign Folder");

            if( myFolder != null )

            {

                var myIndsnFiles = myFolder.getFiles("*.indd");

                for( var k = myIndsnFiles.length -1; k >= 0; k--)

                {

                    try

                    {

                        var myIndsnDoc = app.open(myIndsnFiles[k]);

                        var answersLayer = myIndsnDoc.layers.item("us");

                        answersLayer.visible = false;

                        myIndsnDoc.close(SaveOptions.yes);

                    } catch ( error ) {};// try-catch

                };// for

            };// if

            1 person found this helpful
            • 4. Re: How can I batch turn on/turn off layers in a large group of files
              Jump_Over Level 5

              Hi,

               

              If we talk about mistakes:

               

              ...
              for(k=0; k<myIndsnFiles.length; k++)
              {
              var myIndsnFiles = app.open(myIndsnFiles[k]); 
              var myIndsnFiles = app.documents[0];
              ...
              

              This var - myIndsnFiles is assigned 3 times but your code is iterating through 1st version, so fails in 2nd step. Use different var names or Sergey's code.

               

              Jarek

              • 5. Re: How can I batch turn on/turn off layers in a large group of files
                Laubender Adobe Community Professional & MVP

                @ratsurfer1 – you are missing one crucial point from the other discussion you are mentioning:

                 

                The first thing you have to do is setting the user interaction level of the app temporarily to "never interact".

                 

                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
                
                //Then insert your lines of code here…
                
                //After running the script restore it back:
                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                

                 

                And it would be best to improve the catch(e){}-scenario with an error message.
                Something like that (or a more elaborate one):

                 

                catch(e){alert(e.message)};
                

                 

                Uwe

                • 6. Re: How can I batch turn on/turn off layers in a large group of files
                  ratsurfer1 Level 1

                  I got Sergey's Version to work. See below. It's very different than my original. I understood why my original wasn't working, but couldn't figure out how to fix it. I tried inserting Laubender's code into this, but I got the same error as above "Object does not support the property or method 'length'".

                  I made some adjustments so that the script turns off all layers that should be off, then turns on the layer I need.

                  Everything runs perfect except I wonder if there's a way I can turn off any warnings, such as missing links, so that it processes the whole batch without pausing. Maybe that's what Laubender was referring to.

                   

                   

                   

                   

                   

                   

                   

                  var myFolder = Folder.selectDialog("Select Indesign Folder");

                   

                  if( myFolder != null )

                   

                  {

                   

                      var myIndsnFiles = myFolder.getFiles("*.indd");

                   

                      for( var k = myIndsnFiles.length -1; k >= 0; k--)

                   

                      {

                   

                          try

                   

                          {

                   

                              var myDoc = app.open(myIndsnFiles[k]);

                             

                                         

                             

                  var inLayer = myDoc.layers.item("in");

                  inLayer.visible = false;

                  var itLayer = myDoc.layers.item("it");

                  itLayer.visible = false;

                  var deLayer = myDoc.layers.item("de");

                  deLayer.visible = false;

                  var ukLayer = myDoc.layers.item("uk");

                  ukLayer.visible = false;

                  var usLayer = myDoc.layers.item("us");

                  usLayer.visible = false;

                  var intLayer = myDoc.layers.item("INT");

                  intLayer.visible = false;

                   

                  deLayer.visible = true;

                             

                              myDoc.close(SaveOptions.yes);

                   

                          } catch ( error ) {};// try-catch

                   

                      };// for

                   

                  };// if