Swapping fill and stroke on multiple objects

Community Beginner ,
Oct 17, 2009 Oct 17, 2009

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

8.3K

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
Contributor ,
Oct 24, 2009 Oct 24, 2009

Copy link to clipboard

Copied

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

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
Community Beginner ,
Oct 30, 2009 Oct 30, 2009

Copy link to clipboard

Copied

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.

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
New Here ,
May 06, 2017 May 06, 2017

Copy link to clipboard

Copied

Six and a half years later, still works perfectly. But still isn't an option in Illustrator!

Thank you for saving my headache sonicDream.

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
Community Beginner ,
Dec 30, 2017 Dec 30, 2017

Copy link to clipboard

Copied

Error in Line 2..pls fix

Thanks

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
Adobe Community Professional ,
Dec 30, 2017 Dec 30, 2017

Copy link to clipboard

Copied

Did you save the file as plain text and not RTF?

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
New Here ,
Nov 30, 2020 Nov 30, 2020

Copy link to clipboard

Copied

i tried on AI CC 2021, and the script doesn't work

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
Enthusiast ,
Nov 30, 2020 Nov 30, 2020

Copy link to clipboard

Copied

LATEST

First script:

 

var v_selection = app.activeDocument.pathItems;
SwapFillStroke(v_selection);
function SwapFillStroke(objSel) {
  for(k = 0; k < objSel.length; k++){
    var subSel = objSel[k];
    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();

 

 

Second script:

 

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[k];
    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[k]);
        } else {
          ob_keep.length +=1;
          ob_keep.push(objSel[k]);
        }           
    }
  }
  if(ob_keep !=null){
    for(n = 0; n < ob_keep.length; n++) {
      if(ob_keep[n].typename == 'GroupItem') swapFillStroke(ob_keep[n].pageItems);
      else if (ob_keep[n].typename == 'CompoundPathItem') swapFillStroke(ob_keep[n].pathItems);
    }
  }
}
redraw()

 

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