Copy link to clipboard
Copied
I am having a bear of a time changing the fill color of a polygon object. The polygon object has a name of "Ribbon". Here is the code I am using to try and change it along with the errors I receive in the comments:
var gDoc = app.activeDocument;
var myPages = gDoc.pages;
myGroupItems = myPages[0].allPageItems;
if (gDoc.pageItems.item("Ribbon").isValid) {
gDoc.pageItems.item("Ribbon").fillColor = templateDoc.colors.item("myTabColor").colorValue; // Invalid value for set property 'fillColor'. Expected Swatch or String, but received (34, 0, 9, 0).
gDoc.pageItems.item("Ribbon").fillColor = templateDoc.colors.item("myTabColor") // Invalid value for set property 'fillColor'. Expected Swatch or String, but received Color
gDoc.pageItems.item("Ribbon").fillColor = templateDoc.swatches.item("myTabColor"); // Invalid value for set property 'fillColor'. Expected Swatch or String, but received Color.
}
On the command line of the debugger I tried the following:
templateDoc.swatches.item("myTabColor")
[object Swatch]
templateDoc.swatches.item("myTabColor").isValid
true
So you can see that templateDoc.swatches.item("myTabColor") returns a swatch object.
So why does gDoc.pageItems.item("Ribbon").fillColor = templateDoc.swatches.item("myTabColor"); not work if I am assigning a swatch value?
I have tried so many different variations and none have worked. I never thought it would be this hard to change the color of a polygon. š
Thank you in advance for any help provided.
If you select the polygon on the page you could best check what kind of code with assigning a color to property fillColor will work. After you mastered this, look for the other issue, how you can find the named page item in your document.
Hints:
var myColor = app.documents[0].colors.itemByName("myTabColor");
alert( myColor.isValid );
// Further when the polygon object on the page is selected:
app.selection[0].fillColor = myColor;
If there is only one single page item named "Ribbon" that is
...Copy link to clipboard
Copied
If you select the polygon on the page you could best check what kind of code with assigning a color to property fillColor will work. After you mastered this, look for the other issue, how you can find the named page item in your document.
Hints:
var myColor = app.documents[0].colors.itemByName("myTabColor");
alert( myColor.isValid );
// Further when the polygon object on the page is selected:
app.selection[0].fillColor = myColor;
If there is only one single page item named "Ribbon" that is not nested in a structure like a group or is anchored you can get it like this:
var myItem = app.documents[0].pageItems.itemByName("Ribbon");
// Did you expect the next line?
alert( myItem.isValid );
Regards,
Uwe Laubender
( Adobe Community Professional )
Copy link to clipboard
Copied
Hi Uwe,
Thank you for the information! I was able to successfully change the polygons fill color by selecting it and then changing the fill color of the selection.
So I can hopefully solve these types of problems on my own in the future, can you explain to me why I need to select the polygon to assign the fill color? Why can I not choose the polygon directly and assign the fill color? Something like:
app.activeDocument.polygons.itemByName("Ribbon").fillColor
That property in the object model is listed as read/write.
Thank you again for all your help!
Copy link to clipboard
Copied
There is no need to select an object.
It's just the easiest method for testing simple sample code.
Regards,
Uwe Laubender
( Adobe Community Professional )