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

[jsx] Given gradient path item, duplicating fill to path item on another doc

Community Expert ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

Hi all, I'm working on a script where I am getting a fill gradient from a path item and duplicating to another similar path item on a separate doc (though the copy path item may be of slightly varying length. 

I tried something rudimentarily of: 

 

doc2.pathItem[0].fillColor = doc2.pathItem[0].fillColor;

 

But because the gradient info does not (and cannot) exist on doc2, the gradient copies over, but it is all white. So then I tried to read doc1's gradient info, and create a new gradient in doc2 using the following two snippets: 

 

var setGradientInfo = function(cco/*a custom object*/) {
    var gradient = cco.color.gradient;
    var gs = gradient.gradientStops;
    var n = gs.length;
    var i = 0;
    cco.gradientInfo = {
        type: gradient.type,
        stops: []
    };
    var stopInfo;
    for(i; i < n; i++) {
        stopInfo = {};
        for (var p in gs[i]) {
            stopInfo[p] = gs[i][p];
            cco.gradientInfo.stops.push(stopInfo);
        }
    }
}
var addGradient = function(colorObj) {
    var gi = colorObj.gradientInfo; //a custom object
    var grad = activeDocument.gradients.add();
    grad.type = gi.type;
    var n = gi.stops.length;
    var i = 0;
    var s, p;
    for (i; i < n; i++) {
        s = grad.gradientStops.add();
        for (p in gi.stops[i]) {
            s[p] = gi.stops[i][p];
        }
    }
    colorObj.color.gradient = grad;
}

 

 

The problem is, there are two gradient stops with a rampPoint of 0, which throws an error in the second function, saying a rampPoint can't overlap or be the same as an existing rampPoint. I tried to exclude writing the second 0 rampPoint if it already existed in the custom object, but the gradient came out incorrect (with black at the end). 

Does anyone have any thoughts on this? Am I overthinking this? Is there a way I can get the swatch of the gradient in doc1, and just duplicate that swatch to doc2, which would bring over the gradient object?

 

TIA

TOPICS
Scripting

Views

53

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 Expert ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

quote

Am I overthinking this? 

TIA


By @brianp311

 

I think you are. I'm not sure why your code is not working (you're trying to use doc2 to doc2 perhaps that's the issue). Filling a path using the color of another path's fill in another document is possible.

 

try this, destination document should be active. The gradient swatch does not exists in the destination document.

var destDoc = app.documents[0];

var sourceDoc = app.documents[1]; 

var gradientPath = sourceDoc.pathItems[0];

var destPath = destDoc.pathItems[0];

destPath.fillColor = gradientPath.fillColor;

 

if you want to bring the gradient swatch to the destination document, then copying the path does it

var destDoc = app.documents[0];

var sourceDoc = app.documents[1]; 

var gradientPath = sourceDoc.pathItems[0];

var dup = gradientPath.duplicate(destDoc, ElementPlacement.PLACEATBEGINNING);

 

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
Advisor ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

LATEST

Using your furnctions, I don't get the error you describe (you should be able to have two stops with the same ramp point); I do, however, get two extra stops, a white one at the beginning and a black one at the end.  I presume this is because a gradient has two stops by default, to which you are adding stops with gradientStops.add().  At any rate, as @CarlosCanto said, this works

 

app.documents[0].pathItems[0].fillColor = app.documents[1].pathItems[0].fillColor;

 

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