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.
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
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
}
}
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
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
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[i] === value) {
return true;
}
}
}
while(col.ObjectValid()){
if (!keepColors.contains(col.Name)) {
col2 = col.NextColorInDoc;
col.Delete();
col = col2;
}
else {
col = col.NextColorInDoc;
}
}
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
North America
Europe, Middle East and Africa
Asia Pacific