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

Trouble dealing with setting colors on grouped objects.

Engaged ,
Jun 18, 2023 Jun 18, 2023

I've been working on a script for awhile but I've come to a sticking point. The script I created duplicates the current page and then duplicates the color swatches on that page and assigns them to the new page. This allows me to test out different color pallets without messing up the first page. Anyhow the script works great if everything is ungrouped or if the things that are grouped have the same color but it fails if the group contains objects that have different colors. Does anyone have any idea how I can deal with this? How do you deal with gouped objects, or can I ungroup and then group together again? Here is the script, thanks for any help. 

var doc = app.activeDocument;

// Select the last page in the document
var lastPage = doc.pages.lastItem();
app.activeWindow.activePage = lastPage;

var currentPage = app.activeWindow.activePage;
var currentPageNumber = currentPage.documentOffset + 1;

// Create a white swatch with custom name
function createWhiteSwatch() {
    var swatchName = "======== Duplicate " + currentPageNumber + " ========"
    var existingSwatch = doc.colors.itemByName(swatchName);

    if (existingSwatch.isValid) {
        return existingSwatch;
    }

    var whiteSwatch = doc.colors.add({
        name: swatchName,
        model: ColorModel.process,
        colorValue: [0, 0, 0, 0],
        space: ColorSpace.CMYK
    });

    return whiteSwatch;
}


// Duplicate color and return the new color
function duplicateColor(color) {
    var newColorName = color.name + "_Duplicate_" + currentPageNumber;
    var existingColor = doc.colors.itemByName(newColorName);

    if(existingColor.isValid){
        return existingColor;
    }

    var newColor = doc.colors.add({
        model: color.model,
        space: color.space,
        colorValue: color.colorValue,
        name: newColorName
    });
    return newColor;
}

// Create white swatch with custom name
createWhiteSwatch();

// Duplicate current page
var newPage = currentPage.duplicate(LocationOptions.AFTER, currentPage);

// Duplicate colors used on the current page and assign to new page elements
for (var i = 0; i < currentPage.pageItems.length; i++) {
    var currentItem = currentPage.pageItems[i];
    var newItem = newPage.pageItems[i];

    // Check if the item has a fillColor property (not all pageItems have color properties)
    if (currentItem.hasOwnProperty("fillColor")) {
        // Check if fillColor is a simple color, not a gradient or mixed ink
        if (currentItem.fillColor instanceof Color) {
            var newFillColor = duplicateColor(currentItem.fillColor);
            newItem.fillColor = newFillColor;
        }
    }

    // Check if the item has a strokeColor property
    if (currentItem.hasOwnProperty("strokeColor")) {
        // Check if strokeColor is a simple color, not a gradient or mixed ink
        if (currentItem.strokeColor instanceof Color) {
            var newStrokeColor = duplicateColor(currentItem.strokeColor);
            newItem.strokeColor = newStrokeColor;
        }
    }
}
TOPICS
Scripting
514
Translate
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

Community Expert , Jun 18, 2023 Jun 18, 2023

Hi @davidn5918184 , Look at the page’s .allPageItems property rather than .pageItems—.allPageItems returns an array (not a collection) of page items, groups, and the groups’s items. So this gets all of the page items except for the 2 group objects:

 

 

 

var api = app.activeDocument.pages[0].allPageItems;
for (var i = 0; i < api.length; i++){
    if (api[i].constructor.name != "Group") {
        $.writeln(api[i])
        //returns 5 Rectangles
    }
};   

 

 

 

For this allPageItems length is 7—

...
Translate
Community Expert ,
Jun 18, 2023 Jun 18, 2023

Hi @davidn5918184 , Look at the page’s .allPageItems property rather than .pageItems—.allPageItems returns an array (not a collection) of page items, groups, and the groups’s items. So this gets all of the page items except for the 2 group objects:

 

 

 

var api = app.activeDocument.pages[0].allPageItems;
for (var i = 0; i < api.length; i++){
    if (api[i].constructor.name != "Group") {
        $.writeln(api[i])
        //returns 5 Rectangles
    }
};   

 

 

 

For this allPageItems length is 7—5 rectangles and 2 groups:

 

Screen Shot 20.png

Translate
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
Engaged ,
Jun 19, 2023 Jun 19, 2023
LATEST

Just wanted to say a quick thanks for helping me out with my script. Your input was spot-on and really helped me solve the issue!

Translate
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