Skip to main content
Participating Frequently
September 28, 2009
Question

Return The Colors Used In A Group

  • September 28, 2009
  • 1 reply
  • 785 views

Is there a way to return the colors used in a group? I am trying to place color labels at the top of a sheet, one label for each color used in the document. I can delete all of the un-used colors from the color palette, except for Black, and the refer to those colors. The problem is that sometimes my document doesn't actually contain black, so I'm not really sure how to get around this problem.

Any advice?

Thanks

This topic has been closed for replies.

1 reply

Inspiring
September 29, 2009

AFAIK you have to do the labour of inspecting every page item inside the group.

If your documents contain only flat groups (i.e. you have no groups inside other groups) a simple loop on myGroup.pageItems should do.

If you have nested groups and pageitems pasted into other pageItems you should probably look into a recursive algorithm (http://en.wikipedia.org/wiki/Recursion_(computer_science))

Kasyan Servetsky
Legend
September 29, 2009

(http://en.wikipedia.org/wiki/Recursion_(computer_science)

This link is broken

I'd use the following approach to get the list of colors used in a group (even if it includes nested groups):


var myDoc = app.activeDocument;
var myGroup = myDoc.groups[0];
var myPageItems = myGroup.allPageItems;
    var myColors =[];
   
for (i = 0; i < myPageItems.length; i++) {
    var myPageItem = myPageItems;
    try {
        if  (!IsInArray(myPageItem.fillColor.name, myColors)) {
            myColors.push(myPageItem.fillColor.name);
        }
        if  (!IsInArray(myPageItem.strokeColor.name, myColors)) {
            myColors.push(myPageItem.strokeColor.name);
        }
    }
    catch (myError) {    }
}

if (myColors.length != 0) {
    alert("List of the colors used in the group:\n" + myColors.join(",").replace(/,/g, "\n"));
}


function IsInArray(myString, myArray) {
    for (x in myArray) {
        if (myString == myArray) {
            return true;
        }
    }
    return false;
}

Inspiring
September 29, 2009

>This link is broken

Sorry, the forumsoftware omitted the last parantheses from the link.

I forgot that group has an "allPageItems" property. Nice