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

Script to open files and layer them?

Community Beginner ,
Nov 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

Hi, I was wondering if a direction could be given on how I might go about creating a script that would open file "0001.tif" from folder A and file "0001.tif" from folder B and layer the folder B image into folder A image, saves and closes, as well as, keeping a counter so that the script then moves on to check and open image "0002.tif" and so on until it doesn't find any more images. Thoughts? Is it possible?

 

A long time ago I did Actionscript, which was like Javascript, but most of that knowledge has faded.

 

Any advice on how to approach this or how to start would be amazing. Thanks!

TOPICS
Actions and scripting , Windows

Views

1.1K

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

correct answers 1 Correct answer

Community Expert , Nov 12, 2021 Nov 12, 2021
//community.adobe.com/t5/photoshop/auto-merge-files/m-p/10753387
// Auto merge files

// Reference code:
// https://forums.adobe.com/thread/2116421
// https://forums.adobe.com/message/8597315#8597315

#target photoshop

var folder1 = Folder.selectDialog("Please select the base folder with files to process");  // input folder 1
var folder2 = Folder.selectDialog("Please select the layered folder with files to process");  // input folder 2 
var saveFolder = Folder.selectDialog("Please select the fo
...

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

Try the following four scripts, adjustments can be made as required...

 

Instructions here:

 

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

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not word-processor)
  3. Paste the code in
  4. Save the text file as .txt
  5. Rename the text .txt to .jsx
  6. Install or browse to the .jsx file to run

 

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 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

/*
Photoshop script to auto stack pair of images, then auto-align, and export.
https://community.adobe.com/t5/photoshop/photoshop-script-to-auto-stack-pair-of-images-then-auto-align-and-export/td-p/11989171

Auto Align Files from 2 Folders to TIFF.jsx
Stephen Marsh
Version 1.4 - 6th May 2021
*/

#target photoshop

    (function () {

        if (app.documents.length === 0) {

            // Save the current dialog display settings
            var savedDisplayDialogs = app.displayDialogs;
            app.displayDialogs = DialogModes.NO;

            // Input folder 1
            var folder1 = Folder.selectDialog("Please select the originals folder");
            if (folder1 === null) return;

            // Input folder 2
            var folder2 = Folder.selectDialog("Please select the processed files folder");
            if (folder2 === null) return;

            // Validate input folder selection
            var validateInputDir = (folder1.fsName === folder2.fsName);
            if (validateInputDir === true) {
                alert("Script cancelled as both the input folders are the same");
                return;
            }

            var searchMask = '*.???';
            var list1 = folder1.getFiles(searchMask);
            var list2 = folder2.getFiles(searchMask);
            // Alpha-numeric sort
            list1.sort();
            list2.sort();

            // Validate that folder 1 & 2 lists are not empty 
            var validateEmptyList = (list1.length > 0 && list2.length > 0);
            if (validateEmptyList === false) {
                alert("Script cancelled as one of the input folders is empty");
                return;
            }

            // Validate that the item count in folder 1 & 2 matches
            var validateListLength = (list1.length === list2.length);
            if (validateListLength === false) {
                alert("Script cancelled as the input folders don't have equal quantities of images");
                return;
            }

            // Output folder
            var saveFolder = Folder.selectDialog("Please select the folder to save to");
            if (saveFolder === null) return;

            for (var i = 0; i < list1.length; i++) {

                var doc = open(list1[i]);
                var docName = doc.name.replace(/\.[^\.]+$/, '');
                backgroundLayerNameToVariableName(docName, 100, 2);

                placeFile(list2[i], 100);
                app.runMenuItem(stringIDToTypeID('rasterizePlaced'));

                // Action set to run
                var actionSet = "AUTO-ALIGN-SET2";
                // Action to run from set
                var actionName = "AUTO_ALIGN2";
                // Run it!
                app.doAction(actionName, actionSet);

                tiffSaveOptions = new TiffSaveOptions();
                tiffSaveOptions.embedColorProfile = true;
                tiffSaveOptions.byteOrder = ByteOrder.IBM;
                tiffSaveOptions.transparency = true;
                tiffSaveOptions.layers = false;
                tiffSaveOptions.layerCompression = LayerCompression.ZIP;
                tiffSaveOptions.interleaveChannels = true;
                tiffSaveOptions.alphaChannels = true;
                tiffSaveOptions.annotations = true;
                tiffSaveOptions.spotColors = true;
                tiffSaveOptions.saveImagePyramid = false;
                tiffSaveOptions.imageCompression = TIFFEncoding.NONE;

                doc.saveAs(new File(saveFolder + '/' + docName + '.tif'), tiffSaveOptions);
                doc.close(SaveOptions.DONOTSAVECHANGES);
            }

            alert('Script completed!' + '\n' + 'Files saved to:' + '\r' + saveFolder.fsName);

            // Restore the dialogs
            app.displayDialogs = savedDisplayDialogs;

            // Here be functions

            function placeFile(file, scale) {
                var idPlc = charIDToTypeID("Plc ");
                var desc2 = new ActionDescriptor();
                var idnull = charIDToTypeID("null");
                desc2.putPath(idnull, new File(file));
                var idFTcs = charIDToTypeID("FTcs");
                var idQCSt = charIDToTypeID("QCSt");
                var idQcsa = charIDToTypeID("Qcsa");
                desc2.putEnumerated(idFTcs, idQCSt, idQcsa);
                var idOfst = charIDToTypeID("Ofst");
                var desc3 = new ActionDescriptor();
                var idHrzn = charIDToTypeID("Hrzn");
                var idPxl = charIDToTypeID("#Pxl");
                desc3.putUnitDouble(idHrzn, idPxl, 0.000000);
                var idVrtc = charIDToTypeID("Vrtc");
                var idPxl = charIDToTypeID("#Pxl");
                desc3.putUnitDouble(idVrtc, idPxl, 0.000000);
                var idOfst = charIDToTypeID("Ofst");
                desc2.putObject(idOfst, idOfst, desc3);
                var idWdth = charIDToTypeID("Wdth");
                var idPrc = charIDToTypeID("#Prc");
                desc2.putUnitDouble(idWdth, idPrc, scale);
                var idHght = charIDToTypeID("Hght");
                var idPrc = charIDToTypeID("#Prc");
                desc2.putUnitDouble(idHght, idPrc, scale);
                var idAntA = charIDToTypeID("AntA");
                desc2.putBoolean(idAntA, true);
                executeAction(idPlc, desc2, DialogModes.NO);
            }

            function backgroundLayerNameToVariableName(layerName, opacity, layerID) {
                var c2t = function (s) {
                    return app.charIDToTypeID(s);
                };
                var s2t = function (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(c2t("null"), reference);
                descriptor2.putString(s2t("name"), layerName);
                descriptor2.putUnitDouble(s2t("opacity"), s2t("percentUnit"), opacity);
                descriptor2.putEnumerated(s2t("mode"), s2t("blendMode"), s2t("normal"));
                descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
                descriptor.putInteger(s2t("layerID"), layerID);
                executeAction(s2t("set"), descriptor, DialogModes.NO);
            }
        }

        else {

            alert('Please close all open documents before running this script!');

        }

    }());

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 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

 

Wrong code posted!

 

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 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

// combine images from  two folders;
// 2016, use it at your own risk;
#target photoshop
// select folders;
var theFolder1 = Folder.selectDialog("select folder");
if (theFolder1) {
    var theFolder2 = Folder.selectDialog("select other folder");
    if (theFolder2 && String(theFolder1) != String(theFolder2)) {
        // get files;
        var theFiles1 = theFolder1.getFiles(/\.(jpg|tif|eps|psd)$/i);
        var theFiles2 = theFolder2.getFiles(/\.(jpg|tif|eps|psd)$/i);
        // if more than one image is in the folder and equal number in both;
        if (theFiles1.length > 0 && theFiles2.length == theFiles1.length) {
            // tif options;
            tifOpts = new TiffSaveOptions();
            tifOpts.embedColorProfile = true;
            tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
            tifOpts.alphaChannels = false;
            tifOpts.byteOrder = ByteOrder.MACOS;
            tifOpts.layers = true;
            // do the operation;
            for (var m = 0; m < theFiles1.length; m++) {
                var theFile = theFiles1[m];
                var theOtherFile = theFiles2[m];
                // create file;
                var myDocument = app.open(File(theFile));
                var docName = myDocument.name;
                var basename = docName.match(/(.*)\.[^\.]+$/)[1];
                var docPath = myDocument.path;
                // place the corresponding file;
                placeScaleFile(theOtherFile, 0, 0, 100);
                //save tif as a copy:
                myDocument.saveAs((new File(Folder(theFolder1).parent + "/" + basename + "_combined" + ".tif")), tifOpts, true);
                myDocument.close(SaveOptions.DONOTSAVECHANGES)
            }
        };
        alert("done")
    }
};
////// get from subfolders //////
function retrieveJPGs(theFolder, theFiles) {
    if (!theFiles) { var theFiles = [] };
    var theContent = theFolder.getFiles();
    for (var n = 0; n < theContent.length; n++) {
        var theObject = theContent[n];
        if (theObject.constructor.name == "Folder") {
            theFiles = retrieveJPGs(theObject, theFiles)
        };
        if (theObject.name.slice(-4) == ".jpg" || theObject.name.slice(-4) == ".JPG") {
            theFiles = theFiles.concat(theObject)
        }
    };
    return theFiles
};
////// place //////
function placeScaleFile(file, xOffset, yOffset, theScale) {
    // =======================================================
    var idPlc = charIDToTypeID("Plc ");
    var desc5 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc5.putPath(idnull, new File(file));
    var idFTcs = charIDToTypeID("FTcs");
    var idQCSt = charIDToTypeID("QCSt");
    var idQcsa = charIDToTypeID("Qcsa");
    desc5.putEnumerated(idFTcs, idQCSt, idQcsa);
    var idOfst = charIDToTypeID("Ofst");
    var desc6 = new ActionDescriptor();
    var idHrzn = charIDToTypeID("Hrzn");
    var idPxl = charIDToTypeID("#Pxl");
    desc6.putUnitDouble(idHrzn, idPxl, xOffset);
    var idVrtc = charIDToTypeID("Vrtc");
    var idPxl = charIDToTypeID("#Pxl");
    desc6.putUnitDouble(idVrtc, idPxl, yOffset);
    var idOfst = charIDToTypeID("Ofst");
    desc5.putObject(idOfst, idOfst, desc6);
    var idWdth = charIDToTypeID("Wdth");
    var idPrc = charIDToTypeID("#Prc");
    desc5.putUnitDouble(idWdth, idPrc, theScale);
    var idHght = charIDToTypeID("Hght");
    var idPrc = charIDToTypeID("#Prc");
    desc5.putUnitDouble(idHght, idPrc, theScale);
    var idLnkd = charIDToTypeID("Lnkd");
    desc5.putBoolean(idLnkd, false);
    executeAction(idPlc, desc5, DialogModes.NO);
    return app.activeDocument.activeLayer;
};

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 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

//community.adobe.com/t5/photoshop/auto-merge-files/m-p/10753387
// Auto merge files

// Reference code:
// https://forums.adobe.com/thread/2116421
// https://forums.adobe.com/message/8597315#8597315

#target photoshop

var folder1 = Folder.selectDialog("Please select the base folder with files to process");  // input folder 1
var folder2 = Folder.selectDialog("Please select the layered folder with files to process");  // input folder 2 
var saveFolder = Folder.selectDialog("Please select the folder to save to...");  // output folder
var searchMask = '*.???';
var list1 = folder1.getFiles(searchMask);
var list2 = folder2.getFiles(searchMask);
list1.sort(); // Alpha-numeric sort
list2.sort(); // Alpha-numeric sort

app.displayDialogs = DialogModes.NO;

var psdOptions = new PhotoshopSaveOptions();
psdOptions.embedColorProfile = true;
psdOptions.alphaChannels = true;
psdOptions.layers = true;
psdOptions.spotColors = true;

for (var i = 0; i < list1.length; i++) {
    var doc = open(list1[i]);
    var docName = doc.name;
    placeFile(list2[i], 100);
    doc.activeLayer.blendMode = BlendMode.MULTIPLY; //change blend mode  
    doc.saveAs(new File(saveFolder + '/' + docName.split('.')[0] + '.psd'), psdOptions);
    doc.close(SaveOptions.DONOTSAVECHANGES);
};

alert('Script completed!' + '\n' + 'Files saved to:' + '\r' + saveFolder.fsName);

function placeFile(file, scale) {
    try {
        var idPlc = charIDToTypeID("Plc ");
        var desc2 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        desc2.putPath(idnull, new File(file));
        var idFTcs = charIDToTypeID("FTcs");
        var idQCSt = charIDToTypeID("QCSt");
        var idQcsa = charIDToTypeID("Qcsa");
        desc2.putEnumerated(idFTcs, idQCSt, idQcsa);
        var idOfst = charIDToTypeID("Ofst");
        var desc3 = new ActionDescriptor();
        var idHrzn = charIDToTypeID("Hrzn");
        var idPxl = charIDToTypeID("#Pxl");
        desc3.putUnitDouble(idHrzn, idPxl, 0.000000);
        var idVrtc = charIDToTypeID("Vrtc");
        var idPxl = charIDToTypeID("#Pxl");
        desc3.putUnitDouble(idVrtc, idPxl, 0.000000);
        var idOfst = charIDToTypeID("Ofst");
        desc2.putObject(idOfst, idOfst, desc3);
        var idWdth = charIDToTypeID("Wdth");
        var idPrc = charIDToTypeID("#Prc");
        desc2.putUnitDouble(idWdth, idPrc, scale);
        var idHght = charIDToTypeID("Hght");
        var idPrc = charIDToTypeID("#Prc");
        desc2.putUnitDouble(idHght, idPrc, scale);
        var idAntA = charIDToTypeID("AntA");
        desc2.putBoolean(idAntA, true);
        executeAction(idPlc, desc2, DialogModes.NO);

    }
    catch (e) { }
}//end function placeFile

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 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

Amazing! Thanks for giving me somewhere to start. Will give these a try and see what I can get out of them.

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 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

What do you want to do once you have one layer stacked over the other?

 

Some of these scripts run an action. Some use blend modes or opacity of the upper layer over the lower layer.

 

Where should the combined file be saved, with/without layers, what name etc?

 

There is a lot to think about.

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 17, 2021 Nov 17, 2021

Copy link to clipboard

Copied

Hi, I simply want to save the file with the layered image, unflattened and it doesn't need to be saved with a new name or in a new folder.

 

Other coding question, and this may not be the thread to ask, but is there a way to skip a dialog box and have a script run automatically?

 

I'm using a script to export layers as files, but it pops up with a dialog box for every layered file when I run it in an action. Is there a way to have it skip having to hit the RUN button or is there a way to add an autorun function upon load of dialogbox?

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 17, 2021 Nov 17, 2021

Copy link to clipboard

Copied

Stephen_A_Marsh_0-1637211470607.png

Hi, I simply want to save the file with the layered image, unflattened and it doesn't need to be saved with a new name or in a new folder.


By @jensou

 

As I said, when it comes to scripting - you need to think about it... There are two separate input folders, so where to save, this is not clear? Input folder A (overwriting), input folder B (overwriting), or somewhere else?

 

EDIT: Apologies, you did explain in your original post which file should be saved/overwritten.

 

As the input files are TIFF, do you wish to save layered TIFF or layered PSD?

 

EDIT: I'll presume layered TIFF.

 

NOTE: The scripts that I posted don't check/match filenames, they just expect/presume that the files in each folder are naturally alphabetically sorting and have the same names so that the correct files are just "naturally" paired.

 

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 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

@jensou â€“ here is a "final" refined version specifically configured for this topic:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-open-files-and-layer-them/m-p/12532657
Script to open files and layer them?
Stephen Marsh, v1.1 - 20th November 2021
Note: It is assumed that the filenames in both folders are the same (or will at least sort as expected).
*/

#target photoshop

    (function () {

        if (app.documents.length === 0) {

            try {

                // Input folder 1
                var folder1 = Folder.selectDialog("Select the first folder:");
                if (folder1 === null) {
                    alert('Script cancelled!');
                    return;
                }

                // Input folder 2
                var folder2 = Folder.selectDialog("Select the second folder to layer:");
                if (folder2 === null) {
                    alert('Script cancelled!');
                    return;
                }

                // Validate input folder selection
                var validateInputDir = (folder1.fsName === folder2.fsName);
                if (validateInputDir === true) {
                    alert("Script cancelled as both the input folders are the same!");
                    return;
                }

                // Save folder is the first input folder
                var saveFolder = folder1;

                // Limit the file input to tiff
                var list1 = folder1.getFiles(/\.(tif|tiff)$/i);
                var list2 = folder2.getFiles(/\.(tif|tiff)$/i);

                // Alpha-numeric sort
                list1.sort();
                list2.sort();

                // Validate that folder 1 & 2 lists are not empty 
                var validateEmptyList = (list1.length > 0 && list2.length > 0);
                if (validateEmptyList === false) {
                    alert("Script cancelled as one of the input folders is empty!");
                    return;
                }

                // Validate that the item count in folder 1 & 2 matches
                var validateListLength = (list1.length === list2.length);
                if (validateListLength === false) {
                    alert("Script cancelled as the input folders don't have equal quantities of images!");
                    return;
                }

                // Save and set the dialog display settings
                var savedDisplayDialogs = app.displayDialogs;
                app.displayDialogs = DialogModes.NO;

                // TIFF save options
                tifOpts = new TiffSaveOptions();
                tifOpts.embedColorProfile = true;
                tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
                tifOpts.alphaChannels = true;
                tifOpts.byteOrder = ByteOrder.MACOS;
                tifOpts.layers = true;

                // Perform the stacking and saving
                for (var i = 0; i < list1.length; i++) {
                    var doc = open(list1[i]);
                    var docName = doc.name;
                    placeFile(list2[i], 100);
                    app.runMenuItem(stringIDToTypeID('rasterizePlaced'));
                    doc.saveAs(new File(saveFolder + '/' + docName), tifOpts);
                    doc.close(SaveOptions.DONOTSAVECHANGES);
                }

                function placeFile(file, scale) {
                    try {
                        var idPlc = charIDToTypeID("Plc ");
                        var desc2 = new ActionDescriptor();
                        var idnull = charIDToTypeID("null");
                        desc2.putPath(idnull, new File(file));
                        var idFTcs = charIDToTypeID("FTcs");
                        var idQCSt = charIDToTypeID("QCSt");
                        var idQcsa = charIDToTypeID("Qcsa");
                        desc2.putEnumerated(idFTcs, idQCSt, idQcsa);
                        var idOfst = charIDToTypeID("Ofst");
                        var desc3 = new ActionDescriptor();
                        var idHrzn = charIDToTypeID("Hrzn");
                        var idPxl = charIDToTypeID("#Pxl");
                        desc3.putUnitDouble(idHrzn, idPxl, 0.000000);
                        var idVrtc = charIDToTypeID("Vrtc");
                        var idPxl = charIDToTypeID("#Pxl");
                        desc3.putUnitDouble(idVrtc, idPxl, 0.000000);
                        var idOfst = charIDToTypeID("Ofst");
                        desc2.putObject(idOfst, idOfst, desc3);
                        var idWdth = charIDToTypeID("Wdth");
                        var idPrc = charIDToTypeID("#Prc");
                        desc2.putUnitDouble(idWdth, idPrc, scale);
                        var idHght = charIDToTypeID("Hght");
                        var idPrc = charIDToTypeID("#Prc");
                        desc2.putUnitDouble(idHght, idPrc, scale);
                        var idAntA = charIDToTypeID("AntA");
                        desc2.putBoolean(idAntA, true);
                        executeAction(idPlc, desc2, DialogModes.NO);
                    } catch (e) { }
                }

                // End of script
                app.displayDialogs = savedDisplayDialogs;
                app.beep();
                var listCount = list1.length;
                alert('Script completed!' + '\n' + listCount + ' layered TIFF files saved to:' + '\r' + saveFolder.fsName);

            } catch (err) {
                alert("An unexpected error has occurred!");
            }

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

 

 

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 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

LATEST

That worked insanely perfectly! I was not even close to being on the right track changing the previous code to work like this does. Thanks so 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