• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Batch destination unique for each source file

New Here ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

447

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Advocate ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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

(function(){

    #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.close();

        }

        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");

            }

            win.close();

            main()

        }

    win.show();

    function main() {

        try {

            var mainDoc = open(File(win.grpInput.etInputFile.text));

            for (var i = 0, il = mainDoc.layers.length; i < il; i ++) {

                mainDoc.layers.visible = false;

            }

            if (mainDoc.backgroundLayer)

                mainDoc.backgroundLayer.visible = true;

            var saveFolder = Folder(win.grpOutput.etOutputFolder.text + "/" + mainDoc.backgroundLayer.name);

                saveFolder.create();

            for (var i = 0, il = mainDoc.layers.length; i < il; i ++) {

                if (mainDoc.layers !== mainDoc.backgroundLayer) {

                    mainDoc.layers.visible = true;

                    var saveFile = new File(saveFolder.fsName + "/" + mainDoc.layers.name + ".png");

                    activeDocument.saveAs(saveFile, new PNGSaveOptions(), true, Extension.LOWERCASE);

                    mainDoc.layers.visible = false;

                }

            }

            mainDoc.close(SaveOptions.DONOTSAVECHANGES);

        } catch (e) {

            alert(e.toString() + "\nLine: " + e.line.toString());

        }    

    }

})();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

LATEST

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines