Skip to main content
Inspiring
March 8, 2024
Question

Change color of a particular item in the doc

  • March 8, 2024
  • 5 replies
  • 403 views

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

This topic has been closed for replies.

5 replies

Robert at ID-Tasker
Legend
March 8, 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".

 

John D Herzog
Inspiring
March 8, 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");

Robert at ID-Tasker
Legend
March 8, 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"?

 

John D Herzog
Inspiring
March 8, 2024

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

James Gifford—NitroPress
Legend
March 8, 2024

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

rob day
Community Expert
Community Expert
March 8, 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"

 

brian_p_dts
Community Expert
Community Expert
March 8, 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