10 Replies Latest reply: Dec 15, 2011 11:49 AM by Milo G RSS

    Script for removing color definitions

    Milo G

      I'm trying to create a script that will remove color definitions other than the default plus our corporate colors. I'm thinking some kind of "while" loop to go through the doc's color list, matching it to an array of named colors.

       

      I see that there is a Color object and methods such as Delete(), but it's not coming together for me.

       

      Any help would be appreciated.

        • 1. Re: Script for removing color definitions
          Chintu_123 Adobe Employee

          I am posting a small snippet which demonstrates how to traverse the color object in doc and delete it. The following sample iterates over all the color present in the doc until it finds a color named “testcolor” and then deletes it. It works on the active document.

           

          doc = app.ActiveDoc   //getting the active doc

           

          col  = doc.FirstColorInDoc   //getting the first color in doc

           

          while (col.ObjectValid() )  //checking if the color object is valid

          {

              if (col.Name == "testcolor" )  // matching for testcolor

              {

                  col.Delete()  //deleting the color

                  break

               }

          else {

          col = col.NextColorInDoc

          }

          }

           

          Regards,

          Vikash

          FrameMaker Engineering team

          • 2. Re: Script for removing color definitions
            Milo G Community Member

            Finally able to get back to this project.

             

            Thanks for this snippet which does work great for removing a single "known" color. What I actually need to do is remove all colors except a specified list.

             

            I've made an attempt (I'm sill relatively new to JS programming), but it only deletes the first instance instead of iterating through all the color definitions:

             

            doc = app.ActiveDoc   //getting the active doc

             

            col  = doc.FirstColorInDoc   //getting the first color in doc

             

            keepColors = new Array ("Black","White","Red","Green","Blue","Cyan","Magenta","Yellow","Dark Grey","Pale Green","Forest Green","Royal Blue","Olive","Salmon","Mauve","Light Salmon","Custom Color");

             

            while (col.ObjectValid() )  //checking if the color object is valid

            {

                if (col.Name in (keepColors))  //color exists

                {

                   break

                 }

            else {

            col.Delete()  //delete color

            col = col.NextColorInDoc

            }

            }

            • 3. Re: Script for removing color definitions
              Chintu_123 Adobe Employee

              You are using "break" in the while loop which will terminate the loop whenever the condition is true. Try the following snippet:

               

              while( col.ObjectValid() )

              {

                   if (col.Name <not in> (keepColors) )  //Color doesn't exits

                   {

                        col2 = col.NextColorInDoc

                        col.Delete()

                        col = col2

                   }

                   else{

                   col = col.NextColorInDoc

                   }

              }

               

              Regards,

              Vikash

              Frame Engineering Team

              • 4. Re: Script for removing color definitions
                Milo G Community Member

                I think I need something more complicated than my solution for "<not in>."

                 

                I tried:

                     if (!(col.Name in keepColors))

                 

                Which successfully eliminated all non-default colors, but it also eliminated the custom colors I designated in keepColors.

                • 5. Re: Script for removing color definitions
                  Ian Proudfoot Community Member

                  The problem is that (col.Name in keepColors) does not work on the string value of the array element. It is checking for the element's name which in an array is its index value. The way to do this is to create a for loop to check each value of the array against each color in the document.

                   

                  Ian

                  • 6. Re: Script for removing color definitions
                    Milo G Community Member

                    Okay, I got it to work with the rather sloppy:

                     

                         if (!(col.Name in {"Custom1":"", "Custom2":"", "Custom3":""}))

                     

                     

                    Thankfully we only have three corporate colors so this isn't too ugly. If someone has time, I would like to see a cleaner method of checking against an array (or list) of items as my next project is to clean out variables for which we have tons that are no longer acceptable.

                     

                     

                     

                    Thanks

                     

                     

                    Milo

                    • 7. Re: Script for removing color definitions
                      Ian Proudfoot Community Member

                      Try this as one way to solve the problem. It adds a new contains() method to the Array class.

                       

                      var doc = app.ActiveDoc, 
                          col  = doc.FirstColorInDoc
                          keepColors = ["Black","White","Red","Green","Blue","Cyan","Magenta","Yellow","Dark Grey","Pale Green","Forest Green","Royal Blue","Olive","Salmon","Mauve","Light Salmon","Custom Color"];
                      
                      
                      Array.prototype.contains = function (value) {
                          var i;
                          for (i = 0; i < this.length; i+=1) {
                              if (this[i] === value) {
                                  return true;
                              }
                          }
                      }
                      
                      
                      while(col.ObjectValid()){
                          if (!keepColors.contains(col.Name)) { 
                              col2 = col.NextColorInDoc;
                              col.Delete();
                              col = col2;
                          }
                          else {
                              col = col.NextColorInDoc;
                          }
                      }
                      
                      
                      • 8. Re: Script for removing color definitions
                        Milo G Community Member

                        Well that did the trick! Sadly I only have a vague understanding of the code, but I'll happily copy and paste it for now.

                         

                        I did find that I only need to list the custom colors in the array as, understandably, the default colors can't be deleted.

                         

                         

                         

                        Milo

                        • 9. Re: Script for removing color definitions
                          Ian Proudfoot Community Member

                          Milo,

                           

                          In JavaScript it is possible to extend the built-in classes. Normally the Array class has no method for testing if it contains an element with a specific value. The new anonymous function simply checks each element in the array and returns true if it matches the value parameter.

                           

                          Aother useful way to extend the Array class would be to add an indexOf() method this could return the index number of a given value, or -1 if the value is not found.

                           

                          Ian

                          • 10. Re: Script for removing color definitions
                            Milo G Community Member

                            Ah, it's starting to make sense. Thanks for taking the time for writing and explaining the code.

                             

                             

                            Milo