2

I have an Illustrator document with a lot of objects. All the objects are black, same swatch.

I have a swatch group with a few dozen coloured swatches. I wish to apply these swatches to all the many black objects. It doesn’t have to be in any particular order, but each object should be coloured individually—I don’t just want to change black into some other colour, but to dozens of other colours. What I really want to avoid is having to select each object individually and then applying a different swatch to each.

I’ve never quite managed to get my head properly around the Recolor Artwork feature in Illustrator, and I cannot for the life of me get it to do anything like this. The “New” box in the Recolor Artwork dialogue box appears to accept only one colour.

Is there some way to do this using the Recolor Artwork feature? Or using some other feature that I’m not aware of? Bonus points if there’s a way to control the order in which the colours are applied; e.g., specifying an order and then click on each object in turn to apply each swatch being ‘iterated’ through in that order.

(I would prefer to avoid having to script it, but I will if there’s no other way.)

2 Answers 2

3

This script RandomSwatchesFill.jsx does exactly that, selecting all the shapes and all the swatches:

script

mySelection = app.activeDocument.selection;
myDoc = app.activeDocument;
if (mySelection instanceof Array)
{
 selSwatches = myDoc.swatches.getSelected();

if(selSwatches.length != 0)
    for (i=0; i<mySelection.length; i++)
    {
        if(mySelection[i].typename == "PathItem" || mySelection[i].typename == "CompoundPathItem")
        {
            selItem = mySelection[i];
            selItem.filled = true;

            swatchIndex = Math.round( Math.random() * (selSwatches.length - 1 ));

            if(selItem.typename == "PathItem")
                selItem.fillColor = selSwatches[swatchIndex].color;
            else
                selItem.pathItems[0].fillColor = selSwatches[swatchIndex].color;

        }
    }
 }

If you download it change the extension from .js to .jsx

4
  • Thank you for that! Accepting that there is probably no GUI-based way to do this easily, I adapted this script to use a user-defined swatch group (instead of selected swatches) and iterate through them in order (instead of choosing one at random). Works perfectly well! Commented Aug 7, 2018 at 17:10
  • @JanusBahsJacquet a script can have a gui
    – joojaa
    Commented Aug 7, 2018 at 18:00
  • @joojaa I meant the built-in Illustrator GUI-driven functions available through menus and panels, obviously. Commented Aug 7, 2018 at 18:02
  • @JanusBahsJacquet you can make the script appear in a menu. You cant expect illustrator to do much things since it has no concept of parametric design.
    – joojaa
    Commented Aug 7, 2018 at 19:12
0

The script posted/linked to by Danielillo does most of what I was looking for. I used it as the basis for the following script, which asks the user for the name of the swatch group to use, and then iterates through the selected objects and the swatches in the named swatch group, applying the current colour to the current object.

(The objects I had needed to have their stroke, rather than their fill, changed, so I also changed that. To colour the fill instead of the stroke, replace strokeColor with fillColor.)

var selected = app.activeDocument.selection;
var doc = app.activeDocument;
var swatchIndex = 0;

if (selected instanceof Array) {
    title = prompt("Please enter the name of the swatch group containing the colours to apply.", "");

    if (title) {
        swatches = doc.swatchGroups.getByName(title).getAllSwatches();

        if (swatches.length != 0) {
            for (i = 0, j = selected.length; i < j; i++) {
                item = selected[i];
                if (swatchIndex >= swatches.length) swatchIndex = 0;

                if (item.typename == "CompoundPathItem") {
                    item = item.pathItems[0];
                }

                if (item.typename == "PathItem") {
                    item.strokeColor = swatches[swatchIndex].color;
                    swatchIndex++;
                }
            }
        }
    }
}

(The script does not handle exceptions, so if you type in an incorrect swatch group name, you’ll probably just get errors thrown at you.)

1
  • Note, your adaptation also switched it to stroke color, not fill color.
    – Welz
    Commented Aug 7, 2018 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.