• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script to change fillColor in object appearance attributes

New Here ,
Jun 23, 2024 Jun 23, 2024

Copy link to clipboard

Copied

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:

Screenshot 2024-06-23 at 21.32.39.png

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!

TOPICS
Scripting

Views

477

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

One of the few possibilities: 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 24, 2024 Jun 24, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 25, 2024 Jun 25, 2024

Copy link to clipboard

Copied

Thanks for the sample file. It helps to understand the task.

 

Currently, I don't have a scripting solution, but perhaps two different approaches that may or may not be suitable.

 

To verify, you may want to elaborate further:

 

1. Are the labels with the numbers always pre-built in each file and are they already located at the intended position?

 

2. Or do you create the labels in each document individually and then move them to the intended position?

 

3. Are all or almost all labels usually located where the open stroked paths start or end? Or is that rather variable?

 

In any case, it may help to share one of your files that contain more than 100 labels. Just to get a better overview.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 25, 2024 Jun 25, 2024

Copy link to clipboard

Copied

Thanks for taking a look. The numbers are not pre-built, they are created and moved manually. They are not necessarily at the start or end of the paths (though in practice it happens more often than not). Here is another file I just completed, with 114 lines and the numbers already at the right colors: https://www.playbook.com/s/alex-buisse/f5poiiHgTXA3F4Lyx2N6YDWF?assetToken=BSSeqvfvyhvSfbujiP4YGRdm 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 25, 2024 Jun 25, 2024

Copy link to clipboard

Copied

Thanks for the file.

 

Some outstanding scripters may prove me wrong, but based on this example I'd say that it's not possible to completely automate the task. This is mainly manual work. At least I do not see reliable relationships between the paths and the labels, such that they could be "colour-tied".

 

Some parts might be automated with brushes and there may be another way that would use transparency blending modes. But there is no instant way, as far as I can see.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 25, 2024 Jun 25, 2024

Copy link to clipboard

Copied

LATEST

Thanks for your assessment. There is a relationship: the id in each number label, obtained via 

Number(obj.contents)

 where obj is a number label, matches the beginning of a line label, which I do get by just iterating over all the lines and doing a little regex in the layer name:

for (var i = 0; i < routes.pageItems.length; i++) {
    var n = routes.pageItems[i].name;
    var ids = n.match("^([0-9]+): ");
    if (ids != null) {
      id = Number(ids[1]);
      if (id == targetID) { return routes.pageItems[i].strokeColor; }
    }
  }

That part correctly gives me the right color with which to color the number label. The part where I am stuck is how to actually change the fill color of the label.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines