20 Replies Latest reply on Oct 14, 2015 6:40 AM by mariap14286444

    Edit script from JPEG to PNG

    mariap14286444 Level 1

      I've been spending hours trying to edit this script I have that edits a smart layer and then saves all groups as a JPEG. This is great but sometimes I need to save each group as a PNG instead. Problems I've been encountering is the final file having been cropped to match the size of the picture inside the smart layer, when all files should be 483px x 483px and transparent.

       

      Here is the script I use. If someone could edit it for me I would be so grateful. Many thanks in advance.

       

       

      // select files and place in a selected smart object; 
      // to cause the so in the so to be scaled to fit the resolution is temprarily changed, but this is no guarantee for this to happen; 
      // 2015, use it at your own risk; 
      #target photoshop 
      if (app.documents.length > 0) { 
      var myDocument = app.activeDocument; 
      var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1]; 
      var thePath = myDocument.path; 
      var theLayer = myDocument.activeLayer; 
      // psd options; 
      psdOpts = new PhotoshopSaveOptions(); 
      psdOpts.embedColorProfile = true; 
      psdOpts.alphaChannels = true; 
      psdOpts.layers = true; 
      psdOpts.spotColors = true; 
      // check if layer is smart object; 
      if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")} 
      else { 
      if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.pdf;*.ai;*.psd;*.tif;*.png", true)} 
      else {var theFiles = File.openDialog ("please select files", getFiles, true)}; 
      if (theFiles) { 
      // set to pixels; 
      var originalRulerUnits = app.preferences.rulerUnits; 
      app.preferences.rulerUnits = Units.PIXELS; 
      // get bounds; 
      var theBounds = theLayer.bounds; 
      // work through the array; 
      for (var m = 0; m < theFiles.length; m++) { 
      var theNewName = File(theFiles[m]).name.match(/(.*)\.[^\.]+$/)[1]; 
      // replace smart object; 
      placeSmartObjectInSmartObject(app.activeDocument.activeLayer, theFiles[m]); 
      #target photoshop 
      function main(){ 
          if(!documents.length) return; 
          var doc = activeDocument; 
          var oldPath = activeDocument.path; 
          for(var a=0;a<doc.layerSets.length;a++){ 
              activeDocument.activeLayer = activeDocument.layers.getByName(doc.layerSets[a].name); 
              dupLayers(); 
              activeDocument.mergeVisibleLayers(); 
              activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true);
         var saveFile= File(oldPath+"/"+doc.layerSets[a].name.replace("*****", theNewName) +".jpg");

       

       

      var fileName = theNewName.slice(0,5);

       

      if (doc.layerSets[a].name.indexOf("*****") != -1) {

       

      var saveFile= File(oldPath+"/"+doc.layerSets[a].name.replace("*****", fileName) +".jpg");

       

      }

       

      else {

       

      var saveFile= File(oldPath+"/"+doc.layerSets[a].name+"_"+fileName +".jpg");

       

      };

       

         saveJPG(saveFile,10); 
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 
         
      main(); 
      function dupLayers() { 
          var desc143 = new ActionDescriptor(); 
          var ref73 = new ActionReference(); 
          ref73.putClass( charIDToTypeID('Dcmn') ); 
          desc143.putReference( charIDToTypeID('null'), ref73 ); 
          desc143.putString( charIDToTypeID('Nm  '), activeDocument.activeLayer.name ); 
          var ref74 = new ActionReference(); 
          ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
          desc143.putReference( charIDToTypeID('Usng'), ref74 ); 
          executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO ); 
      function saveJPG(saveFile, jpegQuality) { 
          app.activeDocument.flatten();  
          jpgSaveOptions = new JPEGSaveOptions(); 
          jpgSaveOptions.embedColorProfile = true; 
          jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
          jpgSaveOptions.matte = MatteType.NONE; 
          jpgSaveOptions.quality = jpegQuality; //1-12 
          activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

      // undo; 

      myDocument.activeHistoryState = myDocument.historyStates[myDocument.historyStates.length-2] 

      }; 

      // reset; 

      originalRulerUnits = app.preferences.rulerUnits; 

      }; 

      ////// open smart object ////// 
      function placeSmartObjectInSmartObject (theLayer, thisFile) { 
      if (theLayer.kind == "LayerKind.SMARTOBJECT") { 
      // get prefernces setting; 
      var ref = new ActionReference(); 
      ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("generalPreferences")); 
      ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
      var applicationDesc = executeActionGet(ref); 
      var generalDesc = applicationDesc.getObjectValue(stringIDToTypeID("generalPreferences")); 
      var resizeDuringPlace = generalDesc.getBoolean(stringIDToTypeID("resizePastePlace")); 
      if (resizeDuringPlace == false) { 
      // ======================================================= 
      var idsetd = charIDToTypeID( "setd" ); 
          var desc1 = new ActionDescriptor(); 
          var idnull = charIDToTypeID( "null" ); 
              var ref1 = new ActionReference(); 
              var idPrpr = charIDToTypeID( "Prpr" ); 
              var idGnrP = charIDToTypeID( "GnrP" ); 
              ref1.putProperty( idPrpr, idGnrP ); 
              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 idresizePastePlace = stringIDToTypeID( "resizePastePlace" ); 
              desc2.putBoolean( idresizePastePlace, true ); 
              var idlegacyPathDrag = stringIDToTypeID( "legacyPathDrag" ); 
              desc2.putBoolean( idlegacyPathDrag, true ); 
          var idGnrP = charIDToTypeID( "GnrP" ); 
          desc1.putObject( idT, idGnrP, desc2 ); 
      executeAction( idsetd, desc1, DialogModes.NO ); 
      }; 
      // ======================================================= 
      var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" ); 
          var desc2 = new ActionDescriptor(); 
      executeAction( idplacedLayerEditContents, desc2, DialogModes.NO ); 
      // set to high resolution to make scaling on placing probable; 
      var theResolution = activeDocument.resolution; 
      activeDocument.resizeImage(undefined, undefined, 3000, ResampleMethod.NONE); 
      // place so; 
      // ======================================================= 
      var idPlc = charIDToTypeID( "Plc " ); 
          var desc4 = new ActionDescriptor(); 
          var idAs = charIDToTypeID( "As  " ); 
              var desc5 = new ActionDescriptor(); 
              var idfsel = charIDToTypeID( "fsel" ); 
              var idpdfSelection = stringIDToTypeID( "pdfSelection" ); 
              var idpage = stringIDToTypeID( "page" ); 
              desc5.putEnumerated( idfsel, idpdfSelection, idpage ); 
              var idPgNm = charIDToTypeID( "PgNm" ); 
              desc5.putInteger( idPgNm, 1 ); 
              var idCrop = charIDToTypeID( "Crop" ); 
              var idcropTo = stringIDToTypeID( "cropTo" ); 
              var idtrimBox = stringIDToTypeID( "trimBox" ); 
              desc5.putEnumerated( idCrop, idcropTo, idtrimBox ); 
          var idPDFG = charIDToTypeID( "PDFG" ); 
          desc4.putObject( idAs, idPDFG, desc5 ); 
          var idnull = charIDToTypeID( "null" ); 
          desc4.putPath( idnull, new File( thisFile ) ); 
          var idFTcs = charIDToTypeID( "FTcs" ); 
          var idQCSt = charIDToTypeID( "QCSt" ); 
          var idQcsa = charIDToTypeID( "Qcsa" ); 
          desc4.putEnumerated( idFTcs, idQCSt, idQcsa ); 
          var idOfst = charIDToTypeID( "Ofst" ); 
              var desc6 = new ActionDescriptor(); 
              var idHrzn = charIDToTypeID( "Hrzn" ); 
              var idRlt = charIDToTypeID( "#Rlt" ); 
              desc6.putUnitDouble( idHrzn, idRlt, 0.000000 ); 
              var idVrtc = charIDToTypeID( "Vrtc" ); 
              var idRlt = charIDToTypeID( "#Rlt" ); 
              desc6.putUnitDouble( idVrtc, idRlt, 0.000000 ); 
          var idOfst = charIDToTypeID( "Ofst" ); 
          desc4.putObject( idOfst, idOfst, desc6 ); 
          var idAntA = charIDToTypeID( "AntA" ); 
          desc4.putBoolean( idAntA, true ); 
      executeAction( idPlc, desc4, DialogModes.NO ); 
      // toggle visibility of others; 
      // ======================================================= 
      var idShw = charIDToTypeID( "Shw " ); 
          var desc10 = new ActionDescriptor(); 
          var idnull = charIDToTypeID( "null" ); 
              var list4 = new ActionList(); 
                  var ref7 = new ActionReference(); 
                  var idLyr = charIDToTypeID( "Lyr " ); 
                  var idOrdn = charIDToTypeID( "Ordn" ); 
                  var idTrgt = charIDToTypeID( "Trgt" ); 
                  ref7.putEnumerated( idLyr, idOrdn, idTrgt ); 
              list4.putReference( ref7 ); 
          desc10.putList( idnull, list4 ); 
          var idTglO = charIDToTypeID( "TglO" ); 
          desc10.putBoolean( idTglO, true ); 
      executeAction( idShw, desc10, DialogModes.NO ); 
      // reset resolution; 
      activeDocument.resizeImage(undefined, undefined, theResolution, ResampleMethod.NONE); 
      // close; 
      app.activeDocument.close(SaveOptions.SAVECHANGES) 
      }; 
      }; 
      ////// get psds, tifs and jpgs from files ////// 
      function getFiles (theFile) { 
      if (theFile.name.match(/\.(pdf|ai|jpg|tif|psb|psd)$/i)) { 
      return true 
      }; 
      }; 

       

        • 1. Re: Edit script from JPEG to PNG
          c.pfaffenbichler Level 9

          Looks like it’s based on

          Re: Applying files in PS in bulk

          but why is

          #target photoshop

          in the Script twice?

          You seem to have made some changes that seem peculiar.

          • 2. Re: Edit script from JPEG to PNG
            mariap14286444 Level 1

            I'm not sure, I don't remember putting it in there. It works fine for me, and even when I removed the second #target photoshop it still works?

            • 3. Re: Edit script from JPEG to PNG
              c.pfaffenbichler Level 9

              In any case it seems you use a function (saveJPG) to save the jpg – if you want to save png instead corresponding functions for saving pngs have been posted before.

              https://forums.adobe.com/search.jspa?q=savepng&place=%2Fplaces%2F1383833&depth=ALL

              • 4. Re: Edit script from JPEG to PNG
                mariap14286444 Level 1

                When I use something like

                 

                function SavePNG(saveFile){
                  pngSaveOptions = new PNGSaveOptions();
                  activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
                }


                it crops my image to match the size of the file. I have a smart layer in my psd file which I import other images to, and if one is smaller than the smart layer, it will be cropped which I can't have ? I'm not sure how to edit it.

                • 5. Re: Edit script from JPEG to PNG
                  c.pfaffenbichler Level 9

                  Maybe you should not have inserted the line

                  activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true);
                  • 6. Re: Edit script from JPEG to PNG
                    mariap14286444 Level 1

                    honestly I keep trying to change it and I have no idea why it isn't working. I just seem to be having trouble changing this

                     

                    function saveJPG(saveFile, jpegQuality) { 
                        app.activeDocument.flatten();  
                        jpgSaveOptions = new JPEGSaveOptions(); 
                        jpgSaveOptions.embedColorProfile = true; 
                        jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
                        jpgSaveOptions.matte = MatteType.NONE; 
                        jpgSaveOptions.quality = jpegQuality; //1-12 
                        activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

                     

                    to save as a PNG. There's also

                     

                    saveJPG(saveFile,10);

                                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 

                            } 

                     

                    on line 59 which I don't understand.

                     

                    When I replaced those sections with

                     

                    function SavePNG(saveFile){
                      pngSaveOptions = new PNGSaveOptions();
                      activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);


                    }


                    it told me to remove saveJPG(savefile,10) and then it doesn't even save them any more ?

                    • 7. Re: Edit script from JPEG to PNG
                      c.pfaffenbichler Level 9

                      Posting fragments of the code you are using is not particularly helpful.

                       

                      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                      That closes the active document without saving.

                      • 8. Re: Edit script from JPEG to PNG
                        mariap14286444 Level 1

                        Hi,

                         

                        It won't let me have saveJPEG(app.activedocument) before, and when i change saveoptions to SAVECHANGES it asks me to save the smart layer manually.

                         

                        Is there no easy way to change my first post's script to save, instead of a JPEG, into a PNG?

                        • 9. Re: Edit script from JPEG to PNG
                          c.pfaffenbichler Level 9

                          You inserted what seems like dubious code to me into that so maybe you should start with the original code again.

                          Having a Script save png instead of jpg is no great problem.

                           

                          What is the Layer and Smart Object Structure?

                          • 10. Re: Edit script from JPEG to PNG
                            mariap14286444 Level 1

                            Capture.PNG

                             

                            I have attached the image of my PSD file. If there is no 'Layer 0' visible, it will crop the final file so there is no edge around the science flask image, which I don't want.

                            • 11. Re: Edit script from JPEG to PNG
                              c.pfaffenbichler Level 9

                              That screenshot has no relevance as for the Layer structure, Smart Object structure etc.

                              • 12. Re: Edit script from JPEG to PNG
                                mariap14286444 Level 1

                                I don't understand what that means; the script usually makes me choose an image and then just opens the smart layer, and it updates the layer with the image I choseand saves the file as a JPEG.

                                • 13. Re: Edit script from JPEG to PNG
                                  c.pfaffenbichler Level 9

                                  To be blunt your amendments to the Script seem like a mess to me.

                                  What’s dupLayers supposed to do for example?

                                  Why introduce a main function within the operation?

                                  Why a function depending on LayerSets (for(var a=0;a<doc.layerSets.length;a++){…)?

                                   

                                  So either let’s start from the beginning or please start a new thread that will hopefully attract the attention of someone other than me who will help you.

                                   

                                  What do you want to do exactly?

                                  What is the structure of the file (Layers, Smart Objects, …)?

                                  • 14. Re: Edit script from JPEG to PNG
                                    mariap14286444 Level 1

                                    Sorry, it's a frankenstein script and I didn't write most of it. It did what I wanted it to do and I thought it was simple to change the final save file, but I don't understand what you are asking so I might shelve this and look at it at a later date when I've done more script work. Thanks for your time

                                    • 15. Re: Edit script from JPEG to PNG
                                      c.pfaffenbichler Level 9

                                      I had forgotten you explicitly mentioned Groups in the original post, so the layerSets naturally make sense, but please post screenshots of the file with the Layers Panel visible and the resulting images.

                                      • 16. Re: Edit script from JPEG to PNG
                                        c.pfaffenbichler Level 9

                                        Assuming the Groups have Layer Masks please try this:

                                        // 2015, use it at your own risk;
                                        #target photoshop
                                        if (app.documents.length > 0) {
                                        var myDocument = app.activeDocument;
                                        var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];
                                        var thePath = myDocument.path;
                                        var theLayer = myDocument.activeLayer;
                                        // psd options;
                                        psdOpts = new PhotoshopSaveOptions();
                                        psdOpts.embedColorProfile = true;
                                        psdOpts.alphaChannels = true;
                                        psdOpts.layers = true;
                                        psdOpts.spotColors = true;
                                        // check if layer is smart object;
                                        if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")}
                                        else {
                                        if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.pdf;*.ai;*.psd;*.tif;*.png", true)}
                                        else {var theFiles = File.openDialog ("please select files", getFiles, true)};
                                        if (theFiles) {
                                        // set to pixels;
                                        var originalRulerUnits = app.preferences.rulerUnits;
                                        app.preferences.rulerUnits = Units.PIXELS;
                                        // get bounds;
                                        var theBounds = theLayer.bounds;
                                        // work through the array;
                                        for (var m = 0; m < theFiles.length; m++) {
                                        var theNewName = File(theFiles[m]).name.match(/(.*)\.[^\.]+$/)[1];
                                        // replace smart object;
                                        placeSmartObjectInSmartObject(app.activeDocument.activeLayer, theFiles[m]);
                                        main();
                                        // undo;
                                        myDocument.activeHistoryState = myDocument.historyStates[myDocument.historyStates.length-2]
                                        };
                                        // reset;
                                        originalRulerUnits = app.preferences.rulerUnits;
                                        }
                                        }
                                        };
                                        ////// open smart object //////
                                        function placeSmartObjectInSmartObject (theLayer, thisFile) {
                                        if (theLayer.kind == "LayerKind.SMARTOBJECT") {
                                        // get prefernces setting;
                                        var ref = new ActionReference();
                                        ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("generalPreferences"));
                                        ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
                                        var applicationDesc = executeActionGet(ref);
                                        var generalDesc = applicationDesc.getObjectValue(stringIDToTypeID("generalPreferences"));
                                        var resizeDuringPlace = generalDesc.getBoolean(stringIDToTypeID("resizePastePlace"));
                                        if (resizeDuringPlace == false) {
                                        // =======================================================
                                        var idsetd = charIDToTypeID( "setd" );
                                            var desc1 = new ActionDescriptor();
                                            var idnull = charIDToTypeID( "null" );
                                                var ref1 = new ActionReference();
                                                var idPrpr = charIDToTypeID( "Prpr" );
                                                var idGnrP = charIDToTypeID( "GnrP" );
                                                ref1.putProperty( idPrpr, idGnrP );
                                                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 idresizePastePlace = stringIDToTypeID( "resizePastePlace" );
                                                desc2.putBoolean( idresizePastePlace, true );
                                                var idlegacyPathDrag = stringIDToTypeID( "legacyPathDrag" );
                                                desc2.putBoolean( idlegacyPathDrag, true );
                                            var idGnrP = charIDToTypeID( "GnrP" );
                                            desc1.putObject( idT, idGnrP, desc2 );
                                        executeAction( idsetd, desc1, DialogModes.NO );
                                        };
                                        // =======================================================
                                        var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );
                                            var desc2 = new ActionDescriptor();
                                        executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );
                                        // set to high resolution to make scaling on placing probable;
                                        var theResolution = activeDocument.resolution;
                                        activeDocument.resizeImage(undefined, undefined, 3000, ResampleMethod.NONE);
                                        // place so;
                                        // =======================================================
                                        var idPlc = charIDToTypeID( "Plc " );
                                            var desc4 = new ActionDescriptor();
                                            var idAs = charIDToTypeID( "As  " );
                                                var desc5 = new ActionDescriptor();
                                                var idfsel = charIDToTypeID( "fsel" );
                                                var idpdfSelection = stringIDToTypeID( "pdfSelection" );
                                                var idpage = stringIDToTypeID( "page" );
                                                desc5.putEnumerated( idfsel, idpdfSelection, idpage );
                                                var idPgNm = charIDToTypeID( "PgNm" );
                                                desc5.putInteger( idPgNm, 1 );
                                                var idCrop = charIDToTypeID( "Crop" );
                                                var idcropTo = stringIDToTypeID( "cropTo" );
                                                var idtrimBox = stringIDToTypeID( "trimBox" );
                                                desc5.putEnumerated( idCrop, idcropTo, idtrimBox );
                                            var idPDFG = charIDToTypeID( "PDFG" );
                                            desc4.putObject( idAs, idPDFG, desc5 );
                                            var idnull = charIDToTypeID( "null" );
                                            desc4.putPath( idnull, new File( thisFile ) );
                                            var idFTcs = charIDToTypeID( "FTcs" );
                                            var idQCSt = charIDToTypeID( "QCSt" );
                                            var idQcsa = charIDToTypeID( "Qcsa" );
                                            desc4.putEnumerated( idFTcs, idQCSt, idQcsa );
                                            var idOfst = charIDToTypeID( "Ofst" );
                                                var desc6 = new ActionDescriptor();
                                                var idHrzn = charIDToTypeID( "Hrzn" );
                                                var idRlt = charIDToTypeID( "#Rlt" );
                                                desc6.putUnitDouble( idHrzn, idRlt, 0.000000 );
                                                var idVrtc = charIDToTypeID( "Vrtc" );
                                                var idRlt = charIDToTypeID( "#Rlt" );
                                                desc6.putUnitDouble( idVrtc, idRlt, 0.000000 );
                                            var idOfst = charIDToTypeID( "Ofst" );
                                            desc4.putObject( idOfst, idOfst, desc6 );
                                            var idAntA = charIDToTypeID( "AntA" );
                                            desc4.putBoolean( idAntA, true );
                                        executeAction( idPlc, desc4, DialogModes.NO );
                                        // toggle visibility of others;
                                        // =======================================================
                                        var idShw = charIDToTypeID( "Shw " );
                                            var desc10 = new ActionDescriptor();
                                            var idnull = charIDToTypeID( "null" );
                                                var list4 = new ActionList();
                                                    var ref7 = new ActionReference();
                                                    var idLyr = charIDToTypeID( "Lyr " );
                                                    var idOrdn = charIDToTypeID( "Ordn" );
                                                    var idTrgt = charIDToTypeID( "Trgt" );
                                                    ref7.putEnumerated( idLyr, idOrdn, idTrgt );
                                                list4.putReference( ref7 );
                                            desc10.putList( idnull, list4 );
                                            var idTglO = charIDToTypeID( "TglO" );
                                            desc10.putBoolean( idTglO, true );
                                        executeAction( idShw, desc10, DialogModes.NO );
                                        // reset resolution;
                                        activeDocument.resizeImage(undefined, undefined, theResolution, ResampleMethod.NONE);
                                        // close;
                                        app.activeDocument.close(SaveOptions.SAVECHANGES)
                                        };
                                        };
                                        ////// get psds, tifs and jpgs from files //////
                                        function getFiles (theFile) {
                                        if (theFile.name.match(/\.(pdf|ai|jpg|tif|psb|psd)$/i)) {
                                        return true
                                        };
                                        };
                                        ////// function to png //////
                                        function savePNG (docPath) {
                                        // weboptions;
                                        var webOptions = new ExportOptionsSaveForWeb();
                                        webOptions.format = SaveDocumentType.PNG;
                                        webOptions.PNG8 = false;
                                        webOptions.transparency = true;
                                        webOptions.interlaced = 0;
                                        webOptions.includeProfile = false;
                                        webOptions.optimized = true;
                                        activeDocument.exportDocument(docPath, ExportType.SAVEFORWEB, webOptions);
                                        };
                                        function dupLayers() {
                                        var desc143 = new ActionDescriptor();
                                        var ref73 = new ActionReference();
                                        ref73.putClass( charIDToTypeID('Dcmn') );
                                        desc143.putReference( charIDToTypeID('null'), ref73 );
                                        desc143.putString( charIDToTypeID('Nm  '), activeDocument.activeLayer.name );
                                        var ref74 = new ActionReference();
                                        ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
                                        desc143.putReference( charIDToTypeID('Usng'), ref74 );
                                        executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO );
                                        return activeDocument;
                                        };
                                        function saveJPG(saveFile, jpegQuality) {
                                            app.activeDocument.flatten(); 
                                            jpgSaveOptions = new JPEGSaveOptions();
                                            jpgSaveOptions.embedColorProfile = true;
                                            jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                                            jpgSaveOptions.matte = MatteType.NONE;
                                            jpgSaveOptions.quality = jpegQuality; //1-12
                                            activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
                                        }
                                        function main(){
                                        if(!documents.length) return;
                                        var doc = activeDocument;
                                        var oldPath =  doc.path;
                                        for(var a=0;a<doc.layerSets.length;a++){
                                        doc.activeLayer =  doc.layers.getByName(doc.layerSets[a].name);
                                        var theDuplicate = dupLayers();
                                        // crop;
                                        try {
                                        // load layer mask:
                                        // =======================================================
                                        var idsetd = charIDToTypeID( "setd" );
                                            var desc7 = new ActionDescriptor();
                                            var idnull = charIDToTypeID( "null" );
                                                var ref5 = new ActionReference();
                                                var idChnl = charIDToTypeID( "Chnl" );
                                                var idfsel = charIDToTypeID( "fsel" );
                                                ref5.putProperty( idChnl, idfsel );
                                            desc7.putReference( idnull, ref5 );
                                            var idT = charIDToTypeID( "T  " );
                                                var ref6 = new ActionReference();
                                                var idChnl = charIDToTypeID( "Chnl" );
                                                var idOrdn = charIDToTypeID( "Ordn" );
                                                var idTrgt = charIDToTypeID( "Trgt" );
                                                ref6.putEnumerated( idChnl, idOrdn, idTrgt );
                                            desc7.putReference( idT, ref6 );
                                        executeAction( idsetd, desc7, DialogModes.NO );
                                        theDuplicate.crop(theDuplicate.selection.bounds);
                                        }
                                        catch (e) {
                                        theDuplicate.mergeVisibleLayers();
                                        activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true)
                                        };
                                        var saveFile= File(oldPath+"/"+doc.layerSets[a].name.replace("*****", theNewName) +".jpg");
                                        // new path;
                                        var fileName = theNewName.slice(0,5);
                                        if (doc.layerSets[a].name.indexOf("*****") != -1) {
                                        var saveFile= File(oldPath+"/"+doc.layerSets[a].name.replace("*****", fileName) +".jpg");
                                        }
                                        else {
                                        var saveFile= File(oldPath+"/"+doc.layerSets[a].name+"_"+fileName +".png");
                                        };
                                        savePNG(saveFile);
                                        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                        }
                                        };
                                        
                                        • 17. Re: Edit script from JPEG to PNG
                                          mariap14286444 Level 1

                                          that works great, but it still saves the file as a JPEG.

                                          • 18. Re: Edit script from JPEG to PNG
                                            c.pfaffenbichler Level 9

                                            that works great, but it still saves the file as a JPEG.

                                            That seems unlikely as the function savePNG does save png.

                                            Please post the exact code you used.

                                            When I run it on this file and select three images (simple checkerboards for demonstration)

                                            smartObjectReplaceTestScr.jpg

                                            I get these resulting images

                                            smartObjectReplaceTestScr2.jpg

                                             

                                            One problem I noticed, though.

                                            The expression in the line

                                                var idT = charIDToTypeID( "T  " );

                                            needs to have three spaces!

                                                var idT = charIDToTypeID( "T   " );

                                            • 19. Re: Edit script from JPEG to PNG
                                              c.pfaffenbichler Level 9

                                              Please clarify the Layer structure of the file (Layers, Groups, Smart Objects, Masks, …) by way of a meaningful screenshot (including the Layers Panel).

                                              • 20. Re: Edit script from JPEG to PNG
                                                mariap14286444 Level 1

                                                Just wondering, with this script you posted, is there a way to get rid saving it altogether? Like, it merges each group in the file and saves them, but is there a way for the pop-up box to occur, select the file you'd like, and then update the smart layer with that file and stop there?