remove swatches with duplicate names from illustrator document

At work I come across a lot of documents in illustrator that have multiple identical swatches. Currently I’m staring at one that has 11 different “K=100” swatches. I’d like to write a script that goes through them and if it finds another one with the same properties, deletes it. However, I’m drawing a blank as to how to actually do it. What I’ve got so far will get the name of every swatch and put it in a list, and put the actual swatches into another list, and repeat through them. What I’m having trouble figuring out is how to check for multiple copies of the same swatch. I know how to go through the list and if the properties of the currently selected swatch are the same as a swatch found in the properties list, delete that, but if I do that, I will end up with a document containing no swatches, since they are essentially two forms of the same list. Any ideas?


tell application "Adobe Illustrator"
	tell current document
		set swatchProp to properties of every swatch
		set swatchList to every swatch
		repeat with i in swatchProp
			repeat with i in swatchList
				--processing goes here
			end repeat
		end repeat
	end tell
end tell

Before you do that there is a default Action ‘Delete all unused panel items’ that was put there to remove unwanted junk. You could call that first. You might find it needs running the twice thou. If only this app gave un-used lists like ID does. :smiley:

Hi

I agree with mark regarding the action, its something i use daily and works pretty well, however a while back i was trying something similar via a script so by all means you can have a look at it, its not complete in my opinion but i think its in the same ball park as what you want to do but you will need to adapt it for your use.

set swatchList to {}
tell application "Adobe Illustrator"
	tell current document
		set t to every swatch
		repeat with i in t
			try
				set y to properties of the color of i
				if y is not in swatchList then
					copy y to end of swatchList
				end if
			end try
		end repeat
		--return swatchList
		repeat with j in t
			try
				if properties of the color of j is not in swatchList then
					delete j
				end if
			end try
		end repeat
	end tell
end tell

Cheers

Hmm. CS2 only has “Select all unused”. However, that’s not quite what I’m looking for. I can think of about half a dozen times within the past week where I would have wanted to delete the duplicates, but not necessarily all the unused swatches.

Hey, guys. In order to avoid a logic problem, the test must compare more than just color. My example preserves the first swatch instance by deleting same color items with non-matching names. You may want to add in some additional safeguards so that you don’t delete gradient or pattern fills, which would cause a default color to be applied to objects that use it.


tell application "Illustrator CS"'s document 1 to repeat with itemVar in swatches
	try
		delete (swatches whose color is (get itemVar's color) and name is not (get itemVar's name))
	end try
end repeat