10 Replies Latest reply on Mar 7, 2013 12:32 PM by MrTIFF

    Get text from a box, search swatches and apply color - JS / CS5

    Diego_Pro_Imagem

      Hey all!

       

      First of all, I know just some basic concepts of scripting, my experience on the area is all about taking a little from other scripts and putting it all together to do what I want, so I'm guessing I will be needing a lot of help, so thanks in advance! Second, I'm brazilian and my english isn't that good, so sorry!

       

      Here's what I need to do:

      - I have a file that when I apply Data Merge it develops multiple files with different texts, in multiple frames. Together with these text frames there's another frame where I want to apply the color that is named with the content of the text frame. Something like this:

      MAP.jpg

       

      The Data Merge works perfectly and in this moment I'm applying the colors manually, this step is what I'm looking to automate with your help...

       

      The text frame and the frame that is going to be coloured are in the same group, I don't know if that helps, I'm guessing it does.

       

      I'm guessing that the steps are going to be something like this:

      - search for the group in the opened file;
      - "select" the group (I can label these groups as "FRAME 1"; "FRAME 2" and so on);

      - get the text inside the text frame and make it a variable;

      - select the frame that is going to be coloured;

      - search for the variable in the swatches list;

      - apply the color;

      - go to next group and loop.

       

      The number of groups may vary from file to file.

       

      That's it. I HAVE NO IDEA of how to start this, I know I'm probabling asking too much, so thanks for all your help and for reading this until here.

       

      Thanks, thanks, thanks!

        • 1. Re: Get text from a box, search swatches and apply color - JS / CS5
          MrTIFF Level 3

          Well, there may be more elegant ways, and I may not be making all the right assumptions, but I think you want to do something like this:

           

          function main()

          {

                    for (var d = 0; d < app.documents.length; d++)

                    {

                              var doc = app.documents[d];

                              for (var g = 0; g < doc.groups.length; g++)

                              {

                                        var grp = doc.groups[g];

           

                                        // get the textFrame

                                        if (grp.textFrames.length != 1)

                                        {

                                                  alert("should be 1 textFrame in the group. ignoring...");

                                                  continue;

                                        }

                                        var tf = grp.textFrames[0];

           

                                        // get the swatch

                                        var colorName = tf.contents;

                                        var sw = doc.swatches.item(colorName);

                                        if (!sw)

                                        {

                                                  alert("swatch: " + colorName + " was not found. ignoring...");

                                                  continue;

                                        }

           

                                        // get the graphic frame (rectangle)

                                        if (grp.rectangles.length != 1)

                                        {

                                                  alert("should be 1 graphics frame in the group. ignoring...");

                                                  continue;

                                        }

                                        var r = grp.rectangles[0];

           

                                        // apply the swatch

                                        r.fillColor = sw;

                              }

                    }

          }

           

           

          main();

          1 person found this helpful
          • 2. Re: Get text from a box, search swatches and apply color - JS / CS5
            Jump_Over Level 5

            Hi,

             

            @stephen, watch the line;

             

            if (!sw)

            which is true even if contents doesn't match any of swatche's name.

            Supposed to be

            if (!sw.isValid)

             

            However your code leads to succes if the name is found

            (and rectangles are used not i.e polyghons, ovals...)


            • 3. Re: Get text from a box, search swatches and apply color - JS / CS5
              Diego_Pro_Imagem Level 1

              Thanks for your support. That was a great start indeed!

               

              Now some considerations:

               

               

              1- I did a test if for a swatch that didnt exist and for some reason the alert that checked for the color on the swatch list wasnt working an it was crashing. I tried to change to a simple content check:

              if (sw = "[object Swatch]")

              But then when the swatch exist it simply just dont apply the color.

               

              EDIT: I changed to:
              if (!sw.isValid)

              And now he just alerts everything, even when the color exists

               

               

              2 - Now, it isn't always rectangles, so i changed to this:

               

                                            if (grp.pageItems.length != 2)

                                            {

                                                      alert("Deve haver pelo menos um outro objeto no grupo para que a cor seja aplicada. Ignorando.");

                                                      continue;

                                            }

                                            var r = grp.pageItems[0];

               

              And this worked out fine, cause in the group there will be always just 2 items.

               

              3 - I need to change to it search for the groups only in the layer I'm working, not the whole document. So i changed to:
              var grp = doc.activeLayer.groups[g];

              And it worked out, i guess, I'm still doing some tests.

               

              Heres my script now:

               

              function main()
              {
                        for (var d = 0; d < app.documents.length; d++)
                        {
                                  var doc = app.documents[d];
                                  for (var g = 0; g < doc.activeLayer.groups.length; g++)
                                  {
                                            var grp = doc.activeLayer.groups[g];
              
                                            // get the textFrame
                                            if (grp.textFrames.length != 1)
                                            {
                                                      alert("Deve haver pelo menos um bloco de texto no grupo. Ignorando.");
                                                      continue;
                                            }
                                            var tf = grp.textFrames[0];
              
                                            // get the swatch
                                            var colorName = tf.contents;
                                            var sw = doc.swatches.item(colorName);
                                            if (!sw.isValid);
                                            {
                                                      alert("A cor " + colorName + " não existe. Ignorando.");
                                                      continue;
                                            }
              
                                            // get the graphic frame (rectangle)
                                            if (grp.pageItems.length != 2)
                                            {
                                                      alert("Deve haver pelo menos um outro objeto no grupo para que a cor seja aplicada. Ignorando.");
                                                      continue;
                                            }
                                            var r = grp.pageItems[0];
              
                                            // apply the swatch
                                            r.fillColor = sw;
                                  }
                        }
              }
              
              
              main();
              

               

               

              Thats it for now, I'll keep trying... Thanks for all again.

              • 4. Re: Get text from a box, search swatches and apply color - JS / CS5
                Jump_Over Level 5

                Hi,

                 

                if (!sw.isValid);<== ";" has to be removed

                otherwise if statements ends at first line.

                 

                if (grp.pageItems.length !=2)

                I suggest to use splineItems instead of pageItems.

                Advantage: textFrames excluded and any graphic frame included.

                 

                rgds

                1 person found this helpful
                • 5. Re: Get text from a box, search swatches and apply color - JS / CS5
                  MrTIFF Level 3

                  Duh - forgot to test the swatch-not-found case. Thanks!

                  • 6. Re: Get text from a box, search swatches and apply color - JS / CS5
                    Diego_Pro_Imagem Level 1

                    OK now guys, everything worked out perfectly, now I'm just gonna put some extra benefits.

                     

                    Here's the result:

                    function main()
                    {
                              for (var d = 0; d < app.documents.length; d++)
                              {
                                        var doc = app.documents[d];
                    
                                        // checa se não existe nenhum grupo
                                        var nenhum = doc.activeLayer.groups.length;
                    
                                        if (nenhum < 1)
                                        {
                                            alert("Não há nenhum grupo na layer ativa, por favor, selecione a layer correta.");
                                            continue;
                                        }
                    
                                        for (var g = 0; g < doc.activeLayer.groups.length; g++)
                                        {
                                                  var grp = doc.activeLayer.groups[g];
                    
                    
                                                  // checa se existe mais de 2 objetos no grupo
                                                   if (grp.splineItems.length > 1)
                                                  {
                                                            alert("Um dos grupos possui mais que dois objetos. Ignorando.");
                                                            continue;
                                                  }
                    
                                                  // get the textFrame
                                                  if (grp.textFrames.length != 1)
                                                  {
                                                            alert("Não há nenhum ou mais que um bloco de texto no grupo. Ignorando.");
                                                            continue;
                                                  }
                                                  var tf = grp.textFrames[0];
                    
                                                  // get the swatch
                                                  var colorName = tf.contents;
                                                  var sw = doc.swatches.item(colorName);
                                                  if (!sw.isValid)
                                                  {
                                                            alert("A cor " + colorName + " não existe. Ignorando.");
                                                            continue;
                                                  }
                    
                                                  // get the graphic frame (rectangle)
                                                  if (grp.splineItems.length != 1)
                                                  {
                                                            alert("Deve haver pelo menos um outro objeto no grupo para que a cor seja aplicada. Ignorando.");
                                                            continue;
                                                  }
                                                  var r = grp.splineItems[0];
                    
                                                  // apply the swatch
                                                  r.fillColor = sw;
                                        }
                              }
                    }
                    
                    
                    main();
                    

                     

                    I added a If check to alert if there's no groups in the current layer, changed from pageItems to splineItems.

                     

                    Now I'm looking to delete the group that the text box is empty. Because, as I said before, it runs in multiple files, and some of them have few or more colors then others. So the script keeps warning me that the color "" dont exist because the box is empty. But the plus is that, instead of just skip the alert and go to next i want to erase the group that contains the empty text box. I tried this:

                     

                     

                    if (colorName == "")
                    {
                    grp.remove();
                    }
                    

                     

                    But it doesnt work.

                    • 7. Re: Get text from a box, search swatches and apply color - JS / CS5
                      MrTIFF Level 3

                      Well, the test and remove DO work: 

                       

                      if (colorName == "") {

                           grp.remove();

                           continue;     // to skip to the next group

                      }

                       

                      Other than that 'continue', one more line needs to change:

                      the top of the group loop, to:

                       



                      for (var g = doc.groups.length-1; g >= 0; g--)

                       

                      because whenever you remove an item from an array or collection, you have to ask yourself if the indexing of the collection has changed.  It has, so you want to loop backward through the collection of groups.

                      1 person found this helpful
                      • 8. Re: Get text from a box, search swatches and apply color - JS / CS5
                        Laubender Adobe Community Professional & MVP

                        Hi, all!

                         

                        See the following discussion in the Swiss/German forum HilfDirSelbst at:

                         

                        http://www.hilfdirselbst.ch/gforum/gforum.cgi?post=499391#499391

                         

                        Here the latest version of the ColorPattern script:

                         

                        http://www.hilfdirselbst.ch/gforum/gforum.cgi?post=502427#502427

                         

                        Hans Haesler did some scripts that are building color ramps or color patterns out of a list of color definitions (csv file).

                         

                        Also helpful the original discussion at (though altogether in German):

                         

                        http://www.hilfdirselbst.ch/foren/Farbmusterbuch_mit_Indesign_P499018.html#499018

                         

                         

                         

                        Uwe

                        • 9. Re: Get text from a box, search swatches and apply color - JS / CS5
                          Diego_Pro_Imagem Level 1

                          Thanks again Stephen. I tried to include the continue line and change the for at the start, still not getting anywhere.

                          It alerts me that the color of the text box that is empty doesnt exist, but the script just passes through the if check, I tested like this:

                           

                                                        if (colorName == "")
                                                        {
                                                            alert("TESTE");
                                                            grp.remove();
                                                            continue;
                                                        }
                          

                           

                           

                          The whole script is now like this:

                          function main()
                          {
                                    for (var d = 0; d < app.documents.length; d++)
                                    {
                                              var doc = app.documents[d];
                                              
                                              // checa se não existe nenhum grupo
                                              var nenhum = doc.activeLayer.groups.length;
                                              
                                              if (nenhum < 1)
                                              {
                                                  // alert("Não há nenhum grupo na layer ativa, por favor, selecione a layer correta.");
                                                  continue;
                                              }
                                              
                                              for (var g = doc.activeLayer.groups.length-1; g >= 0; g--)
                                              {
                                                        var grp = doc.activeLayer.groups[g];
                          
                          
                                                        // checa se existe mais de 2 objetos no grupo
                                                         if (grp.splineItems.length > 1)
                                                        {
                                                                  // alert("Um dos grupos possui mais que dois objetos. Ignorando.");
                                                                  continue;
                                                        }
                                                        
                                                        // get the textFrame
                                                        if (grp.textFrames.length != 1)
                                                        {
                                                                  // alert("Não há nenhum ou mais que um bloco de texto no grupo. Ignorando.");
                                                                  continue;
                                                        }
                                                        var tf = grp.textFrames[0];
                          
                                                        // get the swatch
                                                        var colorName = tf.contents;
                          
                          
                                                        // remove o grupo que não possui texto
                                                        if (colorName == "")
                                                        {
                                                            alert("TESTE");
                                                            grp.remove();
                                                            continue;
                                                        }
                                                        // final do "remove o grupo que não possui texto"
                                                    
                                                        var sw = doc.swatches.item(colorName);
                                                        if (!sw.isValid)
                                                        {
                                                                  alert("A cor " + colorName + " não existe. Ela está presente no arquivo " + doc.name );
                                                                  continue;
                                                        }
                          
                                                        // get the graphic frame (rectangle)
                                                        if (grp.splineItems.length != 1)
                                                        {
                                                                  // alert("Deve haver pelo menos um outro objeto no grupo para que a cor seja aplicada. Ignorando.");
                                                                  continue;
                                                        }
                                                        var r = grp.splineItems[0];
                          
                                                        // apply the swatch
                                                        r.fillColor = sw;
                                              }
                                    }
                          }
                          
                          
                          main();
                          

                           

                          I commented all the alerts, except when the color doesnt exist, because it loops throught all documents and it was giving me too many alerts.
                          But it is working very nice already, so thanks again!

                          • 10. Re: Get text from a box, search swatches and apply color - JS / CS5
                            MrTIFF Level 3

                            I ran exactly your code above on my test doc, and it worked perfectly. So the logical conclusion is that there must be something in that "empty" textFrame of yours -- even if it is only a space or a Return.

                             

                            So you may need to broaden that test a bit. Instead of

                             

                            if (colorName == "")

                             

                            you could test the length of the string, for example, like

                             

                            if (colorName.length <= 1)

                             

                             


                            1 person found this helpful