Skip to main content
Ku11
Inspiring
January 4, 2022
Answered

Change the color of the fill (without affecting the stroke color)

  • January 4, 2022
  • 1 reply
  • 2866 views

Hello, everyone. I got the code on Community and modified it. The code changes the fillColor, but it also fills the stroke with color (even though the stroke doesn't need it). I searched for the code on Community, but still don't know how to fix.

1. only change those real fillcolor, ignoring the state of the stroke.
2, Is it possible to change the code so that it can also change the of strokecolor and ignore the state of the fillcolor?
3, The code doesn't seem to work when the target layer is off display

 

 

//定义变量
    var document = app .活动文档;
     var allLayers = app 。活动。层; 

    var darkGreyCMYK =   new   CMYKColor  (  )  ; 
        深灰尘CMYK 。青色=   0  ; 
        深灰尘CMYK 。洋红色=   100  ; 
        深灰尘CMYK 。黄色=   100  ; 
        深灰尘CMYK 。黑色=   0  ;

//基本功能
    //~避免因为不存在的层名称而导致的失败。
    function   getLayer ( layerName )   {  
        try   {  
            return document . layer.getByName ( layerName )  ;  
        }捕捉(错误){
            返回空值;
        }  
    }  
    //收集任何子路径项组,她们的父项是/复合路径
    函数 getAllPathItems ( parent )   {  
        var list =   [  ]  ;
        为(VAR我=   0  ;我<母体。 pageItems 。长度;我+ +){  
        VAR项=母体。pageItems [ i ]  ;  
        if   ( item . pageItems && item . pageItems . length )  
            list =   [ ] .concat ( list ,   getAllPathItems (项)  )  ; 
        否则,如果(/路/我。测试(项。类型名)&&   !/复合/ I 。测试(项。类型名))
            列表。推(项目);
        }
        返回列表;
    }



//~change okeCap 和 StrJoin 
    var targetLayers =   [  "a"  ,   "D-14-热塑性填充"  ]  ;

//定义lineFormat
函数 restyleAllPathItemsWithin ( color , layers )   {  
    app .选择=空;
     for   (  var i =   0  ; i <层。长度; i ++  )   {  
        var layer =   getLayer ( layers [ i ]  )  ; 
        继续;
        别的{  
            VAR列表=   getAllPathItems(层);
            为(VAR索引=   0  ;索引<列表。长度;索引++){  
            VAR pathItem =列表[指数]  ; 
            路径项。fillColor = color
              //pathItem.StrokeColor = color 
            //pathItem.strokeCap = StrokeCap.BUTTENDCAP; //ROUNDENDCAP, PROJECTINGENDCAP 
            //pathItem.strokeJoin = StrokeJoin.ROUNDENDJOIN; //BEVELENDJOIN, MITERENDJOIN 
            }  
        }  
    }  
}

//使用函数
restyleAllPathItemsWithin ( darkGreyCMYK , targetLayers )  ;

 

 

This topic has been closed for replies.
Correct answer Ku11

nothing happens and no text items and does not contain text items within the group = =

//Define Variables
    var document = app.activeDocument;
    var allLayers = app.activeDocument.layers; 
    var redColor = new RGBColor();
        redColor.red = 255;
        redColor.green = 0;
        redColor.blue = 0;


    function Changecolor(){
        //DefineVar
            var fillLayer = ["Test"];

        //DefineFunction
            function fillColor(color, targetLayer){
                app.selection = null;
                for (var i = 0; i < allLayers.length; i++) {
                    var curLayer = getLayer(targetLayer[i]);

                    if (curLayer = targetLayer[i]) {
                        document.activeLayer = document.layers.targetLayer[i]; 

                        var docText = document.layers.textFrames;
                        for ( var i = 0; i < docText.length; i++ ){
                            if (ture && docText.filled ) {
                                docText.selected = true;
                                //docText.Filled = true;
                                docText.FillColor = color;

                        }
                    }
                }
            }

            fillColor(redColor, fillLayer)
    }

 

i don't know, maybe we can just modify this script to get the text items at the same time

looking for your help

    //Gather any children pathItems and textframes in case their parent is a Group/Compound Path
        function getAllPathItems(parent) {
            var list = [];
            for (var i = 0; i < parent.pageItems.length; i++) {
            var item = parent.pageItems[i];
            if (item.pageItems && item.pageItems.length)
                list = [].concat(list, getAllPathItems(item));
            else if (/path/i.test(item.typename) && !/compound/i.test(item.typename))
            //else if (/path/i.test(item.typename) && /text/i.test(Frames.typename) && !/compound/i.test(item.typename))
                list.push(item);
            }
            return list;
        }

 

Best regards


Here is the full script. It can changes the color of text boxes and path items in layers at the same time (ignoring black and color) 

Thank you Charu Rajput for your help

 

Best regards

 

#target illustrator  

//~Define Variables
    var document = app.activeDocument;
    var allLayers = app.activeDocument.layers; 
    //~~
    var black = new RGBColor();
        black.red = 0;
        black.green = 0;
        black.blue = 0;
    var darkGrey = new RGBColor();
        darkGrey.red = 85;
        darkGrey.green = 85;
        darkGrey.blue = 85;
    var lightGrey = new RGBColor();
        lightGrey.red = 128;
        lightGrey.green = 128;
        lightGrey.blue = 128;

//~BASE Function
    //~~avoid failures due to non-existent layer names.
        function getLayer(layerName) {
            try {
                return document.layers.getByName(layerName);
            } catch(err) {
                return null;
            }
        }
    //Gather any children pathItems in case their parent is a Group/Compound Path
        function getAllPathItems(parent) {
            var list = [];
            for (var i = 0; i < parent.pageItems.length; i++) {
            var item = parent.pageItems[i];
            if (item.pageItems && item.pageItems.length)
                list = [].concat(list, getAllPathItems(item));
            else if (/path/i.test(item.typename) && !/compound/i.test(item.typename))
                list.push(item);
            }
            return list;
        }
	//Gather All textframes
		function getAllTextFrames(parent) {
            var list = [];
            for (var i = 0; i < parent.pageItems.length; i++) {
            var item = parent.pageItems[i];
            if (item.pageItems && item.pageItems.length)
                list = [].concat(list, getAllTextFrames(item));
            else if (item.typename=="TextFrame")
                list.push(item);
            }
            return list;
        }
    //judge color
        function judgeColor(item, isStroke){
            var isChangeColor = true;

            if(isStroke){
                var _color = item.strokeColor
            }else{
                var _color = item.fillColor
            }
            
            var _red = Math.round(_color.red);
            var _green = Math.round(_color.green);
            var _blue = Math.round(_color.blue);
            //
            var _cyan = Math.round(_color.cyan);
            var _yellow = Math.round(_color.yellow);
            var _magenta = Math.round(_color.magenta);
            var _black = Math.round(_color.black);
            //

            if(
                (_red == 0 &&_green == 0 && _blue == 0) || 
                (_red == 255 &&_green == 255 && _blue == 255) || 
                (_cyan == 93 &&_magenta == 88 && _yellow == 89 && _black == 80) || 
                (_cyan == 0 &&_magenta == 0 && _yellow == 0 && _black == 0)
            )

            isChangeColor = false;
                
            return isChangeColor;
        }

//////////////////////////


    //DefineVariables
    var blackLayers = ["a"];
    var darkGreyLayers = ["b", "c"];
    var lightGreyLayers = ["d", "e", "f"];


    //DefineFunction
        function restyleAllItemsWithin(layers, color, changeFill, changeStroke) {
            app.selection = null;
            // Iterate through our layer list:
            for (var i = 0; i < layers.length; i++) {
                // Get our target layer:
                var layer = getLayer(layers[i]);
                // If a layer wasn't found or there are no items to restyle, continue looping:
                //if (!layer || !layer.pathItems.length) continue;
                if (!layer) continue;
                else {
                    var _layerStatus = layer.visible;//Recording layer status
                    layer.visible = true;
                    //If pathitem exists in the layer
                    var listPath = getAllPathItems(layer);
                    for (var index = 0; index < listPath.length; index++) {
                        var pathItem = listPath[index];
                        
                        if (changeFill && pathItem.filled && judgeColor(pathItem, false)) {// Change pathItem fillColor when it exists
                            pathItem.filled = true;    
                            pathItem.fillColor = color;
                        }
                        if (changeStroke &&  pathItem.stroked && judgeColor(pathItem, true)) {// Change pathItem strokeColor when it exists
                            pathItem.stroked = true;   
                            pathItem.strokeColor = color;
                        }
                    }
                    ///If textFrames exists in the layer
                    var listText = getAllTextFrames(layer);
                    for (var index = 0; index < listText.length; index++) {
                        var textFrames=listText[index].textRange.characterAttributes;

                        if (changeFill && textFrames.fillColor.typename!="NoColor" && judgeColor(textFrames, false)) {// Change textFrames fillColor when it exists
                            textFrames.fillColor = color;
                        }
                        if (changeStroke && textFrames.strokeColor.typename!="NoColor" && judgeColor(textFrames, true)) {// Change textFrames strokeColor when it exists
                            textFrames.strokeColor = color;
                        } 
                    }
                    layer.visible = _layerStatus;//Restore layer state
                }
            }
        }
    

    //UsingFunction
        //just changeStroke and changeFill
        restyleAllItemsWithin(blackLayers, black, true, true);
        //just changeStroke
        restyleAllItemsWithin(darkGreyLayers, darkGrey, false, true);
        //just changeFill
        restyleAllItemsWithin(lightGreyLayers, lightGrey, true, false);

  
  

 

1 reply

Charu Rajput
Community Expert
Community Expert
January 4, 2022

Hi,

Could you please share the link from where you get this code? Because this code is not formatted correctly. Getting the orginal url of the code will help us to to help you.

Best regards
Charu Rajput
Community Expert
Community Expert
January 6, 2022

Our previous script can unify all objects of a certain layer into one color. But if there is a color in the layer that I don't want to change, then we need to exclude it
For example: if(!pathitemcolor)=color var{chang the color}

 


This can be possible.

1. You need to have list of swatches or colors that you want to skip and after that, we can check if the pathitems contain any color from your exception, then does not change the color. 

2. Another way, is something to mark items that you don't want to change color, but that involves some manual task which I don't prefer.

Do you have list of exception colors that you don't want to change for fill or stroke?

 

 

Best regards