Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created August 22, 2011 13:33
Show Gist options
  • Save moluapple/1162385 to your computer and use it in GitHub Desktop.
Save moluapple/1162385 to your computer and use it in GitHub Desktop.
[Illustrator] Delete Unused Swatches
//only for mark. For work, use John Wundes's script please.
//http://www.wundes.com/JS4AI/delete_fluff.js
function deleteUnusedSwatches (doc) {
var items = doc.pageItems,
len = items.length,
sw = doc.swatches,
slen = sw.length,
arr = [],
str = null,
i = 0,
j,
temp = [],
p,
path,
isFill = (function (){
p = doc.pathItems.rectangle(0,0,100,100);
p.fillColor = sw[2].color;
doc.selection = [p];
if (sw.getSelected()[0] == sw[2]){
p.remove();
return 1
} else {
p.remove();
return 0
}
})();
for (; i < len; i ++){
doc.selection = [items[i]];
arr.push(sw.getSelected()[0]);
isFill ? (items[i].stroked && temp.push(items[i].strokeColor)) : (items[i].filled && temp.push(items[i].fillColor));
}
while (temp.length){
swatch = temp.pop();
path = doc.pathItems.rectangle(0,0,100,100);
isFill ? (path.fillColor = swatch) : (path.strokeColor = swatch);
doc.selection = [path];
arr.push(sw.getSelected()[0]);
path.remove();
}
str = arr.toString();
for (j = slen - 1; j > 1; j --){
RegExp(sw[j].name).test(str) || sw[j].remove();
}
}
deleteUnusedSwatches(app.activeDocument);
function deleteUnusedSwatches (doc) {
var items = doc.pageItems,
len = items.length,
sw = doc.swatches,
slen = sw.length,
arr = [],
str = null,
i = 0,
j;
for (; i < len; i ++){
doc.selection = [items[i]];
arr.push(sw.getSelected()[0]);
}
str = arr.toString();
for (j = slen - 1; j > 1; j --){
RegExp(sw[j].name).test(str) || sw[j].remove();
}
}
deleteUnusedSwatches(app.activeDocument);
@moluapple
Copy link
Author

Sorry, this will not work as expected. But I think it still worth some points. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment