11 Replies Latest reply: Jun 1, 2012 8:42 AM by RobGreer RSS

    Crazy Scripting Help

    RobGreer Community Member

      I’ve created a group of 40 different icons (telephone, star, diamond, etc.) and I need to apply 150 colors to each one of those icons then save each icon individually as a PNG with a filename of <ICONNAME-COLORNAME.png>. Those 150 colors have already been defined with individual custom color names within a color group I created called “MyColors.”

       

      I’ve figured out the PNG export part of the javascript (and that script is available all over the place) but my scripting skills suck and I’m not sure what I need to do to get this done. I’ve read the Illustrator Scripting Guide (I got lost pretty quick) and I’ve done some searching for scripts but without any luck. I was thinking that the best way to approach this was to put each of the different icons (telephone, star, diamond, etc.) on a single layer, each within a different illustrator file and then run the script on each file to automatically generate the colors and layers I need within each file. I could then run a PNG export script against each file. Or is there a better way to create these PNGs?

       

      I know it’s a lot to ask and I hate to ask it, but I’ll lose my mind if I have to do this by hand. Can anyone help me with this scripting? I’d be happy to throw a few bucks your way (or donate them to the charity of your choice)!

       

      I'm on Illustrator CS5/CS6 ...

        • 1. Re: Crazy Scripting Help
          moluapple Community Member

          A sample document will help a lot to figure out what you want.

          How can you apply pure color to each one of those icons, your icons are single colored(Monochrome)?

          • 2. Re: Crazy Scripting Help
            Muppet Mark Community Member

            You should be able to do this any how multi-docs, mulit-layered doc or multi-artbaord doc… Its a scriptable exercise… More info?

            • 3. Re: Crazy Scripting Help
              RobGreer Community Member

              All of the icons are monochrome. Here is an image that may help explain what I need. The icon in this example (figure 1) is a star. The color group (figure 2) is called "Rob" and each color is named (e.g. gold, gray, red, etc.). I need a script that will take a basic monochrome icon and then create a new layer for every color in that color group with a naming convention of ICONNAME-COLORNAME (e.g. star-gold) and then apply the appropriate color to the icon on that layer. In the end, the final output should look something like the layers seen in figure 3. Is that helpful?

               

              example.jpg

              • 4. Re: Crazy Scripting Help
                Muppet Mark Community Member

                You example is very basic ( a single path item )… I don't see any need to create layers of the colors options you could just change the color contents of your items on the fly and output *.png all in a loop. How complex is the artwork in question all like this?

                • 5. Re: Crazy Scripting Help
                  RobGreer Community Member

                  Yes. The icons themselves are all this simple but there's another wrinkle that I didn't want to mention because I was afraid it would make this question more confusing.

                   

                  In addition to these simple icons I also need a color button version for each of them. For example, in addition to color icons as previously described, I also need a button in each of the ~150 colors with a white icon (star in this example) on top.

                   

                  This button is comprised of two objects but when grouped and a color is selected, it changes to the appropriate color needed. Anyway, my plan was to generate the color icons first and save them in a file. I was then going to generate the color buttons the same way (treating them like the icons). Then I was going to paste the icons onto the blank buttons and then move them into the same file as the icons and then generate the PNGs from that single file. I was going to use a naming convention of <ICONNAME-COLORNAME-btn.png> for the button files. Therefore, just doing an export straight to a file wouldn't be my ideal in this situation.

                   

                  example2.jpg

                  • 6. Re: Crazy Scripting Help
                    Muppet Mark Community Member

                    Im not sure how you set up your files but given this situation I would… Have 1 doc with all the colors 150 in swatches. 40 layers for icons above a base ( button layer ) then I would…

                     

                    Turn off every layer

                    turn on first layer and change color and export

                    then change it to white

                    turn on last layer and change color and export

                    rinse repeat til done…

                     

                    does that make sense? 12k pngs

                    • 7. Re: Crazy Scripting Help
                      RobGreer Community Member

                      Let me simplify my request.

                       

                      I need a script that will take an object on the first (and only layer) and then create x layers (with x being the total number of colors in the color group) with each layer named for the first layer name (e.g. star) plus the "-COLORNAME" (e.g. -red) to give me x layers with a layer naming convention of FIRSTLAYERNAME-COLORNAME (e.g. star-red).

                       

                      I can do the rest manually.

                      • 8. Re: Crazy Scripting Help
                        RobGreer Community Member

                        So I got this far but the layer objects aren't duplicating. Any ideas?

                         

                         

                         

                         

                         

                        var docRef = app.activeDocument;

                         

                         

                         

                         

                         

                        with (docRef) {

                         

                        var grp = swatchGroups.getByName('Rob');

                         

                        /*

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

                            if (swatchGroups[i].name == 'Rob') {

                                grp = swatchGroups[i];

                            }

                        }

                        */

                         

                        var grpList = grp.getAllSwatches();

                         

                        //var sw = grpList[55];

                         

                        var originalLayerLength = layers.length;

                         

                        for (var i = 0; i < originalLayerLength; i++) {

                         

                        var oldLayer = layers[i];

                         

                        oldLayer.name = oldLayer.name;

                        oldLayer.visible = true;

                         

                            for (var j = 0; j < grpList.length; j++) {

                                var nLayer = layers.add();

                                nLayer.name = oldLayer.name + '-' + grpList[j].name;

                                nLayer.visible = true;

                         

                                for (var k = 0; k < oldLayer.pathItems; k++) {

                                    oldLayer.pathItems[k].duplicate(nLayer,

                         

                        ElementPlacement.PLACEATBEGINNING);

                                }

                         

                                for (var k = 0; k < nLayer.pathItems.length; k++) {

                                    nLayer.pathItems[k].selected = true;

                                    nLayer.pathItems[k].fillColor = grpList.color;

                                }

                            }

                         

                        }

                         

                        /*

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

                            pathItems[i].selected = true;

                                pathItems[i].fillColor = sw.color;

                            }

                        */

                         

                        }

                        • 9. Re: Crazy Scripting Help
                          RobGreer Community Member

                          I should clarify that the new layers are created with the appropriate layer names but there is nothing on the layer. The object isn't copying. Every reference I was able to find said to do it this way so I know I'm missing something basic.

                          • 10. Re: Crazy Scripting Help
                            moluapple Community Member

                            1. When you add a new layer, the layers.length changed, and "var oldLayer = layers[i];" will always point to the wrong layer. So you should store origin layers to an Array first.

                            2. for (var k = 0; k < oldLayer.pathItems; k++) should be for (var k = 0; k < oldLayer.pathItems.length; k++)

                            3. oldLayer.name = oldLayer.name; ???

                            4. nLayer.pathItems[k].selected = true; In script you do not need to select object first in order to change color.

                             

                            So, here you go:

                             

                            var docRef = app.activeDocument;
                            with(docRef) {
                                var grp = swatchGroups.getByName('Rob');
                                var grpList = grp.getAllSwatches();
                                var originalLayerLength = layers.length;
                                var oldLayers = [];
                                for (var i = 0; i < originalLayerLength; i++) {
                                    oldLayers[i] = layers[i];
                                }
                                for (var i = 0; i < oldLayers.length; i++) {
                                    var oldLayer = oldLayers[i];
                                    oldLayer.visible = true;
                                    for (var j = 0; j < grpList.length; j++) {
                                        var nLayer = layers.add();
                                        nLayer.name = oldLayer.name + '-' + grpList[j].name;
                                        for (var k = 0; k < oldLayer.pathItems.length; k++) {
                                            oldLayer.pathItems[k].duplicate(nLayer, ElementPlacement.PLACEATBEGINNING).fillColor = grpList[j].color;
                                        }
                                    }
                                }
                            }
                            
                            

                            I can not test because I have no document like you have.

                            • 11. Re: Crazy Scripting Help
                              RobGreer Community Member

                              That works perfectly! Thanks a bunch! You rock!