Skip to main content
Participating Frequently
June 17, 2022

P: Loose gradient effect when exporting layers with ExportType.SAVEFORWEB

  • June 17, 2022
  • 4 replies
  • 569 views

Hi~

I am a developer focus on cep and extendscript for times. I found a bug in CC2022 when exporting layers.

I want to export a layer with gradient fill from PS in scripts, sample as below:

my steps in scrip are:

1. duplicate the layer to a new document

2. trim the transparent pixels in the new document

3. save the doucument with app.activeDocument.exportDocument api

 

and below are my script:

 

function duplicate() {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putClass(app.stringIDToTypeID("document"));
    desc1.putReference(app.stringIDToTypeID("null"), ref1);
    var ref2 = new ActionReference();
    ref2.putEnumerated(app.stringIDToTypeID("layer"), app.stringIDToTypeID("ordinal"), app.stringIDToTypeID("targetEnum"));
    desc1.putReference(app.stringIDToTypeID("using"), ref2);
    desc1.putInteger(app.stringIDToTypeID("version"), 5);
    app.executeAction(app.stringIDToTypeID("make"), desc1, DialogModes.NO);
}

function trim() {
    var desc1 = new ActionDescriptor();
    desc1.putEnumerated( stringIDToTypeID( "trimBasedOn" ), stringIDToTypeID( "trimBasedOn" ), stringIDToTypeID( "transparency" ) );
    desc1.putBoolean( stringIDToTypeID( "top" ), true );
    desc1.putBoolean( stringIDToTypeID( "bottom" ), true );
    desc1.putBoolean( stringIDToTypeID( "left" ), true );
    desc1.putBoolean( stringIDToTypeID( "right" ), true );
    executeAction( stringIDToTypeID( "trim" ), desc1, DialogModes.NO );

}

function output() {
    var options = new ExportOptionsSaveForWeb();
    options.format = SaveDocumentType.PNG;
    options.PNG8 = false;
    options.transparency = true;
    options.interlaced = false;
    app.activeDocument.exportDocument(new File(Folder.desktop.absoluteURI + "/demo.png"), ExportType.SAVEFORWEB, options);}

// run those actions
duplicate();
trim();
output();

 

 

these codes works all these years from CC2014 to CC2021, but breaks when in CC2022, layer contains gradient fills, lost it's effect, the output image is blank

 

I took all my efforts to make things right but have no success.

 

I have tried these ways:

1.  replace app.activeDocument.exportDocument . api with AM executeAction( stringIDToTypeID( "export" ), desc1, DialogModes.NO );   no success.

2.  do not trim the new document, the output layer shows right. but with transparent pixesl in it.

3.  if I run those step functions isolately, it works fine.

4. I tried $.sleep(1000) to slow the process, no success.

 

I want to know if this is a bug or not, does anyone encounter with this issue and have an answer?

 

Please help...

 

I attach the script file and the sample psd, please have a try in you CC2022 to see the result

Archive.zip 

This topic has been closed for replies.

4 replies

Participating Frequently
October 10, 2023

A year has gone, the bug stays in the new release photoshop version. please see to it.

app.refresh()  did refresh the ui and fix the issue, but the refresh process stuck the main application and slow down the script process, which makes my plugin behaving badly.

Participating Frequently
June 22, 2022

Thank you for your tips, But it shows no luck.

app.refresh()

did refresh the main ui, but have no effect to the image output.

the result is the same.

 

After many tries, I can get the result excepted by rastersizing both the layer and layerStyles before trim, that is

duplicate();
try {
    // if selected layer is a group with gradient layer in it
    // we merge the group to a nomal layer
    mergeGroup();
} catch(e) {
}
// rasterize the layer to Kind.Normal
rasterize();
// the gradient FXStyle is lost, so we rasterize them 
rasterizeStyle();

trim();
// app refresh has no power here
//app.refresh();

output();

 

It's a tricky way, but works. 

Hope the Adobe quickly have this bug fixed and others may get guidence from this snippets when encounter the issue.

Community Manager
June 21, 2022

Hi @xiaoqiang101 , in the meantime adding app.refresh() before output() should force the fill to update.

 

Sam

CShubert
Community Manager
Community Manager
June 20, 2022

Thank you @xiaoqiang101 the team will start investigating this bug.

 

Cheers,