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

Change color of a particular item in the doc

Contributor ,
Mar 08, 2024 Mar 08, 2024

Hi everyone,

I'm trying to write a script that changes a swatch color of item 3 on page 1 only, but no luck. I'm getting this error "Object does not support hte property or method 'swatches'." Here's the code:

 

var doc= app.activeDocument;

var doc = doc.pages[0].pageItems.item(3).swatches.item("Turquoise RGB").remove("Purple RGB");


Can anyone help me?

Thanks,
Rogerio

TOPICS
Scripting
295
Translate
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 ,
Mar 08, 2024 Mar 08, 2024

You can change the fill color of the item with item.fillColor = app.activeDocument.swatches.itemByName("Purple RGB")

 

doc.swatches.itemByName("something").remove would delete the swatch from the document and replace all items colored by it by the passed argument in .remove

Translate
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 ,
Mar 08, 2024 Mar 08, 2024

Hi @Rogerio5C09 , Also, a pageItem doesn’t have a swatches property. If the pageItem is something like a rectangle, you could set its fillColor— the 3rd page item would be pageItems[2] :

 

var doc= app.activeDocument;
var doc = doc.pages[0].pageItems[2].fillColor = "Turquoise RGB"

 

Translate
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 ,
Mar 08, 2024 Mar 08, 2024

Not a scripted solution, but applying an Object Style to that element would allow one-point changes.

Translate
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
Engaged ,
Mar 08, 2024 Mar 08, 2024

To put what @brian_p_dts said to your code. replace...

var doc = doc.pages[0].pageItems.item(3).swatches.item("Turquoise RGB").remove("Purple RGB");

with...

doc.pages.item(0).pageItems.item(3).fillColor = doc.swatches.itemByName("Turquoise RGB");

Translate
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
LEGEND ,
Mar 08, 2024 Mar 08, 2024
quote

To put what @brian_p_dts said to your code. replace...

var doc = doc.pages[0].pageItems.item(3).swatches.item("Turquoise RGB").remove("Purple RGB");

with...

doc.pages.item(0).pageItems.item(3).fillColor = doc.swatches.itemByName("Turquoise RGB");


By @John D Herzog

 

I think "pageItems.item(2).fillColor"?

 

Translate
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
Engaged ,
Mar 08, 2024 Mar 08, 2024
LATEST

I did. Fixing the other parts, I missed that one.

Translate
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
LEGEND ,
Mar 08, 2024 Mar 08, 2024
quote

Hi everyone,

I'm trying to write a script that changes a swatch color of item 3 on page 1 only, [...]

 

By @Rogerio5C09

 

But how do you know - or rather - how can you be sure that it will always be 3rd item?

 

And, as in your code you are referring to 1st page by [0] - same principle would be for the item - as per @rob day's code - [2] for 3rd item.

 

In JavaScript, 1st item have index "0".

 

Translate
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