Has anyone seen any way to call “Select all unused” (swatches) in Illustrator CS4 straight from Applescript? I don’t see a way to do it with the dictionary, and I would rather not call an action to do this.
What’s your hesitation with calling an Action? There should be a default Action to remove unused items that is installed with CS4.
Theres a couple reasons why I don’t want to call an action. The first reason is that the action doesn’t completely clean out the swatch palette (The colors left in the swatch groups are not deleted even if unused), and the second is I don’t want to assume everyone that will be running this script has the action. I like to remove all extra things that can make a script break if possible.
The good news is, I’ve figured out how to delete all 4-CP swatches, gradients and patterns. Here’s the code if anyone is interested.
tell application "Adobe Illustrator"
activate
set docRef to the current document
tell docRef
set swatchCount to get count of swatches
set deleteList to {}
repeat with i from 1 to swatchCount
set colorType to color of swatch i
set colorClass to class of colorType
set colorName to name of swatch i
--finds 4-CP
if colorClass is CMYK color info then
set end of deleteList to colorName
end if
--finds gradients
if colorClass is gradient color info then
set end of deleteList to colorName
end if
--finds patterns
if colorClass is pattern color info then
set end of deleteList to colorName
end if
--excludes spot colors
if colorClass is spot color info then
end if
--excludes "none"
if colorClass is no color info then
end if
end repeat
set deleteCount to count of items in deleteList
repeat with i from 1 to deleteCount
set deleteThisSwatch to item i of deleteList
delete swatch deleteThisSwatch
end repeat
end tell
end tell
I’m happy to have accomplished this, but it doesn’t entirely meet my needs. If my customer gives me a file where they used a gradient or pattern that uses a spot color, it still gets deleted, because it’s class is gradient (or pattern) no matter the colors used within. Also, if my customer uses a gradient or pattern, I think I would like to go ahead and keep it just to be safe, even if it is 4-cp, but I do want to delete the unused patterns and gradients.
Is there a way to find only used swatches? I was thinking to do so, I would have to examine each element on the page. Do you think I’m on the right track? It seems like looking at each element could become very slow depending on the artwork…
Ever get anywhere with this? I’m trying to do something similar (deleting unused swatches from files in CS5). I’m still trying to figure out how to repeat through the swatches to determine which ones are used and which are not.
Hi,
calling a action¿
tell application "Adobe Illustrator"
activate
tell current document
do script "delete unused swatches" from "Standardaktionen" --German version
end tell
end tell
I am trying to select all the colours that are actually used in a document and use them in a legend panel.
I can select all the colours in the swatch panel and return the result, but thats not really what i want. I only need colours that are actually used in the document returned.
Here is the script I have so far, I would really appreciate if someone could give me a hand I am pretty new to applescript and want to learn:
tell application "Adobe Illustrator"
set this_document to current document
set my_inknames to the name of swatches of this_document as list
set my_ink_list to result as string
end tell
Hi Guys
This may not be the full solution but its a script i picked up from somewhere (Not sure where)
It deletes unused swatch colors so if it doesn’t do what you want its a great step in the right direction
and may give you some ideas moving forward.
The trouble with getting unused colors or grabbing the used colours is that it involves a bit of checking and illustrator docs can get pretty busy for a repeat loop with fills and strokes etc…
Hope this helps anyhow.
tell application "Adobe Illustrator" to activate -- just so it doesn't seem distracting, tho no progress indicator
deleteUnusedSwatches(1)
beep 2
on deleteUnusedSwatches(doc)
tell application "Adobe Illustrator"
-- doc might be a document number or name, or it might be a document reference
if class of doc is integer or class of doc is string then set doc to document doc
-- determine quickest way to delete
if (count of page items of doc) > (count of swatches of doc) then -- more page items than swatches, delete by swatches
my deleteUnusedSwatchesForSwatches(document 1)
else -- more swatches than page items or equal, delete by page items
my deleteUnusedSwatchesForPageItems(document 1)
end if
end tell
end deleteUnusedSwatches
on deleteUnusedSwatchesForSwatches(doc)
tell application "Adobe Illustrator"
if class of doc is integer or class of doc is string then set doc to document doc
tell doc
-- don't get rid of the basics
set exemptSwatches to {"[None]", "[Registration]", "White", "Black"}
set s to swatches
set unusedSwatches to {}
repeat with i from 1 to count of s
set thisSwatch to item i of s
-- see if color of thisSwatch is used by any items
set swatchColor to color of thisSwatch
if (not (exists (first path item whose fill color is swatchColor))) ¬
and (not (exists (first path item whose stroke color is swatchColor))) ¬
and (not (exists (first text frame whose fill color of text of it is swatchColor))) ¬
and (not (exists (first text frame whose stroke color of text of it is swatchColor))) ¬
and name of thisSwatch is not in exemptSwatches then ¬
set end of unusedSwatches to thisSwatch
end repeat
end tell
delete unusedSwatches
end tell
end deleteUnusedSwatchesForSwatches
on deleteUnusedSwatchesForPageItems(doc)
tell application "Adobe Illustrator"
if class of doc is integer or class of doc is string then set doc to document doc
tell doc
set itemList to every page item whose class is not raster item and class is not placed item -- not gonna work in cs2
set usedColors to {}
set usedSwatches to {"[None]", "[Registration]", "White", "Black"}
repeat with i from 1 to count of itemList
set thisItem to item i of itemList
if class of thisItem is not text frame then
try
set end of usedColors to fill color of thisItem
end try
try
set end of usedColors to stroke color of thisItem
end try
else if class of thisItem is text frame then
set end of usedColors to fill color of text of thisItem
set end of usedColors to stroke color of text of thisItem
end if
end repeat
repeat with thisColor in usedColors
try
set usedSwatchName to (name of first swatch whose color is thisColor)
if usedSwatchName is not in usedSwatches then set end of usedSwatches to usedSwatchName
end try
end repeat
delete (every swatch whose name is not in usedSwatches)
end tell
end tell
end deleteUnusedSwatchesForPageItems
Pidge - your script is excellent and I learned a lot from it.
Just a note, there are a few ways it can miss spot colors - the results of your script do not always agree with the more accurate methods of using the user interface to “Select all Unused” and then delete, or calling the action:
do script "Delete Unused Panel Items" from "Default Actions" without dialogs
For example, it excludes raster items, but greyscale or bitmap raster items can be assigned spot colors. It will also ignore the spot colors inside placed DCS documents. These sorts of items may be rare occurrences for most users, but they are fairly common in the screen printing industry.
Just an FYI for people using this script.
Model: Hackintosh i5 2500k/16GB/ 64GB Flash+1TB RAID0/Radeon 6850
AppleScript: 2.2.1
Browser: Safari 534.55.3
Operating System: Mac OS X (10.7)