Copy link to clipboard
Copied
Hi Community
I am trying to get my textframes into the registration color form my swatches, but VS shows me this messages:
I already tried:
text.textRange.characterAttributes.fillColor = doc.swatches[1].color;
text.textRange.characterAttributes.fillColor = doc.swatches['[Registration]'].color;
text.textRange.fillColor = doc.swatches[1].color;
text.textRange.fillColor = doc.swatches['[Registration]'].color;
The image above is just to show how I tried to look for an alert msg and what does it gets but shows error.
can anybody help me? Thank you
2 Correct answers
I'm not sure why the code in your screen shot is getting that error. Perhaps the document has the registration swatch named differently (different locales name is differently). Try this function and see if it works:
var doc = app.activeDocument;
var regColor = getRegistrationSwatch(doc);
/**
* Returns the document's Registration swatch.
* @Param {Document} doc - an Illustrator Document
* @Returns {Swatch}
*/
function getRegistrationSwatch(doc) {
for (var i = 0; i < doc.swatches.length
...
I did it! I just changed somethign in your function, this line:
return doc.swatches[i];
to this:
return doc.swatches[i].color;
Then, in the code I did:
text.textRange.characterAttributes.fillColor = getRegistrationSwatch(doc);
Explore related tutorials & articles
Copy link to clipboard
Copied
I'm not sure why the code in your screen shot is getting that error. Perhaps the document has the registration swatch named differently (different locales name is differently). Try this function and see if it works:
var doc = app.activeDocument;
var regColor = getRegistrationSwatch(doc);
/**
* Returns the document's Registration swatch.
* @Param {Document} doc - an Illustrator Document
* @Returns {Swatch}
*/
function getRegistrationSwatch(doc) {
for (var i = 0; i < doc.swatches.length; i++)
if (
doc.swatches[i].color.typename == 'SpotColor'
&& doc.swatches[i].color.spot.colorType == ColorModel.REGISTRATION
)
return doc.swatches[i];
};
- Mark
Copy link to clipboard
Copied
Pretty sure it says registration, BUT of course I will try this code! I triedusing another colors (not index 0 which is none nor index 1 which is registration) and it worked on those ones, thank you so much let me test tomorrow
Copy link to clipboard
Copied
@m1bgood morning!
I tried the following code just to see if I can get a response in return:
var doc = app.activeDocument;
function getRegistrationSwatch(doc) {
for (var i = 0; i < doc.swatches.length; i++)
if (
doc.swatches[i].color.typename == 'SpotColor'
&& doc.swatches[i].color.spot.colorType == ColorModel.REGISTRATION
)
return doc.swatches[i];
};
alert(getRegistrationSwatch(doc));
and I got this:
Now in my code I try to do this:
var rect = doc.pathItems.rectangle(- y + h + space, x, w, h);
var text = doc.textFrames.areaText(rect);
text.contents = "SIZE: " + Math.round(anchoin * 100) / 100 + "'' W x " + Math.round(altoin * 100) / 100 + "'' H";
text.textRange.size = fontsize;
text.textRange.textFont = textFonts[font];
var regColor = getRegistrationSwatch(doc);
text.textRange.characterAttributes.fillColor = regColor;
According to the pretty well logic you did in the code I should get a textframe with fillcolor=registration... but I get the same error message.
swatches looks like this:
it's weird that this is happening, it should work 😕
Copy link to clipboard
Copied
I did it! I just changed somethign in your function, this line:
return doc.swatches[i];
to this:
return doc.swatches[i].color;
Then, in the code I did:
text.textRange.characterAttributes.fillColor = getRegistrationSwatch(doc);
Copy link to clipboard
Copied
You've got it working (well done!!), but you changed the wrong part for good semantics. When you set a fillColor or strokeColor, it must be a Color object, not a Swatch object.
But the getRegistrationSwatch function I wrote returns a Swatch because that's is what is says it does. Once you have the registration Swatch you just ask for its color property, which will be a Color object. So I suggest you don't change the getRegistrationSwatch function, but instead use this:
text.textRange.characterAttributes.fillColor = getRegistrationSwatch(doc).color;
It seems like a small point of order, but since a lot of programming is about knowing exactly what objects are working with, I think it is important.
- Mark
P.S. Alternatively you could rename the function to getRegistrationColor. That would be fine, too, but not my preference because sometimes you want a Swatch.
Copy link to clipboard
Copied
Thank you so much! I'll change it like you said here.

