Skip to main content
AlexBuisse
Participating Frequently
June 24, 2024
Question

Script to change fillColor in object appearance attributes

  • June 24, 2024
  • 2 replies
  • 955 views

Hi,

I have a series of text objects that have special appearances applied to them (outline stroke, ellipse and fill) in order to make them look like numbers within colored circles. There is another series of paths that have matching names. I wrote a script to iterate over each number and make it take the fillColor of the corresponding path, but can't seem to change the fillColor attribute within the appearance panel.

 

Here is the relevant snippet of code:

for (var n = 0; n < doc.layers["lines"].layers["numbers"].pageItems.length; n++) {
  var obj = doc.layers["lines"].layers["numbers"].pageItems[n];
  var id = Number(obj.contents);
  newCol = findCol(id);
  if (newCol != null) {
    obj.fillColor = newCol;
  } else {
    alert("Found nothing for id " + id);
  }
}

And here is the relevant part of the file showing the object structure:

I know the API allows only very limited access to appearance attributes, but was hoping fillColor would be basic enough to be able to be accessed.

 

Thanks in advance for any help!

This topic has been closed for replies.

2 replies

Kurt Gold
Community Expert
Community Expert
June 24, 2024

It would be helpful if you could share a sample Illustrator file with some specific instructions about the task.

AlexBuisse
Participating Frequently
June 24, 2024

Sure thing, here is a sample file (without the base JPG, to keep things lightweight), and the current state of my script, renamed to txt so I could upload it here.

 

It only has a handful of numbers, but most files have 100+ of them. The idea is to take each layer in the "lines/numbers" layer, and match its ID number with the corresponding line in "lines/routes", get the color there, then apply it in the fill property of the appearance panel. Let me know if you need any more info.

AlexBuisse
Participating Frequently
June 24, 2024

Hum, the forum is complaining that the content type of my file is application/postscript, which does not match its contents, and won't let me upload it. I have it here on a third party server: https://www.playbook.com/s/alex-buisse/29tTHomkHNnLdNLbz5zxmJve?assetToken=tcwJ1TRU5yyZr1NCptUbsDFx. Let me know if that works, or if there is another way to share the file.

pixxxelschubser
Community Expert
Community Expert
June 24, 2024

One of the few possibilities: 

Create a graphic style. Assign this style to your object in Script.

AlexBuisse
Participating Frequently
June 24, 2024

Thanks, I saw this mentioned in other forum threads, but I need to assign a new color every time. Is it possible to change the fillColor of a given graphic style before applying it?