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!
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
else if (ob_keep
}
}
}
redraw();
Message was edited by: sonicDream
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.
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.
Copy link to clipboard
Copied
Error in Line 2..pls fix
Thanks
Copy link to clipboard
Copied
Did you save the file as plain text and not RTF?
Copy link to clipboard
Copied
i tried on AI CC 2021, and the script doesn't work
Copy link to clipboard
Copied
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()
Copy link to clipboard
Copied
thanks for the handy tip but how do I use the script? non coder here
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks for this!
Copy link to clipboard
Copied
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();