Apply pattern swatch to object and retain pattern rotation

Community Beginner ,
Mar 11, 2022 Mar 11, 2022

Copy link to clipboard

Copied

I have a script that will apply a pattern swatch to the objects I want, but when I do, it resets any changes I have made to the scale or direction of the pattern. For example, I have a front and back object, the pattern on the front needs to be right side up and the back upside down. Is there a way apply the pattern and retain the orientation that I have previously given the object? Here is what I have:

 

var doc = app.activeDocument;

var swatchFrontIndex = doc.swatches.length - 1;
var swatchBackIndex = doc.swatches.length - 2;
var swatchFront = doc.swatches[swatchFrontIndex].color;
var swatchBack = doc.swatches[swatchBackIndex].color;
var numbOfLayers = doc.layers.length;
var designLayer = doc.layers[numbOfLayers - 1];
var numbOfItems = designLayer.pathItems.length;

 

try{

for(j = 0; j < numbOfItems; j++){

if(designLayer.pathItems[j].name == "Front"){

designLayer.pathItems[j].fillColor = swatchFront;

}

if(designLayer.pathItems[j].name == "Back"){

designLayer.pathItems[j].fillColor = swatchBack;

}


}
}catch(err) {}

TOPICS
Scripting

Views

112

Likes

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 Professional ,
Mar 11, 2022 Mar 11, 2022

Copy link to clipboard

Copied

would rotating the pattern 180 degrees work for the "Back"?

 

if so, after applying the swatch use

 

designLayer.pathItems[j].rotate (180, false);

 

Likes

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 Beginner ,
Mar 14, 2022 Mar 14, 2022

Copy link to clipboard

Copied

That would work for one instance, but I have a variety of artboards that have different angles and scales. Would there be a way to build into the script to know which scale and orientation to apply to a given artboard?

Likes

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 Professional ,
Mar 14, 2022 Mar 14, 2022

Copy link to clipboard

Copied

yeah, it can be done in different ways, depending on how your art is set up (or maybe you need a new setup suitable for automation)

 

basically we would need to pass "name", "rotation", "scale" to the script, for example if the pieces had names like "Back-180-100", you would get the pathItem name, split the string at each dash to get ["Back", "180", "100"], then you would apply the Swatch, Rotation, Scale based on those numbers. Instead of using If-Else.

 

But the above process would require renaming your pieces, if that's not doable then we would need to find a way to come up with that info based on where they are, but that would need structure. So the file might need some prepping.

Likes

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 Beginner ,
Mar 14, 2022 Mar 14, 2022

Copy link to clipboard

Copied

LATEST

I can rename the pathItems. That would work great. Thank you!

Likes

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