What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Load files into Stack automated action


breravin

New Member
Messages
3
Likes
0
Hello! This is my first post/question. I've been spending a couple days trying to work around this myself but haven't succeeded.

I've created a rather complex action within Photoshop (CC), and currently the only step in the action which requires manual input is the "Load files into Stack" script.

I'd like to use the "Load files into Stack" script but prevent all prompts/dialogues. I'd simply like to hard code a folder location (filled with png files) so that the action can continue uninterrupted.

This feels "simple" enough to do, but I'm simply too unfamiliar with Photoshop scripting to get it to work myself (and I'm under a bit of a time constraint).

Cheers!

edit: just for what it's worth, I've scoured the forums and stack overflow looking for an existing answer/solution without success.
 
Last edited:

breravin

New Member
Messages
3
Likes
0
Got it figured out :)

JSX:
var folder = new Folder('~/MyFolder'); 
 
 
function runLoadStack(folderPath) { 
    var loadLayersFromScript = true; 
    // @include 'Load Files into Stack.jsx' 
    var fList = folder.getFiles('*.png') 
    var aFlag = true; 
    loadLayers.alignStack = function( stackDoc ) 
{ 
    selectAllLayers(stackDoc, 0); 
} 
    loadLayers.intoStack(fList, aFlag); 
} 
runLoadStack(folder)
 

dthaxton

New Member
Messages
1
Likes
0
Hey, it sounds like you were in the same place in in now. I'm trying to recreate exactly what you're doing. I'm getting an error code saying "Load Layers: At least two files must be selected to create a stack." Did you get this as well? Can you help me out? Thanks!
 

breravin

New Member
Messages
3
Likes
0
Hey, it sounds like you were in the same place in in now. I'm trying to recreate exactly what you're doing. I'm getting an error code saying "Load Layers: At least two files must be selected to create a stack." Did you get this as well? Can you help me out? Thanks!

Hey dthaxton, sorry for the delay, I've been out of town. If you're seeing this message, it's probably one of two things
  1. The folder you're pointing to in the script (var folder = new Folder('~/MyFolder');) is empty
  2. You're not actually pointing to a folder (don't forget to change "'~/MyFolder' to your desired folder location
 

MarshySwamp

Active Member
Messages
34
Likes
10
I just stumbled over this old topic. Here are three alternatives. The code is easily adapted from using a folder selection dialog to a static folder. There is an option to align layers vertically and or horizontally which is commented out.

This script uses linked smart objects:

JavaScript:
/*
Stacker - Place Linked.jsx
Stephen Marsh, v1.1
*/

#target photoshop

if (app.documents.length === 0) {
    (function () {
        var savedDisplayDialogs = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;
        var origUnits = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;

        var inputFolder = Folder.selectDialog('Please select the input folder:');
        if (inputFolder === null) {
            app.beep();
            return;
        }

        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif)$/i);
        // inputFiles.sort().reverse;
        inputFiles.sort();

        app.displayDialogs = DialogModes.NO;

        var baseDoc = open(inputFiles[0]);
        var baseDoc = activeDocument;
        baseDoc.duplicate("Stacker", false);
        baseDoc.close(SaveOptions.DONOTSAVECHANGES);

        for (var i = 0; i < inputFiles.length; i++) {
            // true = linked | false = embedded
            placeFile(new File(inputFiles[i]), true, 0, 0);
            // Remove the filename extension from the layer name
            activeDocument.activeLayer.name = inputFiles[i].name.replace(/\.[^\.]+$/, '');
            //align2SelectAll('AdCH');
            //align2SelectAll('AdCV');
        }

        activeDocument.activeLayer = activeDocument.backgroundLayer;
        activeDocument.activeLayer.remove();

        //app.runMenuItem(stringIDToTypeID("selectAllLayers"));
        //reverseLayerStack();
        app.beep();
        alert(inputFiles.length + ' files stacked!');
        app.displayDialogs = savedDisplayDialogs;
        app.preferences.rulerUnits = origUnits;


        // Functions

        function placeFile(null2, linked, horizontal, vertical) {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var AD = new ActionDescriptor();
            AD.putInteger(s2t("ID"), 1);
            AD.putPath(s2t("null"), null2);
            AD.putBoolean(s2t("linked"), linked); // false for embedded
            AD.putEnumerated(s2t("freeTransformCenterState"), s2t("quadCenterState"), s2t("QCSAverage"));
            AD.putUnitDouble(s2t("horizontal"), s2t("pixelsUnit"), horizontal);
            AD.putUnitDouble(s2t("vertical"), s2t("pixelsUnit"), vertical);
            AD.putObject(s2t("offset"), s2t("offset"), AD);
            executeAction(s2t("placeEvent"), AD, DialogModes.NO);
        }

        function reverseLayerStack() {
            var idreverse = stringIDToTypeID("reverse");
            var desc4653 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref2335 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idordinal = stringIDToTypeID("ordinal");
            var idtargetEnum = stringIDToTypeID("targetEnum");
            ref2335.putEnumerated(idlayer, idordinal, idtargetEnum);
            desc4653.putReference(idnull, ref2335);
            executeAction(idreverse, desc4653, DialogModes.NO);
        }

        function align2SelectAll(method) {
            /*
            AdLf = Align Left
            AdRg = Align Right
            AdCH = Align Centre Horizontal
            AdTp = Align Top
            AdBt = Align Bottom
            AdCV = Align Centre Vertical
            */
            app.activeDocument.selection.selectAll();
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            desc.putReference(charIDToTypeID("null"), ref);
            desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
            try {
                executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
            } catch (e) {}
            app.activeDocument.selection.deselect();
        }
    })();

} else {
    alert('Please close all open files before running this script...');
}


This script uses embedded smart objects:

JavaScript:
/*
Stacker - Place Embedded.jsx
Stephen Marsh, v1.1
https://community.adobe.com/t5/photoshop-ecosystem-discussions/layers-creating-layers/td-p/13252109
*/

#target photoshop

if (app.documents.length === 0) {
    (function () {
        var savedDisplayDialogs = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;
        var origUnits = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;

        var inputFolder = Folder.selectDialog('Please select the input folder:');
        if (inputFolder === null) {
            app.beep();
            return;
        }

        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif)$/i);
        // inputFiles.sort().reverse;
        inputFiles.sort();

        app.displayDialogs = DialogModes.NO;

        var baseDoc = open(inputFiles[0]);
        var baseDoc = activeDocument;
        baseDoc.duplicate("Stacker", false);
        baseDoc.close(SaveOptions.DONOTSAVECHANGES);

        for (var i = 0; i < inputFiles.length; i++) {
            // true = linked | false = embedded
            placeFile(new File(inputFiles[i]), false, 0, 0);
            // Remove the filename extension from the layer name
            activeDocument.activeLayer.name = inputFiles[i].name.replace(/\.[^\.]+$/, '');
            //align2SelectAll('AdCH');
            //align2SelectAll('AdCV');
        }

        activeDocument.activeLayer = activeDocument.backgroundLayer;
        activeDocument.activeLayer.remove();

        //app.runMenuItem(stringIDToTypeID("selectAllLayers"));
        //reverseLayerStack();
        app.beep();
        alert(inputFiles.length + ' files stacked!');
        app.displayDialogs = savedDisplayDialogs;
        app.preferences.rulerUnits = origUnits;


        // Functions

        function placeFile(null2, linked, horizontal, vertical) {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var AD = new ActionDescriptor();
            AD.putInteger(s2t("ID"), 1);
            AD.putPath(s2t("null"), null2);
            AD.putBoolean(s2t("linked"), linked); // false for embedded
            AD.putEnumerated(s2t("freeTransformCenterState"), s2t("quadCenterState"), s2t("QCSAverage"));
            AD.putUnitDouble(s2t("horizontal"), s2t("pixelsUnit"), horizontal);
            AD.putUnitDouble(s2t("vertical"), s2t("pixelsUnit"), vertical);
            AD.putObject(s2t("offset"), s2t("offset"), AD);
            executeAction(s2t("placeEvent"), AD, DialogModes.NO);
        }

        function reverseLayerStack() {
            var idreverse = stringIDToTypeID("reverse");
            var desc4653 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref2335 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idordinal = stringIDToTypeID("ordinal");
            var idtargetEnum = stringIDToTypeID("targetEnum");
            ref2335.putEnumerated(idlayer, idordinal, idtargetEnum);
            desc4653.putReference(idnull, ref2335);
            executeAction(idreverse, desc4653, DialogModes.NO);
        }

        function align2SelectAll(method) {
            /*
            AdLf = Align Left
            AdRg = Align Right
            AdCH = Align Centre Horizontal
            AdTp = Align Top
            AdBt = Align Bottom
            AdCV = Align Centre Vertical
            */
            app.activeDocument.selection.selectAll();
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            desc.putReference(charIDToTypeID("null"), ref);
            desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
            try {
                executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
            } catch (e) {}
            app.activeDocument.selection.deselect();
        }
    })();

} else {
    alert('Please close all open files before running this script...');
}
 

MarshySwamp

Active Member
Messages
34
Likes
10
This variation uses duplicate to create normal layers:

JavaScript:
/*
Stacker - Dupe.jsx
Stephen Marsh, v1.2
*/

#target photoshop

if (app.documents.length === 0) {
    (function () {
        var savedDisplayDialogs = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;
        var origUnits = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;

        var inputFolder = Folder.selectDialog('Please select the input folder:');
        if (inputFolder === null) {
            return;
        }

        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif)$/i);
        // inputFiles.sort().reverse;
        inputFiles.sort();

        var firstFile = app.open(File(inputFiles[0]));
        var firstFileName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
        app.activeDocument.duplicate("Stacker", false);
        if (activeDocument.mode === DocumentMode.BITMAP) {
            activeDocument.changeMode(ChangeMode.GRAYSCALE);
        } else if (activeDocument.mode === DocumentMode.INDEXEDCOLOR) {
            activeDocument.changeMode(ChangeMode.RGB);
        }
        firstFile.close(SaveOptions.DONOTSAVECHANGES);
        var docStack = app.documents[0];
        app.activeDocument = docStack;
        docStack.activeLayer.name = firstFileName;

        for (var i = 1; i < inputFiles.length; i++) {
            var remainingFiles = app.open(File(inputFiles[i]));
            if (activeDocument.mode === DocumentMode.BITMAP) {
                activeDocument.changeMode(ChangeMode.GRAYSCALE);
            } else if (activeDocument.mode === DocumentMode.INDEXEDCOLOR) {
                activeDocument.changeMode(ChangeMode.RGB);
            }
            var fileName = remainingFiles.name.replace(/\.[^\.]+$/, '');
            remainingFiles.activeLayer.name = fileName;
            remainingFiles.layers[0].duplicate(docStack, ElementPlacement.PLACEATBEGINNING);
            remainingFiles.close(SaveOptions.DONOTSAVECHANGES);
            //align2SelectAll('AdCH');
            //align2SelectAll('AdCV');
        }

        //app.runMenuItem(stringIDToTypeID("selectAllLayers"));
        //reverseLayerStack();
        app.beep();
        alert(inputFiles.length + ' files stacked!');
        app.displayDialogs = savedDisplayDialogs;
        app.preferences.rulerUnits = origUnits;


        // Functions

        function reverseLayerStack() {
            var idreverse = stringIDToTypeID("reverse");
            var desc4653 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref2335 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idordinal = stringIDToTypeID("ordinal");
            var idtargetEnum = stringIDToTypeID("targetEnum");
            ref2335.putEnumerated(idlayer, idordinal, idtargetEnum);
            desc4653.putReference(idnull, ref2335);
            executeAction(idreverse, desc4653, DialogModes.NO);
        }

        function align2SelectAll(method) {
            /*
            AdLf = Align Left
            AdRg = Align Right
            AdCH = Align Centre Horizontal
            AdTp = Align Top
            AdBt = Align Bottom
            AdCV = Align Centre Vertical
            */
            app.activeDocument.selection.selectAll();
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            desc.putReference(charIDToTypeID("null"), ref);
            desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
            try {
                executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
            } catch (e) {}
            app.activeDocument.selection.deselect();
        }
    })();

} else {
    alert('Please close all open files before running this script...');
}
 

MarshySwamp

Active Member
Messages
34
Likes
10
Another way, is to call the Adobe script from a script, bypassing the interface:

JavaScript:
var inputFolder = Folder.selectDialog("Select the input folder");
var fileList = inputFolder.getFiles(/\.(jpg|jpeg|bmp|tif|tiff|psd|psb|tga|gif|png)$/i);
if (fileList != "") stackFiles(fileList);

function stackFiles(sFiles) {
    var loadLayersFromScript = true;
    var SCRIPTS_FOLDER = decodeURI(app.path + '/' + localize('$$$/ScriptingSupport/InstalledScripts=Presets/Scripts'));
    $.evalFile(new File(SCRIPTS_FOLDER + '/Load Files into Stack.jsx'));
    loadLayers.intoStack(sFiles);
};
 

Top