Skip to main content
Participating Frequently
November 17, 2011
Answered

Script for removing color definitions

  • November 17, 2011
  • 1 reply
  • 3873 views

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.

This topic has been closed for replies.
Correct answer Ian Proudfoot

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


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 === value) {

            return true;

        }

    }

}

while(col.ObjectValid()){

    if (!keepColors.contains(col.Name)) {

        col2 = col.NextColorInDoc;

        col.Delete();

        col = col2;

    }

    else {

        col = col.NextColorInDoc;

    }

}

1 reply

Inspiring
November 17, 2011

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

Milo GAuthor
Participating Frequently
December 13, 2011

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

}

}

Inspiring
December 14, 2011

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