10 Replies Latest reply on Feb 9, 2017 9:02 AM by timothyo7275655

    Batch destination unique for each source file


      Hello everyone, I'm trying to create a small database of images that vary only by small alterations. Each source file is basically just a different background, but has a hundred or so individual, hidden layers in it. What I need to do is create a PNG for each of those source files... for each of those individual layers being visible, one at a time.


      I set up an action for one of them that just repeats Show layer 1->Save as->Hide layer 1->Show Layer 2->Save as->Hide layer 2-> and so on. It worked for that file without errors, but when I try to use it on a different file, it ends up overwriting the previous source file's products because the target directory is the same, if that makes sense.


      The final products' names are to be based only on the layer visible, and needs to be specific because they are being referenced in another program. They are supposed to be separated in the database by a subfolder that is named for the background image, but I can't seem to find out how to change that subfolder target in an action, which isn't only keeping me from applying batch to the many source files (backgrounds) but is also keeping me from doing it manually.


      So the question is essentially: Is there a way to make a script/action determine the Save As destination by referencing the source file's name?


      Because I know this is fairly convoluted, I have included a link to a more visual breakdown of how I want it to work:

      Imgur: The most awesome images on the Internet


      Thanks in advance.

        • 1. Re: Batch destination unique for each source file
          Tomas Sinkunas Level 3

          Hi Timothyo. Give this one a shot and let me know if I missed something

          // Script saves each layer as a PNG image with background.
          // File name is based on Layer Nmame.
          // Destination folder - PSD files background layer name.
          // Script by Tomas Sinkunas www.rendertom.com
              #target photoshop
              var win = new Window("dialog", "");
                  win.spacing = 5;
                  win.alignChildren = ["fill", "fill"];
                  win.grpInput = win.add("group");
                      win.grpInput.stInputFile        = win.grpInput.add("statictext", undefined, "Input File");
                      win.grpInput.etInputFile        = win.grpInput.add("edittext", undefined, ""); 
                      win.grpInput.btnSelectInputFile = win.grpInput.add("button", undefined, "...");
                      win.grpInput.stInputFile.preferredSize.width            = 90;
                      win.grpInput.etInputFile.preferredSize.width            = 200;
                      win.grpInput.btnSelectInputFile.preferredSize.width    = 48;
                  win.grpOutput = win.add("group");
                      win.grpOutput.stOutputFolder        = win.grpOutput.add("statictext", undefined, "Output Folder");
                      win.grpOutput.etOutputFolder        = win.grpOutput.add("edittext", undefined, ""); 
                      win.grpOutput.btnSelectOutputFolder = win.grpOutput.add("button", undefined, "...");
                      win.grpOutput.stOutputFolder.preferredSize.width        = 90;
                      win.grpOutput.etOutputFolder.preferredSize.width        = 200;
                      win.grpOutput.btnSelectOutputFolder.preferredSize.width = 48;
                  win.grpFooter = win.add("group");
                      win.grpFooter.btnCloseWindow  = win.grpFooter.add('button',undefined, "Close"); 
                      win.grpFooter.btnRunScript    = win.grpFooter.add('button',undefined, "Run Script");
                      win.grpFooter.btnCloseWindow.alignment =
                      win.grpFooter.btnRunScript.alignment = ["fill", "fill"];
                  win.grpInput.btnSelectInputFile.onClick = function() {
                      var selectedFile = File.openDialog("Please select PSD file", true)
                      if (selectedFile)
                          win.grpInput.etInputFile.text = selectedFile;
                  win.grpOutput.btnSelectOutputFolder.onClick = function() {
                      var outputFolder = Folder.selectDialog("Select PNG folder");
                      if (outputFolder)
                          win.grpOutput.etOutputFolder.text = outputFolder.fsName;
                  win.grpFooter.btnCloseWindow.onClick = function () {
                  win.grpFooter.btnRunScript.onClick = function() {
                      if (!File(win.grpInput.etInputFile.text).exists) {
                          return alert("Please select input file first");
                      } else if (!Folder(win.grpOutput.etOutputFolder.text).exists) {
                          return alert("Please select output folder");
              function main() {
                  try {
                      var mainDoc = open(File(win.grpInput.etInputFile.text));
                      for (var i = 0, il = mainDoc.layers.length; i < il; i ++) {
                          mainDoc.layers[i].visible = false;
                      if (mainDoc.backgroundLayer)
                          mainDoc.backgroundLayer.visible = true;
                      var saveFolder = Folder(win.grpOutput.etOutputFolder.text + "/" + mainDoc.backgroundLayer.name);
                      for (var i = 0, il = mainDoc.layers.length; i < il; i ++) {
                          if (mainDoc.layers[i] !== mainDoc.backgroundLayer) {
                              mainDoc.layers[i].visible = true;
                              var saveFile = new File(saveFolder.fsName + "/" + mainDoc.layers[i].name + ".png");
                              activeDocument.saveAs(saveFile, new PNGSaveOptions(), true, Extension.LOWERCASE);
                              mainDoc.layers[i].visible = false;
                  } catch (e) {
                      alert(e.toString() + "\nLine: " + e.line.toString());
          • 2. Re: Batch destination unique for each source file
            timothyo7275655 Level 1

            Wow this looks great. It threw back an error for no "Line 74" which looking at the code (as the kind of technical dolt for these things that I am) seems to be because there is no layer actually titled "Background".


            Currently what has essentially become the background is a whole lot of individual layers and a few color adjustments, making it easy to modify it for future needs. This however could be flattened in a separate file just used for exporting the PNGs via this script. I'll give that a go and see how it works.


            Preferably however, and it may even be easier to write the code this way, I'd prefer to just name the specific layers that are to be toggled as hidden/visible, and give each of those entries a specific name. Hopefully that makes sense. I'll see if I can't fiddle with what you already provided to see how that works.


            Wasn't expecting an actual script in a reply, thank you very much.

            • 3. Re: Batch destination unique for each source file
              Tomas Sinkunas Level 3

              Hi buddy.

              Script is looking for Background layer, you are right. I forgot to mention it.

              Anyways, it is difficult to see your file set up - can you attach a small PSD file you have? Picture says thousand words.

              • 4. Re: Batch destination unique for each source file
                timothyo7275655 Level 1

                Also, it may have been a little disingenuous to suggest what I wanted was as simple as just one layer at a time on top, so I've provided an example of what I'm trying to do:


                Imgur: The most awesome images on the Internet


                The first 4 are examples of the same background with different layers hidden/visible on top, and the last image is with a different background (different source file).


                What I am going to try to do working off the code already sent is to specify which layer(s) are visible for each PNG and the save file name for each of those. I'll get back here on how it works.

                • 5. Re: Batch destination unique for each source file
                  timothyo7275655 Level 1

                  You know, I have no idea why I didn't do this in the very first post.


                  dropcanvas - instant drag and drop sharing - Albums


                  Hopefully that helps

                  • 6. Re: Batch destination unique for each source file
                    timothyo7275655 Level 1

                    Oh one last tiny detail, all the source files are named already as the same thing for the subfolder their respective PNGs will be saved in. I think that's as simple as changing mainDoc.backgroundLayer.name in line 77 though right?

                    • 7. Re: Batch destination unique for each source file
                      Tomas Sinkunas Level 3

                      Yes, mainDoc.backgroundLayer.name creates a folder based on Background layer name. I thought you asked for it in your first post.

                      Anyways, I am looking at your PSD and I have no clue how to automate it. I dont understand what layers has to be enabled and what should be disabled.

                      • 8. Re: Batch destination unique for each source file
                        timothyo7275655 Level 1

                        So listing them all out with name of PNG to be saved, with layers to be made visible assuming it's in the default format (like the one I linked):

                        • Name: Atmosphere     Layers: AtmosphereDevice
                        • Name: Base     Layers: BaseDevice
                        • Name: CloserSolarOrbit     Layers: CloseStellarOrbitDevice, CapsuleDevice
                        • Name: Deep Atmosphere     Layers: DeepAtmospherDevice
                        • Name: EvaGround     Layers: LanderDevice, EvaDevice
                        • Name: EvaOrbit     Layers: CapsuleDevice, EvaDevice
                        • Name: EvaSpace     Layers: EvaDevice
                        • Name: OrbitCapsule     Layers: CapsuleDevice, OrbitDevice
                        • Name: OrbitCapsuleDocked     Layers: CapsuleDevice, OrbitDevice, DockingDevice
                        • Name: OrbitStation     Layers: StationDevice, OrbitDevice
                        • Name: PlantFlag     Layers: LanderDevice, EvaDevice, FlagDevice
                        • Name: Rover     Layers: RoverDevice, EvaDevice
                        • Name: SphereOfInfluence     Layers: (None)
                        • Name: Atmosphere     Layers: AtmosphereDevice


                        Then, each of those but with the "First Brackets" layer visible as well:


                        • Name: FirstBase     Layers: BaseDevice, First Brackets
                        • Name: FirstCloserSolarOrbit     Layers: CloseStellarOrbitDevice, CapsuleDevice, First Brackets
                        • Name: FirstDeep Atmosphere     Layers: DeepAtmospherDevice, First Brackets
                        • Name: FirstEvaGround     Layers: LanderDevice, EvaDevice, First Brackets
                        • Name: FirstEvaOrbit     Layers: CapsuleDevice, EvaDevice, First Brackets
                        • Name: FirstEvaSpace     Layers: EvaDevice, First Brackets
                        • Name: FirstOrbitCapsule     Layers: CapsuleDevice, OrbitDevice, First Brackets
                        • Name: FirstOrbitCapsuleDocked     Layers: CapsuleDevice, OrbitDevice, DockingDevice, First Brackets
                        • Name: FirstOrbitStation     Layers: StationDevice, OrbitDevice, First Brackets
                        • Name: FirstPlantFlag     Layers: LanderDevice, EvaDevice, FlagDevice, First Brackets
                        • Name: FirstRover     Layers: RoverDevice, EvaDevice, First Brackets
                        • Name: FirstSphereOfInfluence     Layers: First Brackets


                        Honestly though, wouldn't want you to worry too much about all that busywork. If you could just give me a basic template for the code where I can swap in the individual layer names and save names for each output PNG, I'd be more than thrilled. Thanks again for all the help so far.

                        • 9. Re: Batch destination unique for each source file
                          timothyo7275655 Level 1

                          Oops, there should only be one regular "Atmosphere" PNG, and also one for "FirstAtmosphere" with the brackets. I put that in the wrong table.


                          (Why can you not edit posts on this forum? Sigh)