29 Replies Latest reply on Aug 7, 2015 9:33 PM by ApePlanet

    Reduce a Layer's Opacity, Then Export as .png

    ApePlanet

      Hello all. I would like to create a script (I don't think I can do it as an action) to reduce a layer's opacity by 3%, then export the file as a .png. Is this possible? I've seen where it would be possible to set a finite number, i.e. 97%, then export, but I was hoping there might be a chance to write one this way. Any help is welcomed. I've created actions before but never Ps scripts.

        • 1. Re: Reduce a Layer's Opacity, Then Export as .png
          michelr31372089 Level 2

          Hi ApePlanet,

          if you have 2 layers the names of which are "foreground" and "background", the following code sets the foreground opacity to 33%

          Try this code :

          #target photoshop

          app.bringToFront();

           

           

          var doc = app.activeDocument;

          doc.activeLayer = doc.artLayers.getByName("foreground");

          doc.activeLayer.opacity=33; // percent

          doc.flatten();

           

           

          //Define png options

          pngSaveOptions = new PNGSaveOptions();

          pngSaveOptions.compression=0; // without compression)

          pngSaveOptions.interlaced=false;

           

          //save as png file to specific location

          //activeDocument.saveAs(new File([path]+"/"+[name] +".png"), pngSaveOptions)

          1 person found this helpful
          • 2. Re: Reduce a Layer's Opacity, Then Export as .png
            ApePlanet Level 1

            Thanks for your reply michelr! Is there a way instead of setting the layer's opacity at a specific number/percentage, can you reduce the current layer opacity by, say, 3?

            • 3. Re: Reduce a Layer's Opacity, Then Export as .png
              Chuck Uebele Adobe Community Professional & MVP
              var d = activeDocument
              var r = d.activeLayer
              var op = Math.round (r.opacity)-3
              r.opacity = op
              alert(op)
              
              
              • 4. Re: Reduce a Layer's Opacity, Then Export as .png
                ApePlanet Level 1

                Chuck, this seems just what I needed for the opacity change part. I tested it out and it does work. I do have a curious question (as I'm new to PS scripting and my JS is way rusty), do I need the Math.round command or could it be "r.opacity = -3" and achieve the same thing?

                 

                Also, when the opacity gets to 1%, would I put code in that looks like this to make the next step 0%?

                 

                if (r.opacity <= 0) {r.opacity = 0};

                };

                 

                Thanks so much for your help!

                • 5. Re: Reduce a Layer's Opacity, Then Export as .png
                  Chuck Uebele Adobe Community Professional & MVP

                  At first, I didn't have the math round and I got a floating point number, so I put that in.

                  • 6. Re: Reduce a Layer's Opacity, Then Export as .png
                    Chuck Uebele Adobe Community Professional & MVP

                    I guess if you wanted exactly 3 less, you would leave out the round comment. I guess I used the slider for the test and it give you a floating point value.

                    • 7. Re: Reduce a Layer's Opacity, Then Export as .png
                      ApePlanet Level 1

                      Michele,

                       

                      Thanks so much for your reply! Can I ask you for some clarity on the File Saving portion. I've been looking at Adobe's scripting guides and Googling examples and I'm still having trouble wrapping my head around things because I seem to find 5 ways to do everything, haha. Anyways, the pngSaveOptions seem self-explanatory, it's the file path + file name that is throwing me off. When I put the following, it brings up the Save As dialog and does take me to the right folder from the path, but it does not save as the file name. I need to manually change the name and click Save.

                       

                      //save as png file to specific location

                      activeDocument.saveAs(new File("~/Pictures/TestFolder/"+"TestFileName"+".png"), pngSaveOptions)

                      • 8. Re: Reduce a Layer's Opacity, Then Export as .png
                        ApePlanet Level 1

                        Chuck,

                         

                        Thanks so much for all of your help! I got it to the point I need with adjusting the layer's opacity (I even was able to figure out a conditional statement to set the opacity to 0 if it got to 2 or below). The only thing I need now is help with the saving file path and name, which I replied to Michele's comment for some help and clarity. I very much appreciate the help! Thank you!

                        • 9. Re: Reduce a Layer's Opacity, Then Export as .png
                          Chuck Uebele Adobe Community Professional & MVP

                          I use that line in many of my scripts and I don't have a dialog box that comes up, it just saves the file. Do you have something to call the dialog to appear? At the beginning of my scripts I put in:

                           

                          app.displayDialogs = DialogModes.NO;
                          

                           

                          I only turn the dialogModes on if I need them, then turn them off right away. The only issue with this is you have to put in some error checking to make sure you don't overwrite a file, as it will give you no warning if the file exist. Something to the effect of:

                           

                          var checkFile = new File("~/Pictures/TestFolder/"+"TestFileName"+".png";
                          if(checkFile.exists){//code here to either bring up a dialog box or not do the saveas
                          }
                          
                          • 10. Re: Reduce a Layer's Opacity, Then Export as .png
                            michelr31372089 Level 2

                            In the following code, you choose the specific folder where you intend to save your converted file and you save it.

                            #target photoshop

                            app.bringToFront();

                             

                             

                            //save the file to a specific location

                            var outputFolder = Folder.selectDialog("Select the folder where you want to save your PNG file : ");

                            var doc = app.activeDocument;

                            var docPng  = doc.name;

                            var docPng  = outputFolder+"/"+docPng.substring( 0, docPng.indexOf('.') )+".png";

                            doc.activeLayer = doc.artLayers.getByName("foreground");

                            doc.activeLayer.opacity=33; // percent

                            doc.flatten();

                             

                             

                            //Define png options and save

                            pngSaveOptions = new PNGSaveOptions();

                            pngSaveOptions.compression=0; // without compression)

                            pngSaveOptions.interlaced=false;

                            activeDocument.saveAs(new File(docPng), pngSaveOptions)

                            • 11. Re: Reduce a Layer's Opacity, Then Export as .png
                              ApePlanet Level 1

                              Thanks Michele & Chuck! I'm going to have to try these again. I'm not sure why it's not working for me. I'll post back here with my full code for help if it doesn't work for me.

                               

                              By the way Chuck, how do you get the code lines in these posts here? Silly question, but one I'm curious about. Thanks!

                              • 12. Re: Reduce a Layer's Opacity, Then Export as .png
                                Chuck Uebele Adobe Community Professional & MVP

                                Click on "Use Advanced Editor" in upper right the click on the ">>" in the formatting bar. You can then select various options.

                                 

                                • 13. Re: Reduce a Layer's Opacity, Then Export as .png
                                  ApePlanet Level 1

                                  Sorry Chuck, I don't seem to have the Advanced Editor here on Mac FF or Chrome. Weird. Oh well.

                                   

                                  The good news is that I got my script working thanks to both you and Michele! I'm so stoked. This is my first script, so I feel a great sense of accomplishment. I'll paste the final code below for you to check out, but basically the code will reduce a layer's opacity (in my codes case by 3% but that can be changed) and then save a new PNG from that file. This is going to work out great for a project I'm working on. Thanks so much!

                                   

                                  // Copyright 2015, Lee S. Millard

                                   

                                   

                                  /*

                                      This script will reduce a selected

                                      layer's opacity by the defined

                                      percentage (line 26) and then

                                      save a new file as a PNG in a

                                      user-specified location

                                  */

                                   

                                   

                                  // Enable double-clicking from Mac Finder or Windows Explorer to open Photoshop

                                  #target photoshop

                                   

                                   

                                  // In case file is double-clicked

                                  app.bringToFront();

                                   

                                   

                                  // Declaring the variables

                                  var doc = activeDocument; // The active document

                                  var docLayer = doc.activeLayer ; // The selected layer in the active document

                                  var docName = doc.name; // The name of the active document

                                  var layerName = docLayer.name; // The name of the selected layer in the active document

                                   

                                   

                                  if (docLayer.opacity >= 3) // This number defines the minimum opacity percentage that can be used to run line 26. This number should equal the number in line 26.

                                      {

                                          // Run this action if the IF statement is true

                                          var newOpacity = Math.round (docLayer.opacity)-3; // Reduces layer's opacity by defined %

                                          }

                                  else

                                      {

                                          // Run this actiona if the IF statement is false

                                          newOpacity = 0; // Sets layer's opacity to 0%

                                          }

                                   

                                   

                                  // New opacity percentage

                                  docLayer.opacity = newOpacity;

                                   

                                   

                                  // Opacity change alert (uncomment line below if you want an alert to show)

                                  //alert(newOpacity); 

                                   

                                   

                                  //File save-to location and name

                                  var outputFolder = Folder.selectDialog("Select the folder where you want to save your PNG file : "); // Let's user choose save location

                                  var newDocName  = outputFolder+"/"+layerName+"_"+newOpacity+".png"; // Defines the naming structure of new file

                                   

                                  //Define png options and save as a new file

                                  pngSaveOptions = new PNGSaveOptions();

                                  pngSaveOptions.compression=0; // No compression

                                  pngSaveOptions.interlaced=false; // No interlacing

                                  doc.saveAs(new File(newDocName), pngSaveOptions) // New file with new name and PNG settings applied

                                  • 14. Re: Reduce a Layer's Opacity, Then Export as .png
                                    Chuck Uebele Adobe Community Professional & MVP

                                    Glad it worked out. Scripting can be addicting when you realize all the stuff you can do with it. just doing simple things first then working on more complex projects is fun. Here a screen shot of the Use Advanced editor button. Odd that you don't have it:

                                     

                                    • 15. Re: Reduce a Layer's Opacity, Then Export as .png
                                      ApePlanet Level 1

                                      Very odd indeed, and yes, I can see the high you can get with writing code.

                                       

                                      Here's what I see:

                                       

                                      Screen Shot 2015-08-05 at 3.11.18 PM.png

                                      • 16. Re: Reduce a Layer's Opacity, Then Export as .png
                                        ApePlanet Level 1

                                        Okay, for some reason, I left the tab I had open for about a week, refreshed, and now I have the Advanced Editor now. Anyways, thanks again!

                                        • 17. Re: Reduce a Layer's Opacity, Then Export as .png
                                          ApePlanet Level 1

                                          I'm very sorry to keep posting and being an annoyance. Yesterday afternoon I seemed to have figured out my code and all was working perfectly, but then last night it wasn't, and this morning still is not. I'm sure it's a manual error (maybe when I was cleaning up my code) that I did, but I can't find out what for the life of me.

                                           

                                          The problem is when the file saves. A dialog window comes up asking where you would like to save your file. This is fine, this should do that. But after you select your folder to save it in, it should automatically save the PNG file in the naming structure I provided in the script. Anyways, it seems to be throwing an error up for me in the last line, but I don't know what I might have changed to prevent this from automatically saving the file. Can you take a look at it and test out on your end please? Thanks!

                                           

                                          // Copyright 2015, Lee S. Millard
                                          
                                          /*
                                              This script will reduce a selected 
                                              layer's opacity by the defined 
                                              percentage (line 26) and then 
                                              save a new file as a PNG in a 
                                              user-specified location
                                          */
                                          
                                          // Enable double-clicking from Mac Finder or Windows Explorer to open Photoshop
                                          #target photoshop
                                          
                                          // In case file is double-clicked
                                          app.bringToFront();
                                          
                                          // Declaring the variables
                                          var doc = app.activeDocument; // The active document
                                          var docLayer = doc.activeLayer ; // The selected layer in the active document
                                          var docName = doc.name; // The name of the active document
                                          var layerName = docLayer.name; // The name of the selected layer in the active document
                                          
                                          if (docLayer.opacity >= 3) // This number defines the minimum opacity percentage that can be used to run line 26 (This number should equal the number in line 26)
                                              {
                                                  // Run this action if the IF statement is true
                                                  var newOpacity = Math.round (docLayer.opacity)-3; // Reduces layer's opacity by defined % (The number in line 23 should equal this number)
                                                  }
                                          else
                                              {
                                                  // Run this actiona if the IF statement is false
                                                  newOpacity = 0; // Sets layer's opacity to 0%
                                                  }
                                          
                                          // New opacity percentage
                                          docLayer.opacity = newOpacity;
                                          
                                          // Opacity change alert (uncomment line below if you want an alert to show)
                                          //alert(newOpacity);  
                                          
                                          //File save-to location and name
                                          var outputFolder = Folder.selectDialog("Select the folder where you want to save your PNG file:"); // Let's user choose save location
                                          var newDocName  = outputFolder+"/"+layerName+"_"+newOpacity+".png"; // Defines the naming structure of new file
                                          
                                          //Define png options and save as a new file
                                          pngSaveOptions = new PNGSaveOptions();
                                          pngSaveOptions.compression=0; // No compression
                                          pngSaveOptions.interlaced=false; // No interlacing
                                          doc.saveAs(new File(newDocName),pngSaveOptions); // New file with new name and PNG settings applied
                                          
                                          

                                           

                                          (look Chuck Uebele, I even used the fancy Advanced Editor )

                                          • 18. Re: Reduce a Layer's Opacity, Then Export as .png
                                            Chuck Uebele Adobe Community Professional & MVP

                                            What's the exact wording of the error?

                                            • 19. Re: Reduce a Layer's Opacity, Then Export as .png
                                              ApePlanet Level 1

                                              I guess technically it doesn't give me an error, because I can rename the PNG file from the dialog window. It's just not saving like it should.

                                              • 20. Re: Reduce a Layer's Opacity, Then Export as .png
                                                Chuck Uebele Adobe Community Professional & MVP

                                                try this at line 48:

                                                 

                                                app.displayDialogs = DialogModes.ALL;
                                                doc.saveAs(new File(newDocName),pngSaveOptions); // New file with new name and PNG settings applied 
                                                app.displayDialogs = DialogModes.NO;
                                                
                                                • 21. Re: Reduce a Layer's Opacity, Then Export as .png
                                                  ApePlanet Level 1

                                                  No dice. I even ran the latest PS update thinking that might help, but no. It just baffles me on how it was working and then it isn't.

                                                  • 22. Re: Reduce a Layer's Opacity, Then Export as .png
                                                    Chuck Uebele Adobe Community Professional & MVP

                                                    I'm trying it out and it's not taking the new name and path. Will see what I can find.

                                                    • 24. Re: Reduce a Layer's Opacity, Then Export as .png
                                                      Chuck Uebele Adobe Community Professional & MVP

                                                      I think the big issue is trying to change the name in the saveAs dialog box. If you automatically save the file and not have that come up it works. I changed line 48 to AM code, which seems to run better:

                                                       

                                                      //Copyright 2015, Lee S. Millard  
                                                      
                                                      /* 
                                                        This script will reduce a selected  
                                                        layer's opacity by the defined  
                                                        percentage (line 26) and then  
                                                        save a new file as a PNG in a  
                                                        user-specified location 
                                                      */  
                                                      
                                                      // Enable double-clicking from Mac Finder or Windows Explorer to open Photoshop  
                                                      #target photoshop  
                                                      
                                                      // In case file is double-clicked  
                                                      app.bringToFront();  
                                                      
                                                      // Declaring the variables  
                                                      var doc = app.activeDocument; // The active document  
                                                      var docLayer = doc.activeLayer ; // The selected layer in the active document  
                                                      var docName = doc.name; // The name of the active document  
                                                      var layerName = docLayer.name; // The name of the selected layer in the active document  
                                                      
                                                      if (docLayer.opacity >= 3) // This number defines the minimum opacity percentage that can be used to run line 26 (This number should equal the number in line 26)  
                                                        {  
                                                            // Run this action if the IF statement is true  
                                                            var newOpacity = Math.round (docLayer.opacity)-3; // Reduces layer's opacity by defined % (The number in line 23 should equal this number)  
                                                            }  
                                                      else  
                                                        {  
                                                            // Run this actiona if the IF statement is false  
                                                            newOpacity = 0; // Sets layer's opacity to 0%  
                                                            }  
                                                      
                                                      // New opacity percentage  
                                                      docLayer.opacity = newOpacity;  
                                                      
                                                      // Opacity change alert (uncomment line below if you want an alert to show)  
                                                      //alert(newOpacity);    
                                                      
                                                      //File save-to location and name  
                                                      var outputFolder = Folder.selectDialog("Select the folder where you want to save your PNG file:"); // Let's user choose save location  
                                                      var newDocName  = outputFolder+"/"+layerName+"_"+newOpacity+".png"; // Defines the naming structure of new file  
                                                      
                                                      //Define png options and save as a new file  
                                                      //   pngSaveOptions = new PNGSaveOptions();  
                                                      //   pngSaveOptions.compression=0; // No compression  
                                                      //    pngSaveOptions.interlaced=false; // No interlacing  
                                                          
                                                        savePng (newDocName)
                                                      
                                                      function savePng(file){
                                                            var idsave = charIDToTypeID( "save" );
                                                                var desc6 = new ActionDescriptor();
                                                                var idAs = charIDToTypeID( "As  " );
                                                                    var desc7 = new ActionDescriptor();
                                                                    var idPGIT = charIDToTypeID( "PGIT" );
                                                                    var idPGIT = charIDToTypeID( "PGIT" );
                                                                    var idPGIN = charIDToTypeID( "PGIN" );
                                                                    desc7.putEnumerated( idPGIT, idPGIT, idPGIN );
                                                                    var idPNGf = charIDToTypeID( "PNGf" );
                                                                    var idPNGf = charIDToTypeID( "PNGf" );
                                                                    var idPGAd = charIDToTypeID( "PGAd" );
                                                                    desc7.putEnumerated( idPNGf, idPNGf, idPGAd );
                                                                    var idCmpr = charIDToTypeID( "Cmpr" );
                                                                    desc7.putInteger( idCmpr, 0 );
                                                                var idPNGF = charIDToTypeID( "PNGF" );
                                                                desc6.putObject( idAs, idPNGF, desc7 );
                                                                var idIn = charIDToTypeID( "In  " );
                                                                desc6.putPath( idIn, new File( file ) );
                                                                var idDocI = charIDToTypeID( "DocI" );
                                                                desc6.putInteger( idDocI, 310 );
                                                                var idCpy = charIDToTypeID( "Cpy " );
                                                                desc6.putBoolean( idCpy, true );
                                                                var idsaveStage = stringIDToTypeID( "saveStage" );
                                                                var idsaveStageType = stringIDToTypeID( "saveStageType" );
                                                                var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
                                                                desc6.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
                                                            executeAction( idsave, desc6, DialogModes.NO );
                                                            app.displayDialogs = DialogModes.NO; 
                                                            }
                                                      
                                                      • 25. Re: Reduce a Layer's Opacity, Then Export as .png
                                                        ApePlanet Level 1

                                                        Chuck, I have no idea what this code means, but I can tell you it worked for me. What is "AM code" anyway? I understand the "save" and "As" lines and a few others, but have no clue on the code used. I definitely appreciate the help and like I said, it is working now which is great.

                                                        • 26. Re: Reduce a Layer's Opacity, Then Export as .png
                                                          Chuck Uebele Adobe Community Professional & MVP

                                                          It's Action Manager code. It's normally generated when you use the scriptlistener plug in. It will record ALMOST anything you do in PS and save it to a log file on your desktop. You then just have to cut and paste that code into your script, and apply variables as needed.

                                                          • 27. Re: Reduce a Layer's Opacity, Then Export as .png
                                                            ApePlanet Level 1

                                                            I just installed this scriptlistener plugin last night. Very useful I suppose when you can't write code for some processes and actions. Well, either way, it is working now thanks to you. I'm still baffled on why it worked yesterday then all of a sudden stopped working. Thanks again for helping me out!

                                                            • 28. Re: Reduce a Layer's Opacity, Then Export as .png
                                                              Chuck Uebele Adobe Community Professional & MVP

                                                              If you're using Scriptlistener now, you might want to check out this thread on how to turn it on and off:

                                                               

                                                              Turning ScriptListener on and off

                                                              • 29. Re: Reduce a Layer's Opacity, Then Export as .png
                                                                ApePlanet Level 1

                                                                Wow. This definitely will come in handy. Thanks so much for sharing this!