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

Swapping fill and stroke on multiple objects

Guest
Oct 17, 2009 Oct 17, 2009

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

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
Guest
Oct 30, 2009 Oct 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.

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

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

Thank you for saving my headache sonicDream.

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

Error in Line 2..pls fix

Thanks

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

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

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

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

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

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

 

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 Beginner ,
Feb 24, 2023 Feb 24, 2023

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

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 ,
Feb 24, 2023 Feb 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.

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 Beginner ,
Feb 26, 2023 Feb 26, 2023

Thanks for this!

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
New Here ,
Sep 27, 2024 Sep 27, 2024
LATEST
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();
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