• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script for removing color definitions

Community Beginner ,
Nov 17, 2011 Nov 17, 2011

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

3.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Dec 14, 2011 Dec 14, 2011

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;

        }

   

...

Votes

Translate

Translate
Nov 17, 2011 Nov 17, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 13, 2011 Dec 13, 2011

Copy link to clipboard

Copied

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

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Dec 14, 2011 Dec 14, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 14, 2011 Dec 14, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 14, 2011 Dec 14, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 14, 2011 Dec 14, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 14, 2011 Dec 14, 2011

Copy link to clipboard

Copied

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;

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 15, 2011 Dec 15, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 15, 2011 Dec 15, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 15, 2011 Dec 15, 2011

Copy link to clipboard

Copied

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

Milo

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 14, 2020 Apr 14, 2020

Copy link to clipboard

Copied

LATEST

Hi,

 

First time posting here...thank you Ian for sharing your expertise. I'm on Frame 2017. I'm working with a book that has a multiplicity of cross-ref formats – so many, in fact, that I can't add more, which I've never encountered before. I adapted this to remove the unwanted ones. In so doing, I found that I had to change this...

if (this === value) {

to this:

if (this[i] === value) {

 

Thanks Again,

Les

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines