Skip navigation
Currently Being Moderated

Script for removing color definitions

Nov 17, 2011 9:24 AM

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.

 
Replies
  • Currently Being Moderated
    Nov 17, 2011 10:34 AM   in reply to Milo G

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 14, 2011 2:25 AM   in reply to Milo G

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 14, 2011 7:47 AM   in reply to Milo G

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 14, 2011 3:27 PM   in reply to Milo G

    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;
        }
    }
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Dec 15, 2011 9:56 AM   in reply to Milo G

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points