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

REQUEST: Photoshop script - load files to stack INCLUDING layers (in order)

Community Beginner ,
Nov 01, 2023 Nov 01, 2023

Copy link to clipboard

Copied

Title says it all!

 

Looking for a simple script to select a group of 2-20 images and load them into a stack. Some may have one or two layers, and I want them to come with the files and stay in order.

 

I have poked around a lot and haven't found anything like this.... sorry!!

 

Thanks!

TOPICS
Actions and scripting , macOS

Views

279

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
Community Expert ,
Nov 01, 2023 Nov 01, 2023

Copy link to clipboard

Copied

@ethank21106708 – What's the earliest version of Photoshop that will use the script? I have a quick hack to an existing script that may work in Photoshop 2021 (v22) or higher. It could possibly work in earlier versions, I am not sure when the layer panel menu item for "Convert to Layers" was introduced.

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
Community Beginner ,
Nov 01, 2023 Nov 01, 2023

Copy link to clipboard

Copied

2021 or later - basically i have several images that have a few layers that I need to stack but it flattens every time.

 

Thanks!

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
Community Expert ,
Nov 01, 2023 Nov 01, 2023

Copy link to clipboard

Copied

LATEST

@ethank21106708 

 

Try this script, it's a little rough and hasn't had exhaustive testing...

 

No docs should be open, input is the entire top-level of a selected folder and file types can be added or removed via the inputFiles variable. The layer stack will be aligned to the upper left position. The colour mode and profile of the first document opened/stacked will be used for subsequent layers.

 

Update: I have rewritten this code to a 1.1 version so that smart objects are no longer leveraged to retain the layers, this will make the script compatible with older versions of Photoshop that don't have the native option to "unpack" a smart object to layers.

 

/*
Stacker - Retaining Source Layers.jsx
3rd November 2023 - v1.1, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/request-photoshop-script-load-files-to-stack-including-layers-in-order/td-p/14202346
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/combining-tiffs/m-p/13475955
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('Select the folder containing files to stack (retaining layers):');
        if (inputFolder === null) {
            return;
        }

        // Add or remove file extensions as required
        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif|webp)$/i);
        inputFiles.sort().reverse();

        var firstFile = app.open(File(inputFiles[0]));
        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];
        activeDocument = docStack;
        layerFromBackground("Background");

        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);
            }
            selectAllLayers();
            remainingFiles.layers[0].duplicate(docStack, ElementPlacement.PLACEATBEGINNING);
            remainingFiles.close(SaveOptions.DONOTSAVECHANGES);
        }
        
        // Expand the canvas to fit layer content
        activeDocument.revealAll();
        //deselectLayers();
        alert(inputFiles.length + ' files stacked (retaining layers)!');
        app.displayDialogs = savedDisplayDialogs;
        app.preferences.rulerUnits = origUnits;


        ////////// Functions //////////

        function selectAllLayers(ignoreBackground) {
            // Select all layers (doesn't include Background)
            try {
                var desc = new ActionDescriptor();
                var ref = new ActionReference();
                ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
                desc.putReference(charIDToTypeID('null'), ref);
                executeAction(stringIDToTypeID('selectAllLayers'), desc, DialogModes.NO);
            } catch (e) {}
            if (!ignoreBackground) {
                // Add Background Layer to the selection (if it exists)
                try {
                    activeDocument.backgroundLayer;
                    var bgID = activeDocument.backgroundLayer.id;
                    var ref = new ActionReference();
                    var desc = new ActionDescriptor();
                    ref.putIdentifier(charIDToTypeID('Lyr '), bgID);
                    desc.putReference(charIDToTypeID('null'), ref);
                    desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
                    desc.putBoolean(charIDToTypeID('MkVs'), false);
                    executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
                } catch (e) {}
            }
        }

        function deselectLayers() {
            var desc01 = new ActionDescriptor();
            var ref01 = new ActionReference();
            ref01.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
            desc01.putReference(charIDToTypeID('null'), ref01);
            executeAction(stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO);
        }

        function layerFromBackground(layerName) {
            if (activeDocument.hasOwnProperty("backgroundLayer")) {
                function s2t(s) {
                    return app.stringIDToTypeID(s);
                }
                var descriptor = new ActionDescriptor();
                var descriptor2 = new ActionDescriptor();
                var reference = new ActionReference();
                reference.putProperty(s2t("layer"), s2t("background"));
                descriptor.putReference(s2t("null"), reference);
                descriptor2.putString(s2t("name"), layerName);
                descriptor2.putUnitDouble(s2t("opacity"), s2t("percentUnit"), 100);
                descriptor2.putEnumerated(s2t("mode"), s2t("blendMode"), s2t("normal"));
                descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
                executeAction(s2t("set"), descriptor, DialogModes.NO);
            }
        }

    })();

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

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html 

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