2

I want a script that will change a swatch in my Illustrator document, where this particular swatch is named “strap”.

I looked over the Internet, and didn't find anything that worked, but I noticed people seem to string together .spot and .color in ways I don't understand. So for about twenty minutes I just randomly put together combinations of spot and color, and I found something that works. But I don't understand. Why is this “spot” necessary at all? How does it make sense to repeat “color” twice? Here is the code:

var doc = app.activeDocument;
var swatches = doc.swatches;
var my_color = doc.swatches.getByName("straps");

my_color.color.spot.color.red = 41;
my_color.color.spot.color.blue = 62;
my_color.color.spot.color.green = 78;
3
  • IIts because the spot color type is defined a bit wierdly so that they dont need to redefine color object just for spot.
    – joojaa
    Dec 1, 2021 at 5:01
  • Amongst other things it allows assigning spot color with spot color so you could have color.spot.color.spot.color.spot.color too.
    – joojaa
    Dec 1, 2021 at 5:07
  • 1
    To avoid having to use random combinations, take a look at the Adobe Illustrator CC Scripting Reference which describes in detail the various objects that can be accessed in script, and their properties.
    – pbasdf
    Dec 1, 2021 at 13:37

1 Answer 1

1

I would urge you to not to think of the program as a natural language (text is not what gets executed). But rather a object subobject lookup. It makes more sense thatway. So lets unpack it for you:

my_color - is a swatch object (better call it my_swatch?)
    color - property of the swatch, containing a color object

Now you have loaded color into a unnamed handle and that:

unnamed - color object that happens to be of type spot
    spot - property of unnamed that contains a color object

And that object in turn is unnamed and happens to be a RGBColor object...

The name string makes sense when you look at the objects contained. But since it is not aimed to be natural language it does look wierd. But it is actually shorthand object lookup so anything is sane.

Its a bit like having a building complex out if many buildings called building a and then asked to go to building f. Or having a box within a box for example.

3
  • I think I mostly understand. But reading what you have written, is this organization of information redundant? You start off with a swatch object, and it has a property of being a color object, and the color object has the property of a spot type, which has the property of being a color object. I don't see the necessity of bringing a spot type into the equation. Why can't the designers of this system just make a swatch object have the property of being a color object, and leave it there? Thanks for you patience.
    – Chris
    Dec 2, 2021 at 18:37
  • @Chris because when you make a bigger software you want to have hard typing and extending the class sor a trivial thing that you may build out of a composition is not considered kosher
    – joojaa
    Dec 2, 2021 at 21:41
  • It sort of saves you from having to maintain 2 separate things. But really it also depends on your style guide and language. Inveither case dont think what is simplest to you but rather simplest for a team maintaining it for 50 years. In either case the interdiction on your part is trivial
    – joojaa
    Dec 2, 2021 at 21:56

Your Answer

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

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