Skip to main content
October 17, 2009
Question

Swapping fill and stroke on multiple objects

  • October 17, 2009
  • 3 replies
  • 12049 views

Wondering if there's a script out there that will automate the swapping of fill and stroke for multiple objects in a complex image. Doing it object-by-object is a bit tedious (even using 'select similar' commands).

A simplified example: I have four solid filled boxes with no stroke, each box a different color. Can I automate switching stroke and fill on each one so I end up with four non-filled boxes, each stroked with their former fill color? Perhaps there's a way to automate the sequential selection of each object and perform the swaps one at a time.

Thanks for any advice!

This topic has been closed for replies.

3 replies

Participant
September 28, 2024
app.preferences.setBooleanPreference("ShowExternalJSXWarning", false);
var v_selection = app.activeDocument.selection;

Swap(v_selection);

function Swap(selection) {
    var ob_keep = null;
    
    for (var k = 0; k < selection.length; k++) {
        var subSelelction = selection[k];
        
        if (subSelelction.typename == 'PathItem') {
            var c_fill = subSelelction.fillColor;
            var c_stroke = subSelelction.strokeColor;
            subSelelction.fillColor = c_stroke;
            if(!subSelelction.stroked) {
                subSelelction.stroked = true;
            }
            subSelelction.strokeColor = c_fill;                 
        }
        else {
            if(ob_keep == null) {
                ob_keep = new Array();
                ob_keep.push(subSelelction);
            } else {
                ob_keep.push(subSelelction);
            }           
        }
    }
    
    if (ob_keep != null) {
        for (var n = 0; n < ob_keep.length; n++) {
            if (ob_keep[n] && ob_keep[n].typename == 'GroupItem')
            {
                Swap(ob_keep[n].pageItems);
            }
            else if (ob_keep[n] && ob_keep[n].typename == 'CompoundPathItem') {
                Swap(ob_keep[n].pathItems);
            }
        }
    }
}

redraw();
sanmarimarais
Participant
February 24, 2023

thanks for the handy tip but how do I use the script? non coder here

Monika Gause
Community Expert
Community Expert
February 24, 2023

You copy the complete code of the script into a text file (it must be text, not formatted, not RTF, richt text or anything). Save this with the extension .js or .jsx and then run it through File > Scripts > Other scripts

In order to run it, make sure you meet the requirements of the script: objects need to be selected or swatches  need to be highlighted, or whatever.

sanmarimarais
Participant
February 27, 2023

Thanks for this!

Participating Frequently
October 24, 2009

Save this script with a name you desire with the .jsx extension in Illustrator Folder:Presets:Scripts:.

Run the script ... it will swap the fill/color for all the path items in current document.

Cheers.

var v_selection = app.activeDocument.pathItems;
SwapFillStroke(v_selection);

function SwapFillStroke(objSel) {
    for(k = 0; k < objSel.length; k++){
        var subSel = objSel;
         var c_fill = subSel.fillColor;
        var c_stroke = subSel.strokeColor;
        subSel.fillColor = c_stroke;
        if(!subSel.stroked)
             subSel.stroked = true;
        subSel.strokeColor = c_fill;        
    }
}

redraw();

This one should do the same on a selection(as was originally intended):

var v_doc = app.activeDocument;
var v_selection = v_doc.selection;

swapFillStroke(v_selection);

function swapFillStroke(objSel) {
    var ob_keep = null;
    for(k = 0; k < objSel.length; k++){
           var subSel = objSel;
            if(subSel.typename == 'PathItem'){
                  var c_fill = subSel.fillColor;
                  var c_stroke = subSel.strokeColor;
                  subSel.fillColor = c_stroke;
                  if(!subSel.stroked)
                       subSel.stroked = true;
                  subSel.strokeColor = c_fill;                 
            } else {
                  if(ob_keep==null) {
                       ob_keep = new Array();
                       ob_keep.push(objSel);
                  } else {
                      ob_keep.length +=1;
                      ob_keep.push(objSel);
                 }           
           }
    }
    if(ob_keep !=null){
        for(n = 0; n < ob_keep.length; n++) {
            if(ob_keep.typename == 'GroupItem') swapFillStroke(ob_keep.pageItems);
            else if (ob_keep.typename == 'CompoundPathItem') swapFillStroke(ob_keep.pathItems);
        }
      }
}

redraw();

Message was edited by: sonicDream

October 30, 2009

What a lovely script! Thank you. I erased the notation line (19) as it wasn't digesting the colon but it does just what I was hoping. Thank you for your time and consideration. It's most generous of you to share.