8 Replies Latest reply on Jan 8, 2013 1:30 AM by Muppet Mark

    Turn on layer visibility without exact name

    C-Dubs-SF

      Hi all,

       

      This is my first post, and my first attempt to edit javascript for AI, so please bear with me! I am realtively good with actionscript, so I am trying my hand at this to streamline our image exporting process. I am using CS5 on a PC.

       

      We build our illustrator files to be a combination of different layers. Sometimes just one layer is exported at a time, and sometimes layers are combined to form a more complex image. We use layer names with instructions of what layers should be on and off.

       

      An example of the layer naming might look like this:

       

      03 (01-ON, 02-OFF)

      02 (01-OFF)

      01

       

      I have been able to figure out how to check if there is a note to turn a layer on, my question is how do I select the layer to turn on the visibility without using the exact name of the layer?

       

      In my example, when layer 3 gets exported, it should have layer "01" visible and layer "02 (01-OFF)" not visible. Since our files are always constructed differently (meaning 02 might not always have a note about 01 being off), I can't use the getByName option because I don't have an exact name to call the layer and turn the visibility on. The layers always start with 01, 02, 03, etc. Is there a way to use getByName and only have it look at the first 2 characters of the layer name?

       

      Thanks in advance for any help!

        • 1. Re: Turn on layer visibility without exact name
          CarlosCanto Adobe Community Professional & MVP

          use the index property, in your example, if you need to turn layer 2 off use,

           

          activeDocument.layers[1].visible = false;
          

           

          zero based indexes for layers, top most being zero.

          • 2. Re: Turn on layer visibility without exact name
            C-Dubs-SF Level 1

            Thanks for the quick response! Our files aren't always just 3 layers, how would I scale something like that? Say the next file had 7 layers, then layer 2 would be an index value of 5 right? Could I use some variation of finding the length of layers and then some equation to subtract to get the proper index value?

            • 3. Re: Turn on layer visibility without exact name
              C-Dubs-SF Level 1

              If it helps - this is what I have attempted. It doesn't work at all when I call the script, so obviously something about it doesn't work, but maybe I am close? lyr_name is a variable for layer.name. The original script that I am trying to edit would export each layer on it's own, so my goal is to get it to turn noted layers on to export along with that layer. Does that make sense?

               

              if (lyr_name.match(/01ON/gi)){

                                      for (f=0; f<docRef.layers.length; f++) {

                                          if (layers[f].name.match(/01/gi)) {

                                          docRef.layers[f].visible = true;

                                          }

                                      }

                               }

               

              This is running within a much larger for loop, so maybe that isn't allowed? The larger loop basically turns all the other layers off except for one and exports it, so I guess I don't need to worry about turning layers off, just on.

              • 4. Re: Turn on layer visibility without exact name
                Muppet Mark Level 5

                In your snippet you don't need the outer if statement… You check and check again the same condition… The loop in here will do nothing…? Do you need each layer + base layer as a separate file…? You may need to explain a little more…

                • 5. Re: Turn on layer visibility without exact name
                  C-Dubs-SF Level 1

                  Basically if a layer notes that an additional layer should be turned on, I can't figure out a scalable way of turning on a layer bc the names can sometimes be different. Sometimes layer 2 will be named "02" and other graphics it may be named "02 (01ON)". So assuming in the loop, the active layer is named "03 (02 ON)" I want it to check that there is a note to turn 02 on. But if the layer is named anything other than "02" meaning I couldn't have the note (01ON) it won't work. Below is the code I was using to test turning on the layer when it was labeled simply as "02" if I change it "02 (01ON)" it doesn't work. Does that help clarify?

                   

                  if (lyr_name.match(/02ON/gi)){
                                  docRef.layers.getByName('02').visible = true;
                                  }
                  

                   

                  So the my thinking was the if would check if the layer name included a match to the text "02ON" it would go through and check the layer names for a layer named "02" and make it visible.

                  • 6. Re: Turn on layer visibility without exact name
                    Muppet Mark Level 5

                    Looking at the way you have set your files up you don't need to know any names while it is possible to do that I think you are just making more work for yourself… All you actually need do is get a ON/OFF list from each layer name as this is ordered…

                     

                    Edit OK I had a quick go to see if this worked… this appears to what I think you are wanting…

                     

                    #target illustrator
                    
                    exportLayerSets();
                    
                    function exportLayerSets() {
                    
                              var doc, lay, visList, i, j;
                    
                              doc = app.activeDocument;
                    
                              for ( i = 0; i < doc.layers.length; i++ ) {
                    
                                        lay = doc.layers[i];
                    
                                        lay.visible = true;
                    
                                        visList = lay.name.match( /ON|OFF/gi );
                    
                                        if ( visList != null ) {
                    
                                                  for ( j = 0; j < visList.length; j++ ) {
                    
                                                            if ( visList[j] == 'ON' ) {
                    
                                                                      doc.layers[ i + j + 1 ].visible = true; 
                    
                                                            } else {
                    
                                                                      doc.layers[ i + j + 1 ].visible = false;
                    
                                                            };
                    
                                                  };
                    
                                        };
                    
                                        app.redraw();
                    
                                        alert( 'Is this working?' );
                    
                                        // EXPORT HERE…
                    
                                        lay.visible = false;
                    
                              };
                    
                    };
                    
                    
                    1 person found this helpful
                    • 7. Re: Turn on layer visibility without exact name
                      C-Dubs-SF Level 1

                      Mark that is awesome! I ended up messing with it over the weekend and came up with something pretty similar. Thanks for taking the time! I have a follow-up question for you, if you have time. Because the export script uses the Save for web settings, the output is 72dpi, while I need an output of 150dpi. The script I have in place can export with a larger vertical/horizontal scale, but the end result is a 20" wide, 72dpi image, so I need to shrink it down to 10" wide.

                       

                      I saw some other posts of yours talking about using BridgeTalk, but they were a bit over my head. I would love once the image is exported to open in Photoshop and resize and then continue onto the next image to export. I was able to use photoshop.open to get the jpg to open in Photoshop and then everything stopped. I assume that if there was a BridgeTalk connection I could send a script to PS saying to resize the image, save and close, and then return to Illustrator to export the next image. Is that a possibility?

                       

                      It would also be fine to export all the images to Photoshop first and then fire the resize script on all open files. Might be easier than telling it to bounce back and forth?

                      • 8. Re: Turn on layer visibility without exact name
                        Muppet Mark Level 5

                        I would batch all the exports then batch all the resizes… As a work flow I think this would be quicker. Bridgetalk if not the easiest of things to get your head around and making it loop is harder still…