6 Replies Latest reply on Feb 20, 2016 5:53 AM by carlob83680710

    script saving png before psd has had time to update.

    carlob83680710 Level 1

      Hi all,

       

      I use a script to resize a file and export as png and save. Since updating photoshop to 2015 CC the script exports the image ok but gradients are not scaled.

       

      I think the script is actually saving the image before it has time to render the gradients properly when the psd is resized

       

      Is there a way for the script to wait for the psd to render the changes and then save the file? so essentially a callback for the resizeImage() function?

       

       

      thanks

      Jon

       

      var doc = app.activeDocument;
      var docName = doc.name.replace(/\.[^\.]+$/, '');
      var docPath = doc.path;
      var scale = "200%";
      var folderName = "preview";
      var extensionName = "_2x.png";
      
      
      // where the action happens
      function exportScaledPNG() {
          
          
          // resize the document
          doc.resizeImage(scale, scale, doc.resolution, ResampleMethod.BICUBIC);
          
          // check if a folder has already been created or not - create if not
          var x2Folder = Folder(docPath + "/" + folderName + "/x2");
          if(!x2Folder.exists) x2Folder.create();
          
          // check if the PNG already exists or not - delete if so
          pngFile = File(docPath + "/" + folderName + "/x2/" + docName + extensionName); 
          if(pngFile.exists) pngFile.remove();
          
          // save the PNG
          pngSaveOptions = new PNGSaveOptions();
          pngSaveOptions.compression = 9;
          doc.saveAs(pngFile, pngSaveOptions, true, Extension.LOWERCASE)
          
          // undo the history and purge
          doc.activeHistoryState = doc.historyStates[doc.historyStates.length-2];  
          app.purge(PurgeTarget.HISTORYCACHES);
          
          // save the document (to avoid having to do it manually)
          doc.save();
      }
      
      
      // wrap in a try/catch for errors
      try {
        exportScaledPNG();
      } catch (e) {
          if (DialogModes.NO != app.playbackDisplayDialogs) {
              alert(e + " : " + e.line);
          }
      }
      
        • 1. Re: script saving png before psd has had time to update.
          Chuck Uebele Adobe Community Professional & MVP

          You can try adding a redraw in you script before the save:

           

          cTID = function(s) { return app.charIDToTypeID(s); };
          var desc = new ActionDescriptor();
          desc.putEnumerated(cTID("Stte"), cTID("Stte"), cTID("RdCm"));
          executeAction(cTID("Wait"), desc, DialogModes.NO);
          

           

          If that doesn't work, and it doesn't on some computers, you can try to set the playback to step mode then back again to accelerated:

           

          function setActionToStep(){
              var idsetd = charIDToTypeID( "setd" );
                  var desc1 = new ActionDescriptor();
                  var idnull = charIDToTypeID( "null" );
                      var ref1 = new ActionReference();
                      var idPrpr = charIDToTypeID( "Prpr" );
                      var idPbkO = charIDToTypeID( "PbkO" );
                      ref1.putProperty( idPrpr, idPbkO );
                      var idcapp = charIDToTypeID( "capp" );
                      var idOrdn = charIDToTypeID( "Ordn" );
                      var idTrgt = charIDToTypeID( "Trgt" );
                      ref1.putEnumerated( idcapp, idOrdn, idTrgt );
                  desc1.putReference( idnull, ref1 );
                  var idT = charIDToTypeID( "T   " );
                      var desc2 = new ActionDescriptor();
                      var idperformance = stringIDToTypeID( "performance" );
                      var idperformance = stringIDToTypeID( "performance" );
                      var idstepByStep = stringIDToTypeID( "stepByStep" );
                      desc2.putEnumerated( idperformance, idperformance, idstepByStep );
                  var idPbkO = charIDToTypeID( "PbkO" );
                  desc1.putObject( idT, idPbkO, desc2 );
              executeAction( idsetd, desc1, DialogModes.NO );
          }//end function 
          
          // function to return action to accelerated mode - no refresh of screen for each step.==
          function setActionToAcc(){
              var idsetd = charIDToTypeID( "setd" );
                  var desc3 = new ActionDescriptor();
                  var idnull = charIDToTypeID( "null" );
                      var ref2 = new ActionReference();
                      var idPrpr = charIDToTypeID( "Prpr" );
                      var idPbkO = charIDToTypeID( "PbkO" );
                      ref2.putProperty( idPrpr, idPbkO );
                      var idcapp = charIDToTypeID( "capp" );
                      var idOrdn = charIDToTypeID( "Ordn" );
                      var idTrgt = charIDToTypeID( "Trgt" );
                      ref2.putEnumerated( idcapp, idOrdn, idTrgt );
                  desc3.putReference( idnull, ref2 );
                  var idT = charIDToTypeID( "T   " );
                      var desc4 = new ActionDescriptor();
                      var idperformance = stringIDToTypeID( "performance" );
                      var idperformance = stringIDToTypeID( "performance" );
                      var idaccelerated = stringIDToTypeID( "accelerated" );
                      desc4.putEnumerated( idperformance, idperformance, idaccelerated );
                  var idPbkO = charIDToTypeID( "PbkO" );
                  desc3.putObject( idT, idPbkO, desc4 );
              executeAction( idsetd, desc3, DialogModes.NO );
          }//end function
          

           

          And if all those don't work, maybe a sleep command.

           

          $.sleep(7000)
          
          1 person found this helpful
          • 2. Re: script saving png before psd has had time to update.
            carlob83680710 Level 1

            Hi Thanks Chuck!

             

            It didn't work for me, the psd still seems to lag before the gradients are redrawn after the document is resized. The gradients only seem to be redrawn after the entire script runs.

             

            Its only gradients that appear to be effected and this only became a problem after updating to cc-2015. I have tried on a newer mac and it works fine so I assume it is something to do with my particular machine (imac late 2010).

             

            thanks for the help anyway.

             

            Jon

            • 3. Re: script saving png before psd has had time to update.
              Chuck Uebele Adobe Community Professional & MVP

              There's a new script framework in 2015, that has been casing a lot of issues. I'm working on a work around for one of my scripts - not sure if Adobe will totally fix what's going on in my case. I'll see if I can flag someone from Adobe to look at this - don't hold you're breath.

              • 5. Re: script saving png before psd has had time to update.
                Chuck Uebele Adobe Community Professional & MVP

                Is this a gradient layer style that isn't scaling right as ziffizgiz is suggesting?

                • 6. Re: script saving png before psd has had time to update.
                  carlob83680710 Level 1

                  yes it seems to be the gradient that doesn't scale before the image is saved, it seems to save the image and scale the layer styles afterwards even though in the Js the save is after the scale.