11 Replies Latest reply on Jul 14, 2014 10:52 PM by AsOne

    Find multi layer names and alert

    AsOne

      Hi All,

       

      I am trying to do export pdf using condition layer names, if this layer names mismatched should alert, else export pdf.

      If only one layer it is working fine, when multi layers not working.

      my code is always alert if the layer names are correct, what is wrong in my code, can any one correct.

       

      help would be appreciate

       

      ly = app.activeDocument.layers.everyItem().getElements();

          for (i=0; i<ly.length; i++) {

              if (ly[i].name != "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables") {

      alert ("Check Layer names"), exit();

      }else {

      var myJobOptionName = "[Press Quality]";

      var myOutFolderPathName = "/Users/wleastudio/Desktop/Watched Folder/Out/";

      var myPDFFilePath;

      var InDJobOption = app.pdfExportPresets;

      myPDFFilePath = myOutFolderPathName+"/"+app.documents[0].name.split(".indd")[0].split(".INDD")[0]+".pdf";

      app.documents[0].exportFile (ExportFormat.pdfType, myPDFFilePath, false, myJobOptionName);

      }

      }

       

      Thanks in advance

      steve

        • 1. Re: Find multi layer names and alert
          Jump_Over Level 5

          Hi,

           

          Imagine it works, so

          if a particular layer name matches your condition - code is exporting a file

          if not ==> alert

           

          It means you are going to export as many PDF as many layers matches a condition. Why?

           

          But it does not work.

          Wrong condition. You can not compare a string to a "list" of strings ("b" to "a","b","c"). It can "work" only for first element. A solution could be to create a string from each layers names, like:

          goodNames = app.activeDocument.layers.everyItem().name.join();

           

          to find out if THIS name mismatches goodNames use a String.search() method, like:

          goodNames.search(currentName) == -1;

           

          any other value than -1 means a name matches some part of goodNames.

           

          So I am suggesting to change a conditioning procedure and reconsider an exporting goal (when to export and what)

           

          Jarek

          • 2. Re: Find multi layer names and alert
            AsOne Level 1

            Hi Jarek,

             

            Your 1st point of understand is correct, if layer name matches - code is exporting a file, if not => alert.

             

            The other one NOT export as many PDF as many layers matches, though if defied layer names match export only one PDF.

             

            Thank you Jarek for your help and suggestion.

            • 3. Re: Find multi layer names and alert
              Jump_Over Level 5

              Hi,

               

              layers: "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables"

               

              Regarding to your example - only only one layer from 4 listed can be present in a document?

              If two or more consecutive layers match - code exports two or more PDFs and overrides previous one (same file name).

               

              Jarek

              • 4. Re: Find multi layer names and alert
                AsOne Level 1

                Hi Jarek,

                 

                Not like that, if all layer names match then only - code will export PDF else alert

                 

                 

                Steve

                • 5. Re: Re: Find multi layer names and alert
                  Laubender Adobe Community Professional & MVP

                  @Steve – are there other creteria? Like stacking order of said layers? Or the presence or absence of other layers beneath the four ones? First I would validate the layers with isValid and second I would ask, if the layers are locked (optional), visible and printable. Doesn't make sense, if the layers are there, but not visible or not printable.

                   

                  Of course you can set all layers to printable before checking anything…

                   

                  app.documents[0].layers.everyItem().printable = true;
                  

                   

                  Depends on your situation, if that is a wise move or not. We simply do not know enough of your project giving advice here…

                   

                  Uwe

                  • 6. Re: Find multi layer names and alert
                    Jump_Over Level 5

                    Hi,

                     

                    Watch this line:

                    app.documents[0].exportFile (ExportFormat.pdfType, myPDFFilePath, false, myJobOptionName);

                     

                    It is inside a loop - as many times repeated as long is "ly" array.

                    Only break is when some name does not match. But if every one does...?

                     

                    Jarek

                    • 7. Re: Re: Find multi layer names and alert
                      AsOne Level 1

                      Hi Uwe,

                       

                      Thanks for your reply. There is no criteria of visible and printable layers, all are visible and printable layers.

                       

                      My project brief

                       

                      No. of defined layers = 4 ("BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables")

                      condition = If layer names matched to defined 4 layers, code should export pdf else alert. If one layer name also mismatched to defined 4 layers, code should alert. 

                      do = export PDF

                       

                       

                      Thanks

                      Steve

                      • 8. Re: Re: Re: Find multi layer names and alert
                        Chinnadk Level 4

                        Hi Steve,

                         

                        Try this.

                         

                        Array.prototype.contains = function(k) 
                        {
                          for(var i=0; i < this.length; i++)
                          {
                                if(this[i] === k)
                                {
                                    return true;
                                }
                            }
                          return false;
                        }
                        var _layers = [ "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables"];
                        var match = [];
                        var ly = app.activeDocument.layers.everyItem().getElements();
                        for (i=0; i<ly.length; i++)
                        {
                            if (_layers.contains(ly[i].name))
                            {
                                    match.push(i);
                                }
                        }
                        if(match.length == 4)
                        {
                                var myJobOptionName = "[Press Quality]";
                                var myOutFolderPathName = "/Users/wleastudio/Desktop/Watched Folder/Out/";
                                var myPDFFilePath;
                                var InDJobOption = app.pdfExportPresets;
                                myPDFFilePath = myOutFolderPathName+"/"+app.documents[0].name.split(".indd")[0].split(".INDD")[0]+".pdf";
                                app.documents[0].exportFile (ExportFormat.pdfType, myPDFFilePath, false, myJobOptionName);
                        }
                        else
                        {
                                alert ("Check Layer names");
                                exit();
                            }
                        

                         

                         

                        Regards,

                        Chinna

                        • 9. Re: Re: Re: Find multi layer names and alert
                          AsOne Level 1

                          Hi Chinna,

                           

                          Your code running super, catching names correct, but one minor error if layers more than 4 also code is exporting PDF.

                           

                           

                          Steve

                          • 10. Re: Re: Re: Re: Find multi layer names and alert
                            Chinnadk Level 4

                            Hi Steve,

                             

                            Try now.

                             

                            Array.prototype.contains = function(k) 
                            { 
                              for(var i=0; i < this.length; i++) 
                              { 
                                    if(this[i] === k) 
                                    { 
                                        return true; 
                                    } 
                                } 
                              return false; 
                            } 
                            var _layers = [ "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables"]; 
                            var match = []; 
                            var ly = app.activeDocument.layers.everyItem().getElements(); 
                            if(ly.length == 4){
                                for (i=0; i<ly.length; i++) 
                                { 
                                    if (_layers.contains(ly[i].name)) 
                                    { 
                                            match.push(i); 
                                        } 
                                }
                            }
                            else
                            {
                                    alert ("Layer count shouldn't be less or more than 4"); 
                                    exit(); 
                                }
                            if(match.length == 4) 
                            { 
                                    var myJobOptionName = "[Press Quality]"; 
                                    var myOutFolderPathName = "/Users/wleastudio/Desktop/Watched Folder/Out/"; 
                                    var myPDFFilePath; 
                                    var InDJobOption = app.pdfExportPresets; 
                                    myPDFFilePath = myOutFolderPathName+"/"+app.documents[0].name.split(".indd")[0].split(".INDD")[0]+".pdf"; 
                                    app.documents[0].exportFile (ExportFormat.pdfType, myPDFFilePath, false, myJobOptionName); 
                            } 
                            else 
                            { 
                                    alert ("Check Layer names"); 
                                    exit(); 
                                }
                            

                             

                            Regards,

                            Chinna

                            • 11. Re: Re: Re: Re: Find multi layer names and alert
                              AsOne Level 1

                              Hi Chinna,

                               

                              Super, Fantastic. This Works good. Thank you...

                               

                               

                              Steve