4 Replies Latest reply on Feb 14, 2012 2:17 PM by absqua

    A better way to write this.

    Gonterman1201 Level 1

      My hope was to run through the list of inks and check each ink to see if it matched with a name in my array and then assign a value. I was attempts were unsuccessful. So I went with a bunch of if statements. I know I should be able to reduce this down and say if variable ink ==name in this array assign it this value from this array.

      When I make a second loop to get to the array name the first loop does not work.

      I just need a point in the right direction.

       

       

      myDocument=app.activeDocument;

      densityChange();

      function densityChange(){

       

      /*for (i=0; i<myDocument.swatches.length; i++){

                      name = myDocument.swatches[i].name;

                    //neutralDensity

          

           alert(name);

           }

      }

      */

      var ssDensityChng=(["203 Mandarin","201 Orange","601 Purple","202 Met Orange","302 Yellow","401 Bright Grn","301 Golden Yel","106 Bright Red","104 Cadmium Red","102 Maroon","103 Dark Red","101 Burgundy","501 Met Blue","602 Met Purple","404 Met Sage","712 Bronze","704 Silver","702Met Pewter"]);

      var myDensity=([0.0341,0.0699,0.1076,0.1473,0.1892,0.2337,0.281,0.3315,0.3858,0.4444,0.508,0. 5776,0.6545,0.7403,0.8375,0.9493,1.0813,1.242]);

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

                      var name = myDocument.inks[i].name;

                      var density = myDocument.inks[i].neutralDensity;

                      if(name== "203 Mandarin"){

                      density=myDocument.inks[i].neutralDensity=0.0341;

                      }

                      if(name=="201 Orange"){

                      density=myDocument.inks[i].neutralDensity=0.0699;

                      }

                      if(name=="601 Purple"){

                      density=myDocument.inks[i].neutralDensity=0.1076;

                      }

                      if(name=="202 Met Orange"){

                      density=myDocument.inks[i].neutralDensity=0.1473;

                      }

                      if(name=="302 Yellow"){

                      density=myDocument.inks[i].neutralDensity=0.1892;

                      }

                      if(name=="401 Bright Grn"){

                      density=myDocument.inks[i].neutralDensity=0.2337;

                      }

                      if(name=="301 Golden Yel"){

                      density=myDocument.inks[i].neutralDensity=0.281;

                      }

                      if(name=="106 Bright Red"){

                      density=myDocument.inks[i].neutralDensity=0.3315;

                      }

                      if(name=="104 Cadmium Red"){

                      density=myDocument.inks[i].neutralDensity=0.3858;

                      }

                      if(name=="102 Maroon"){

                      density=myDocument.inks[i].neutralDensity=0.4444;

                      }

                      if(name=="103 Dark Red"){

                      density=myDocument.inks[i].neutralDensity=0.508;

                      }

                      if(name=="101 Burgundy"){

                      density=myDocument.inks[i].neutralDensity=0.5776;

                      }

                      if(name=="501 Met Blue"){

                      density=myDocument.inks[i].neutralDensity=0.6545;

                      }

                      if(name=="602 Met Purple"){

                      density=myDocument.inks[i].neutralDensity=0.7403;

                      }

                      if(name=="404 Met Sage"){

                      density=myDocument.inks[i].neutralDensity=0.8375;

                      }

                      if(name=="712 Bronze"){

                      density=myDocument.inks[i].neutralDensity=0.9493;

                      }

                      if(name=="704 Silver"){

                      density=myDocument.inks[i].neutralDensity=1.0813

                      }

                      if(name=="702Met Pewter"){

                      density=myDocument.inks[i].neutralDensity=1.242;

                      }

                 

                 }

          }

        • 1. Re: A better way to write this.
          absqua Level 4

          I think I would set up my ink name–density pairs in an array of objects and loop through it instead of the document inks:

           

          var doc = app.activeDocument,
               densities = [
                    {name: "203 Mandarin", density: 0.0341},
                    {name: "201 Orange", density: 0.0699},
                    // ...
               ],
               i, l;
          
          for (i = 0, l = densities.length; i < l; i++) {
               if (doc.inks.item(densities[i].name).isValid) {
                    doc.inks.item(densities[i].name).neutralDensity = densities[i].density;
               }
          }
          

           

          EDIT: Or you could go back to looping through the document inks and set up the object you're checking against a little differently. This one is probably better:

           

          var doc = app.activeDocument,
               densities = {
                    "203 Mandarin": 0.0341,
                    "201 Orange": 0.0699,
                    // ...
               },
               i, l;
            
          for (i = 0, l = doc.inks.length; i < l; i++) {
               if (doc.inks[i].name in densities) {
                    doc.inks[i].neutralDensity = densities[doc.inks[i].name];
               }
          }
          

           

          Jeff

          1 person found this helpful
          • 2. Re: A better way to write this.
            [Jongware] Most Valuable Participant

            Oof!! I was going for a two-dimensional array but your associative array is much more impressive! (Gonterman, see for example http://www.quirksmode.org/js/associative.html on why this works -- http://www.melbpc.org.au/pcupdate/2408/2408article8.htm is also a pretty thorough read.)

            • 3. Re: A better way to write this.
              Gonterman1201 Level 1

              Thank you very much this was very helpful and looks alot cleaner. I am wondering what i, and 1 are defining? When I ran this I recieved an error saying they were undefined, Then I commented them out and it ran fine. I am also wondering how the name and density in the array are used. How is it able to differentiate between the two when doc.inks[i].name is used for both the name and then density?

              Thank you again for all the help.

              • 4. Re: A better way to write this.
                absqua Level 4

                Sorry I didn't provide much in the way of explanation with the code. The links Jongware provided should help you understand what's happening in the loop.

                 

                i and l are variable declarations. They aren't defined until the for statement. It's considered good style in certain precincts to only have one var declaration statement per function or program, which makes explicit how JavaScript will treat them—by hoisting them to the top, that is. This is to help you not make some incorrect but perfectly natural assumptions about the scope of your variables. Douglas Crockford and his JSLint are the driving forces behind this convention.

                 

                The l in my loop is probably confusing. I'm simply caching the length of the doc.inks collection so I don't have to check it on every go-round. This can make a surprising performance difference in large loops but is totally unnecessary here. (But that's the way I have it set up in Text Expander, so that's how it comes out!)

                 

                The if statement is checking whether the name of the current ink is one of the keys in the densities object/dictionary/hash/associative array. If it is, then the ink's neutralDensity is assigned the value of that key. So if the ink's name is "203 Mandarin", its neutralDensity will be assigned the value of densities["203 Mandarin"], which is 0.0341. I think I'm not explaining very well; probably stepping through it in the ESTK and looking at your values in the console will be more enlightening.

                 

                Jeff