Skip to main content
bagonterman
Inspiring
February 6, 2012
Question

A better way to write this.

  • February 6, 2012
  • 1 reply
  • 1814 views

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.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.name;

                var density = myDocument.inks.neutralDensity;

                if(name== "203 Mandarin"){

                density=myDocument.inks.neutralDensity=0.0341;

                }

                if(name=="201 Orange"){

                density=myDocument.inks.neutralDensity=0.0699;

                }

                if(name=="601 Purple"){

                density=myDocument.inks.neutralDensity=0.1076;

                }

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

                density=myDocument.inks.neutralDensity=0.1473;

                }

                if(name=="302 Yellow"){

                density=myDocument.inks.neutralDensity=0.1892;

                }

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

                density=myDocument.inks.neutralDensity=0.2337;

                }

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

                density=myDocument.inks.neutralDensity=0.281;

                }

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

                density=myDocument.inks.neutralDensity=0.3315;

                }

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

                density=myDocument.inks.neutralDensity=0.3858;

                }

                if(name=="102 Maroon"){

                density=myDocument.inks.neutralDensity=0.4444;

                }

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

                density=myDocument.inks.neutralDensity=0.508;

                }

                if(name=="101 Burgundy"){

                density=myDocument.inks.neutralDensity=0.5776;

                }

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

                density=myDocument.inks.neutralDensity=0.6545;

                }

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

                density=myDocument.inks.neutralDensity=0.7403;

                }

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

                density=myDocument.inks.neutralDensity=0.8375;

                }

                if(name=="712 Bronze"){

                density=myDocument.inks.neutralDensity=0.9493;

                }

                if(name=="704 Silver"){

                density=myDocument.inks.neutralDensity=1.0813

                }

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

                density=myDocument.inks.neutralDensity=1.242;

                }

           

           }

    }

This topic has been closed for replies.

1 reply

Inspiring
February 6, 2012

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.name).isValid) {

          doc.inks.item(densities.name).neutralDensity = densities.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.name in densities) {

          doc.inks.neutralDensity = densities[doc.inks.name];

     }

}

Jeff

Jongware
Community Expert
Community Expert
February 6, 2012

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.)