2 Replies Latest reply on Jan 26, 2016 2:55 AM by natrev

    Need help with javascript - Photoshop Save LayerComps for web in specific user defined folder

    bishop.kat

      Hi everyone! Here's my issue:

       

      I am working with photoshop files that are designs for animated web banners. All of the different frames are listed as Layer Comps (eg. Frame 1, Frame 2, bkg, overlay, etc). We have been manually selecting the layer comp and then Save for Web and selecting our options. The bkg is being saved for web as a jpg, and the other frames are being saved for web as png(s).

       

      I would love some assistance creating a script that would prompt the user for an output folder, and then automatically select the layer comp, and save it out with the appropriate settings.

       

      JPEG

      Compression High, Quality 80%. No other options selected.

       

      PNG-24

      Transparency Yes, No other options selected.

       

       

      I have found scripts for Save for Web - Jpeg and PNG. My main issue is combining them, selecting the correct profile dependent on the name of the layer comp, and defining an output folder.

       

      Any help or resources that you could send me to look into would be much appreciated.

        • 1. Re: Need help with javascript - Photoshop Save LayerComps for web in specific user defined folder
          bishop.kat Level 1

          Here are the two scripts that I have found to help me on my way.

           

          Save for Web - jpg

           

          function main(){  
          if(!documents.length) return;  
          var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');   
          var saveFile = File(Folder.desktop + "/" + Name + ".jpg");  
          if(saveFile.exists){  
             if(!confirm("Overwrite existing document?")) return;  
              saveFile.remove();  
              }  
          SaveForWeb(saveFile,100); //change to 60 for 60%  
          }  
          main();  
          function SaveForWeb(saveFile,jpegQuality) {  
          var sfwOptions = new ExportOptionsSaveForWeb();   
             sfwOptions.format = SaveDocumentType.JPEG;   
             sfwOptions.includeProfile = false;   
             sfwOptions.interlaced = 0;   
             sfwOptions.optimized = true;   
             sfwOptions.quality = jpegQuality; //0-100   
          activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);  
          }  
          

           

          Save for Web - png

          function main() {
                    // declare local variables
                    var doc = app.activeDocument;
                    var docName = app.activeDocument.name.slice(0,-4);
               var saveFile = new File("~/Desktop/" + docName + ".png");
              pngSaveOptions = new PNGSaveOptions();
              pngSaveOptions.interlaced = false;
              doc.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
          } 
          main();
          
          • 2. Re: Need help with javascript - Photoshop Save LayerComps for web in specific user defined folder
            natrev Level 3

            Hi Elven,

             

            Try this Code..

             

            Like this order Layer comp..(Frame 1...n,bg,overlay)

            Screen Shot 2016-01-26 at 4.26.14 pm.png

            -yajiv

             

            #target photoshop  
            app.bringToFront();  
            var outputfolder;
            main();
            function main(){
                if(!documents.length) return;      
                outputfolder=Folder("~/Desktop/Results/");
                if(!outputfolder.exists){outputfolder.create();}     
                var docRef = app.activeDocument;  
                var docName=docRef.name.replace(/\.[^\.]+$/, '');   
                var docPath = app.activeDocument.path;  
                var n= docRef.layerComps.length ;    
            for(i=0;i<n;i++){  
                var layCom=docRef.layerComps[i].name; 
                var FileName=docName+"_"+docRef.layerComps[i].name; 
                
                if(layCom=="Frame "+ (i+1) || layCom=="overlay"){
                    docRef.layerComps[i].apply();  
                    OutFoldPNG(docRef,FileName); 
                    }
                else if(layCom=="bg"){
                    docRef.layerComps[i].apply();  
                    OutFoldJPG(FileName); 
                    }
                }  
            } 
            function OutFoldJPG(docName){    
                var saveFile = File(outputfolder+ "/"+docName + ".jpg");    
                if(saveFile.exists){    
                   if(!confirm("Overwrite existing document?")) return;    
                        saveFile.remove();    
                    }    
                SaveForWeb(saveFile,100); //change to 60 for 60%    
            }    
            function SaveForWeb(saveFile,jpegQuality) {    
                var sfwOptions = new ExportOptionsSaveForWeb();     
                sfwOptions.format = SaveDocumentType.JPEG;     
                sfwOptions.includeProfile = false;     
                sfwOptions.interlaced = 0;     
                sfwOptions.optimized = true;     
                sfwOptions.quality = jpegQuality; //0-100     
                activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);    
            }    
            function OutFoldPNG(doc,docName) { 
                var saveFile = new File(outputfolder + "/"+ docName + ".png");  
                pngSaveOptions = new PNGSaveOptions();  
                pngSaveOptions.interlaced = false;  
                doc.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);  
            }