Skip to main content
Participant
November 10, 2020
Answered

Batch processing multiple images into one layered file

  • November 10, 2020
  • 4 replies
  • 585 views

Hopefully someone can help. I need to create some type of script or action to do the following...

 

I have 8000+ images in one folder named as follows:

image1a.psd
image1b.psd
image2a.psd
image2b.psd

etc.

all the way to image 4000a.psd & image4000b.psd

 

I need to combine "image1a.psd" and "image1b.psd" as layers into a single .psd file for all 8000+ images.

 

How can I accomplish this?

 

Thanks!

 

 

This topic has been closed for replies.
Correct answer Chuck Uebele

I just realized that in the lastest version of PS, you can ungroup smart objects. So I changed the code to that it won't flatten the "b" image.

#target photoshop
var srcFolder = new Folder('~/desktop/combine-test/');
var mask = '*a.psd'
var fileList = srcFolder.getFiles (mask);
var psdOptions = new PhotoshopSaveOptions()
psdOptions.layers = true

for(var i=0;i<fileList.length;i++){
    var sndFileName = fileList[i].name.substring (0, fileList[i].name.length-5)
    var sndFile = new File(srcFolder + '/' + sndFileName + 'b.psd')
    var doc = open(fileList[i]);
    placeFile (sndFile);
    undoSO ();
    doc.saveAs (new File(srcFolder +'/' + sndFileName + 'c.psd'), psdOptions)
    doc.close (SaveOptions.DONOTSAVECHANGES);
    }

function undoSO(){
    var idplacedLayerConvertToLayers = stringIDToTypeID( "placedLayerConvertToLayers" );
    executeAction( idplacedLayerConvertToLayers, undefined, DialogModes.NO );    
    
    var idungroupLayersEvent = stringIDToTypeID( "ungroupLayersEvent" );
        var desc7 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref3 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref3.putEnumerated( idLyr, idOrdn, idTrgt );
        desc7.putReference( idnull, ref3 );
    executeAction( idungroupLayersEvent, desc7, DialogModes.NO );    
    }

function placeFile(file){
    var idPlc = charIDToTypeID( "Plc " );
        var desc12 = new ActionDescriptor();
        var idIdnt = charIDToTypeID( "Idnt" );
        desc12.putInteger( idIdnt, 2 );
        var idnull = charIDToTypeID( "null" );
        desc12.putPath( idnull, new File( file ) );
        var idFTcs = charIDToTypeID( "FTcs" );
        var idQCSt = charIDToTypeID( "QCSt" );
        var idQcsa = charIDToTypeID( "Qcsa" );
        desc12.putEnumerated( idFTcs, idQCSt, idQcsa );
        var idOfst = charIDToTypeID( "Ofst" );
            var desc13 = new ActionDescriptor();
            var idHrzn = charIDToTypeID( "Hrzn" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc13.putUnitDouble( idHrzn, idPxl, 0.000000 );
            var idVrtc = charIDToTypeID( "Vrtc" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc13.putUnitDouble( idVrtc, idPxl, 0.000000 );
        var idOfst = charIDToTypeID( "Ofst" );
        desc12.putObject( idOfst, idOfst, desc13 );
    executeAction( idPlc, desc12, DialogModes.NO );   
    }

4 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
November 11, 2020

I just realized that in the lastest version of PS, you can ungroup smart objects. So I changed the code to that it won't flatten the "b" image.

#target photoshop
var srcFolder = new Folder('~/desktop/combine-test/');
var mask = '*a.psd'
var fileList = srcFolder.getFiles (mask);
var psdOptions = new PhotoshopSaveOptions()
psdOptions.layers = true

for(var i=0;i<fileList.length;i++){
    var sndFileName = fileList[i].name.substring (0, fileList[i].name.length-5)
    var sndFile = new File(srcFolder + '/' + sndFileName + 'b.psd')
    var doc = open(fileList[i]);
    placeFile (sndFile);
    undoSO ();
    doc.saveAs (new File(srcFolder +'/' + sndFileName + 'c.psd'), psdOptions)
    doc.close (SaveOptions.DONOTSAVECHANGES);
    }

function undoSO(){
    var idplacedLayerConvertToLayers = stringIDToTypeID( "placedLayerConvertToLayers" );
    executeAction( idplacedLayerConvertToLayers, undefined, DialogModes.NO );    
    
    var idungroupLayersEvent = stringIDToTypeID( "ungroupLayersEvent" );
        var desc7 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref3 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref3.putEnumerated( idLyr, idOrdn, idTrgt );
        desc7.putReference( idnull, ref3 );
    executeAction( idungroupLayersEvent, desc7, DialogModes.NO );    
    }

function placeFile(file){
    var idPlc = charIDToTypeID( "Plc " );
        var desc12 = new ActionDescriptor();
        var idIdnt = charIDToTypeID( "Idnt" );
        desc12.putInteger( idIdnt, 2 );
        var idnull = charIDToTypeID( "null" );
        desc12.putPath( idnull, new File( file ) );
        var idFTcs = charIDToTypeID( "FTcs" );
        var idQCSt = charIDToTypeID( "QCSt" );
        var idQcsa = charIDToTypeID( "Qcsa" );
        desc12.putEnumerated( idFTcs, idQCSt, idQcsa );
        var idOfst = charIDToTypeID( "Ofst" );
            var desc13 = new ActionDescriptor();
            var idHrzn = charIDToTypeID( "Hrzn" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc13.putUnitDouble( idHrzn, idPxl, 0.000000 );
            var idVrtc = charIDToTypeID( "Vrtc" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc13.putUnitDouble( idVrtc, idPxl, 0.000000 );
        var idOfst = charIDToTypeID( "Ofst" );
        desc12.putObject( idOfst, idOfst, desc13 );
    executeAction( idPlc, desc12, DialogModes.NO );   
    }
Chuck Uebele
Community Expert
Community Expert
November 10, 2020

Well, you can try this script. It will combine two file if the naming is how you state with the files ending in a and b. It will flatten the b file. You can comment out the rasterizing function (ras()) to leave it as a smart object. It will save the file as "c" in the same folder. You will need to change the folder name.

 

#target photoshop
var srcFolder = new Folder('~/desktop/combine-test/');
var mask = '*a.psd'
var fileList = srcFolder.getFiles (mask);
var psdOptions = new PhotoshopSaveOptions()
psdOptions.layers = true

for(var i=0;i<fileList.length;i++){
    var sndFileName = fileList[i].name.substring (0, fileList[i].name.length-5)
    var sndFile = new File(srcFolder + '/' + sndFileName + 'b.psd')
    var doc = open(fileList[i]);
    placeFile (sndFile);
    ras ();
    doc.saveAs (new File(srcFolder +'/' + sndFileName + 'c.psd'), psdOptions)
    doc.close (SaveOptions.DONOTSAVECHANGES);
    }

    

function placeFile(file){
    var idPlc = charIDToTypeID( "Plc " );
        var desc12 = new ActionDescriptor();
        var idIdnt = charIDToTypeID( "Idnt" );
        desc12.putInteger( idIdnt, 2 );
        var idnull = charIDToTypeID( "null" );
        desc12.putPath( idnull, new File( file ) );
        var idFTcs = charIDToTypeID( "FTcs" );
        var idQCSt = charIDToTypeID( "QCSt" );
        var idQcsa = charIDToTypeID( "Qcsa" );
        desc12.putEnumerated( idFTcs, idQCSt, idQcsa );
        var idOfst = charIDToTypeID( "Ofst" );
            var desc13 = new ActionDescriptor();
            var idHrzn = charIDToTypeID( "Hrzn" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc13.putUnitDouble( idHrzn, idPxl, 0.000000 );
            var idVrtc = charIDToTypeID( "Vrtc" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc13.putUnitDouble( idVrtc, idPxl, 0.000000 );
        var idOfst = charIDToTypeID( "Ofst" );
        desc12.putObject( idOfst, idOfst, desc13 );
    executeAction( idPlc, desc12, DialogModes.NO );   
    }

function ras(){
       var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );
        var desc16 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref1.putEnumerated( idLyr, idOrdn, idTrgt );
        desc16.putReference( idnull, ref1 );
    executeAction( idrasterizeLayer, desc16, DialogModes.NO );
    } 
tim33625Author
Participant
November 10, 2020

Thank you, I will give it a try. 

Chuck Uebele
Community Expert
Community Expert
November 10, 2020

So what are your specific needs? The easiest, would be to combine all the files and save them as a new file, then you can go back and do whatever editing you need to do.

tim33625Author
Participant
November 10, 2020

Yes, I understand but to do that manually would take too long. For example, it takes me 30 seconds to open the files and combine layers, then save as a new file... working non-stop 8 hours / day it would take me 3 1/2 weeks to complete. Obviously I need an automated solution 😞

Chuck Uebele
Community Expert
Community Expert
November 10, 2020

No, what I mean is if you combine the two files, what do you want to do with them? Of do you just want to combine the files and save?

Chuck Uebele
Community Expert
Community Expert
November 10, 2020

There have been some other requests like this. You might want to do a search and see if you can fine something that works for you.

tim33625Author
Participant
November 10, 2020

Thank you, I have looked at other posts with similar questions, but have not yet been able to find a solution for my specific need...