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

Cannot fill CompoundPathItem via script

Explorer ,
Jul 05, 2020 Jul 05, 2020

I have a layer with a group and I want to change the color in filled and stroked elements,
It works for PathItems but not for CompoundPaths.

 

var mm          = 2.834645;
var px          = 1;
var doc         = app.activeDocument;

fullDocName = doc.fullName;

var grey = new CMYKColor();
grey.cyan = 10;
grey.magenta = 0;
grey.yellow = 5;
grey.black = 60;

if (app.documents.length > 0) {
    var swatch = doc.swatches.add();
    swatch.color = grey;
    normlayer = doc.layers.getByName("Norm");

    try {
        if (doc.layers.getByName("Norm")) {
            doc.layers.getByName("Norm").locked = false;

            var norm = doc.layers.getByName("Norm");
            norm.pageItems[0].pathItems.rectangle(-8.5*mm, 8.5*mm, 50.5*mm, 50.5*mm);

            for(var i = 0; i < norm.pageItems[0].pathItems.length; i++){
                var normpath = norm.pageItems[0].pathItems[i];
                var normobj = norm.pageItems[0];

                var ab = doc.artboards[0];
                var artboardRight   = ab.artboardRect[2];
                var artboardBottom  = ab.artboardRect[3];
                var artboardX       = ab.artboardRect[0];
                var artboardY       = ab.artboardRect[1];
                var horziontalCenterPosition = (artboardRight - normobj.width)/1.36;
                var verticalCenterPosition = (artboardY + normobj.height)/2;
                normobj.position = [horziontalCenterPosition, verticalCenterPosition];

                if (normpath.filled = true) {
                    normpath.fillColor = swatch.color;
                }

                if (normpath.stroked = true) {
                    normpath.strokeColor = swatch.color;
                } 
            }

            var place = norm.pageItems[0].pathItems[0];
            place.filled = false;
            place.stroked = false;

        }
    } catch (e) {}


	var item = doc.pageItems[0];
	var scale = 22;
	item.resize(
	    scale , 
	    scale ,
	    true,
	    true,
	    true, 
	    true,
	    scale ,
	    undefined);
}
TOPICS
Scripting
1.2K
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 2 Correct answers

Community Expert , Jul 05, 2020 Jul 05, 2020

Hi ellipirelli,

 

I think you need to handle CompoundPathItems specially:

if ( normpath.typename == 'CompoundPathItem') {
    normpath.pathItems[0].fillColor = swatch.color;
} else {
    normpath.fillColor = swatch.color;
}

 

CompoundPathItems have their own PathItems and you seem to need to set at least the first one of those.

 

Regards,

Mark

Translate
Explorer , Jul 06, 2020 Jul 06, 2020

Hi, I have actually found my own answer. 

 

 

            var group = doc.layers["Norm"].groupItems[0];
            for (var i = (group.pageItems.length) - 1; i >= 0; i--) {
                if (group.pageItems[i].typename == "CompoundPathItem") {
                    group.pageItems[i].pathItems[0].filled = true;
                     group.pageItems[i].pathItems[0].fillColor = swatch.color;
                }
            }

 

Translate
Adobe
Community Expert ,
Jul 05, 2020 Jul 05, 2020

Hi ellipirelli,

 

I think you need to handle CompoundPathItems specially:

if ( normpath.typename == 'CompoundPathItem') {
    normpath.pathItems[0].fillColor = swatch.color;
} else {
    normpath.fillColor = swatch.color;
}

 

CompoundPathItems have their own PathItems and you seem to need to set at least the first one of those.

 

Regards,

Mark

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
Explorer ,
Jul 06, 2020 Jul 06, 2020

Thanks Mark,

I actually tried secveral versions of this yesterday and it didn't work. I want to change the colors via script but the compound path items stay the same.

 

The last thing I tried is this:

 

 

 

var mm          = 2.834645;
var px          = 1;
var doc         = app.activeDocument;

//CMYK

var grey = new CMYKColor();
grey.cyan = 10;
grey.magenta = 0;
grey.yellow = 5;
grey.black = 60;


if (app.documents.length > 0) {

    doc.artboards[0].artboardRect = [0, 14.5*mm, 26*mm, 0];

    var swatch = doc.swatches.add();
    swatch.color = grey;
    swatch.name = "Grey CMYK";

    normlayer = doc.layers.getByName("Norm");

    try {
        if (doc.layers.getByName("Norm")) {
            doc.layers.getByName("Norm").locked = false;

            var norm = doc.layers.getByName("Norm");
            norm.pageItems[0].pathItems.rectangle(-8.5*mm, 8.5*mm, 50.5*mm, 50.5*mm);

            for(var i = 0; i < norm.pageItems[0].pathItems.length; i++){
                var normpath = norm.pageItems[0].pathItems[i];
                var normobj = norm.pageItems[0];

                var ab = doc.artboards[0];
                var artboardRight   = ab.artboardRect[2];
                var artboardBottom  = ab.artboardRect[3];
                var artboardX       = ab.artboardRect[0];
                var artboardY       = ab.artboardRect[1];
                var horziontalCenterPosition = (artboardRight - normobj.width)/1.36;
                var verticalCenterPosition = (artboardY + normobj.height)/2;
                normobj.position = [horziontalCenterPosition, verticalCenterPosition];

                if (normpath.filled = true) {
                    normpath.fillColor = swatch.color;
                }

                if (normpath.stroked = true) {
                    normpath.strokeColor = swatch.color;
                } 

                if (normpath.typename == "CompoundPathItem" && normpath.filled = true) {
                        normpath.fillColor = swatch.color;
                }

                if (normpath.typename == "CompoundPathItem" && normpath.stroked = true) {
                        normpath.strokeColor = swatch.color;
                }

            }

            var place = norm.pageItems[0].pathItems[0];
            place.filled = false;
            place.stroked = false;

        }
    } catch (e) {}

    
	var item = doc.pageItems[0];
	var scale = 22;
	item.resize(
	    scale , 
	    scale ,
	    true,
	    true,
	    true, 
	    true,
	    scale ,
	    undefined);



	doc.layers.add();
    doc.layers[0].name = "Rectangle";
    var rect = doc.layers.getByName("Rectangle");
    rect.pathItems.roundedRectangle(25.7*mm, 14.2*mm, 25.7*mm, 14.19*mm, 2*mm, 2*mm, false);
    rect.move(doc, ElementPlacement.PLACEATEND);
    var rectobj = rect.pathItems[0];
    var widthTo = 25.7*mm;
    var scale = widthTo * 100 / rectobj.width;
    rectobj.resize(scale, scale);
    rectobj.position = [.15*mm, 14.344*mm];
    rect.pathItems[0].strokeWidth = 0.88;
    rect.pathItems[0].filled = false;
    rect.pathItems[0].stroked = true;
    rect.pathItems[0].strokeColor = swatch.color;
}

 

 

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
Community Expert ,
Jul 06, 2020 Jul 06, 2020

Did you deliberately use the assignment (=) operator here?

 

if (normpath.typename == "CompoundPathItem" && normpath.filled = true) {

 

 

Could you give it a try this way?

 

if (normpath.typename == "CompoundPathItem" && normpath.filled == true) {

 

 

Not sure if that will fix it, but worth a shot.

 

Edit: Wait! it could be that you are checking the theCompoundPathItem.filled rather than theCompoundPathItem.pathItems[0].filled? Just another thought.

 

Mark

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
Explorer ,
Jul 06, 2020 Jul 06, 2020

This does not make any change.

 

Also the CompoundPathItem is part of a group. 
--> var normpath = norm.pageItems[0].pathItems[i];

 

Also tried the following:

 

var group = doc.layers["Norm"].groupItems[0];
        for (var i = (group.pageItems.length) - 1; i >= 0; i--) {
            if (group.pageItems[i].typename == "CompoundPathItem") {

                var normpath2 = group.pageItems[i];

                //group.pageItems[i].remove();

                if (normpath2.filled = true) {
                    normpath2.fillColor = swatch.color;
                }

                if (normpath2.stroked = true) {
                    normpath2.strokeColor = swatch.color;
                } 


            }
        }
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
Explorer ,
Jul 06, 2020 Jul 06, 2020

Hi, I have actually found my own answer. 

 

 

            var group = doc.layers["Norm"].groupItems[0];
            for (var i = (group.pageItems.length) - 1; i >= 0; i--) {
                if (group.pageItems[i].typename == "CompoundPathItem") {
                    group.pageItems[i].pathItems[0].filled = true;
                     group.pageItems[i].pathItems[0].fillColor = swatch.color;
                }
            }

 

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
Community Expert ,
Jul 06, 2020 Jul 06, 2020

Excellent!

 

By the way, I find it helpful to be as specific or general about variable names for objects as is warranted by tests. eg. if you don't know whether an item is a PathItem, then just call it "item", once you're sure its a PathItem, call it "pathItem", and then later, once you've checked if it is a CompoundPathItem, call it that. In your original script you referred to "normpath", but you discovered (after a lot of work!) that it wasn't a path, but a GroupItem. Calling it "item" originally might have been helpful conceptually.

 

Mark

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
Explorer ,
Jul 08, 2020 Jul 08, 2020

I'm still having a bit of trouble about which is which.  But getting there 😄

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
Community Expert ,
Jul 08, 2020 Jul 08, 2020
LATEST

Hi @ellipirelli,

there is no need to loop through all group pageItems. Use the class CompoundPathItems instead.

 

Here an example with much more better performance:

 

var aDoc = app.activeDocument;
var cPI = aDoc.layers["Norm"].groupItems[0].compoundPathItems;
var cPIlen = cPI.length;
for (var i = 0; i < cPIlen; i++) {
    cPI[i].pathItems[0].filled = true;
    cPI[i].pathItems[0].fillColor = aDoc.swatches[8].color;
}

 

 

If that works for you

have fun

😉

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