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

Participant ,
Jan 04, 2022 Jan 04, 2022

Copy link to clipboard

Copied

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 )  ;

 

 

TOPICS
Print and publish , Scripting , Tools

Views

498

Likes

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 3 Correct answers

Adobe Community Professional , Jan 05, 2022 Jan 05, 2022

Hi, 

If you just want to change the fill color of alreday filled items then you need to you use the following statement

if (changeFill && pathItem.filled) {

instead of

if (changeFill) {

 

So, updated version of your latest script will be 

//Define Variables
var document = app.activeDocument;
var allLayers = app.activeDocument.layers;
//
var darkGreyRGB = new RGBColor();
darkGreyRGB.red = 50;
darkGreyRGB.green = 50;
darkGreyRGB.blue = 50;

//BASE Function
//~avoid failures due to non-existent lay
...

Likes

Translate

Translate
Adobe Community Professional , Jan 07, 2022 Jan 07, 2022

Hi,

You can try the following snippet where balck and white are skip based on your document

//Define Variables
var document = app.activeDocument;
var allLayers = app.activeDocument.layers;
//
var darkGreyRGB = new RGBColor();
darkGreyRGB.red = 50;
darkGreyRGB.green = 50;
darkGreyRGB.blue = 50;

//BASE Function
//~avoid failures due to non-existent layer names.
function getLayer(layerName) {
    try {
        return document.layers.getByName(layerName);
    } catch (err) {
        return null;
  
...

Likes

Translate

Translate
Participant , Jan 10, 2022 Jan 10, 2022

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;
        dark
...

Likes

Translate

Translate
Participant ,
Jan 08, 2022 Jan 08, 2022

Copy link to clipboard

Copied

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

Likes

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
Participant ,
Jan 10, 2022 Jan 10, 2022

Copy link to clipboard

Copied

LATEST

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);

  
  

 

Likes

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