Skip to main content
July 6, 2015
Answered

Applying files in PS in bulk

  • July 6, 2015
  • 2 replies
  • 2305 views

I use the script below which turns every group into a JPEG. It works perfectly. I select a PNG design to go into a smart layer in Photoshop which then sits on an image of a product.

My question is, I have 3000 designs. Is there a way to make a large selection of PNG files that I want importing into Photoshop, to put into the smart layer I have arranged, close and save the smart layer so it is then applied to each product, and then using this script to save each group as a JPEG?

Many thanks for any help!

#target photoshop 
function main(){ 
    if(!documents.length) return; 
    var doc = activeDocument; 
    var oldPath = activeDocument.path; 
    for(var a=0;a<doc.layerSets.length;a++){ 
        activeDocument.activeLayer =

activeDocument.layers.getByName(doc.layerSets.name); 

        dupLayers(); 
        activeDocument.mergeVisibleLayers(); 
        activeDocument.trim

(TrimType.TRANSPARENT,true,true,true,true); 

        var saveFile= File(oldPath +"/NAMEHERE

-"+doc.layerSets.name +".jpg"); 

        saveJPG(saveFile,10); 
        app.activeDocument.close

(SaveOptions.DONOTSAVECHANGES); 

   
main(); 
function dupLayers() { 
    var desc143 = new ActionDescriptor(); 
    var ref73 = new ActionReference(); 
    ref73.putClass( charIDToTypeID('Dcmn') ); 
    desc143.putReference( charIDToTypeID('null'), ref73 ); 
    desc143.putString( charIDToTypeID('Nm  '),

activeDocument.activeLayer.name ); 

    var ref74 = new ActionReference(); 
    ref74.putEnumerated( charIDToTypeID('Lyr '),

charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 

    desc143.putReference( charIDToTypeID('Usng'), ref74 ); 
    executeAction( charIDToTypeID('Mk  '), desc143,

DialogModes.NO ); 

function saveJPG(saveFile, jpegQuality) { 
    app.activeDocument.flatten();  
    jpgSaveOptions = new JPEGSaveOptions(); 
    jpgSaveOptions.embedColorProfile = true; 
    jpgSaveOptions.formatOptions =

FormatOptions.STANDARDBASELINE; 

    jpgSaveOptions.matte = MatteType.NONE; 
    jpgSaveOptions.quality = jpegQuality; //1-12 
    activeDocument.saveAs(saveFile, jpgSaveOptions,

true,Extension.LOWERCASE); 

This topic has been closed for replies.
Correct answer

c.pfaffenbichler I want to thank you so much for taking your time out to help me. I can't tell you how easy you've made this job for me. I'm so grateful for your kind help, thank you!!


// select files and place in a selected smart object; 
// to cause the so in the so to be scaled to fit the

resolution is temprarily changed, but this is no guarantee for

this to happen; 

// 2015, use it at your own risk; 
#target photoshop 
if (app.documents.length > 0) { 
var myDocument = app.activeDocument; 
var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1]; 
var thePath = myDocument.path; 
var theLayer = myDocument.activeLayer; 
// psd options; 
psdOpts = new PhotoshopSaveOptions(); 
psdOpts.embedColorProfile = true; 
psdOpts.alphaChannels = true; 
psdOpts.layers = true; 
psdOpts.spotColors = true; 
// check if layer is smart object; 
if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert

("selected layer is not a smart object")} 

else { 
if ($.os.search(/windows/i) != -1) {var theFiles =

File.openDialog ("please select files",

"*.pdf;*.ai;*.psd;*.tif;*.png", true)} 

else {var theFiles = File.openDialog ("please select

files", getFiles, true)}; 

if (theFiles) { 
// set to pixels; 
var originalRulerUnits = app.preferences.rulerUnits; 
app.preferences.rulerUnits = Units.PIXELS; 
// get bounds; 
var theBounds = theLayer.bounds; 
// work through the array; 
for (var m = 0; m < theFiles.length; m++) { 
var theNewName = File(theFiles).name.match(/(.*)\.[^

\.]+$/)[1]; 

// replace smart object; 
placeSmartObjectInSmartObject

(app.activeDocument.activeLayer, theFiles); 

#target photoshop 
function main(){ 
    if(!documents.length) return; 
    var doc = activeDocument; 
    var oldPath = activeDocument.path; 
    for(var a=0;a<doc.layerSets.length;a++){ 
        activeDocument.activeLayer =

activeDocument.layers.getByName(doc.layerSets.name); 

        dupLayers(); 
        activeDocument.mergeVisibleLayers(); 
        activeDocument.trim

(TrimType.TRANSPARENT,true,true,true,true);

    var saveFile= File(oldPath+"/PRE-NAME"+theNewName

+"-"+doc.layerSets.name +".jpg");

     saveJPG(saveFile,10); 

        app.activeDocument.close

(SaveOptions.DONOTSAVECHANGES); 

   
main(); 
function dupLayers() { 
    var desc143 = new ActionDescriptor(); 
    var ref73 = new ActionReference(); 
    ref73.putClass( charIDToTypeID('Dcmn') ); 
    desc143.putReference( charIDToTypeID('null'), ref73 ); 
    desc143.putString( charIDToTypeID('Nm  '),

activeDocument.activeLayer.name ); 

    var ref74 = new ActionReference(); 
    ref74.putEnumerated( charIDToTypeID('Lyr '),

charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 

    desc143.putReference( charIDToTypeID('Usng'), ref74 ); 
    executeAction( charIDToTypeID('Mk  '), desc143,

DialogModes.NO ); 

function saveJPG(saveFile, jpegQuality) { 
    app.activeDocument.flatten();  
    jpgSaveOptions = new JPEGSaveOptions(); 
    jpgSaveOptions.embedColorProfile = true; 
    jpgSaveOptions.formatOptions =

FormatOptions.STANDARDBASELINE; 

    jpgSaveOptions.matte = MatteType.NONE; 
    jpgSaveOptions.quality = jpegQuality; //1-12 
    activeDocument.saveAs(saveFile, jpgSaveOptions,

true,Extension.LOWERCASE); 

// undo; 

myDocument.activeHistoryState = myDocument.historyStates

[myDocument.historyStates.length-2] 

}; 

// reset; 

originalRulerUnits = app.preferences.rulerUnits; 

}; 

////// open smart object ////// 
function placeSmartObjectInSmartObject (theLayer, thisFile)

if (theLayer.kind == "LayerKind.SMARTOBJECT") { 
// get prefernces setting; 
var ref = new ActionReference(); 
ref.putProperty(stringIDToTypeID("property"),

stringIDToTypeID("generalPreferences")); 

ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID

("Ordn"), charIDToTypeID("Trgt") ); 

var applicationDesc = executeActionGet(ref); 
var generalDesc = applicationDesc.getObjectValue

(stringIDToTypeID("generalPreferences")); 

var resizeDuringPlace = generalDesc.getBoolean

(stringIDToTypeID("resizePastePlace")); 

if (resizeDuringPlace == false) { 
// ======================================================= 
var idsetd = charIDToTypeID( "setd" ); 
    var desc1 = new ActionDescriptor(); 
    var idnull = charIDToTypeID( "null" ); 
        var ref1 = new ActionReference(); 
        var idPrpr = charIDToTypeID( "Prpr" ); 
        var idGnrP = charIDToTypeID( "GnrP" ); 
        ref1.putProperty( idPrpr, idGnrP ); 
        var idcapp = charIDToTypeID( "capp" ); 
        var idOrdn = charIDToTypeID( "Ordn" ); 
        var idTrgt = charIDToTypeID( "Trgt" ); 
        ref1.putEnumerated( idcapp, idOrdn, idTrgt ); 
    desc1.putReference( idnull, ref1 ); 
    var idT = charIDToTypeID( "T  " ); 
        var desc2 = new ActionDescriptor(); 
        var idresizePastePlace = stringIDToTypeID(

"resizePastePlace" ); 

        desc2.putBoolean( idresizePastePlace, true ); 
        var idlegacyPathDrag = stringIDToTypeID(

"legacyPathDrag" ); 

        desc2.putBoolean( idlegacyPathDrag, true ); 
    var idGnrP = charIDToTypeID( "GnrP" ); 
    desc1.putObject( idT, idGnrP, desc2 ); 
executeAction( idsetd, desc1, DialogModes.NO ); 
}; 
// ======================================================= 
var idplacedLayerEditContents = stringIDToTypeID(

"placedLayerEditContents" ); 

    var desc2 = new ActionDescriptor(); 
executeAction( idplacedLayerEditContents, desc2,

DialogModes.NO ); 

// set to high resolution to make scaling on placing

probable; 

var theResolution = activeDocument.resolution; 
activeDocument.resizeImage(undefined, undefined, 3000,

ResampleMethod.NONE); 

// place so; 
// ======================================================= 
var idPlc = charIDToTypeID( "Plc " ); 
    var desc4 = new ActionDescriptor(); 
    var idAs = charIDToTypeID( "As  " ); 
        var desc5 = new ActionDescriptor(); 
        var idfsel = charIDToTypeID( "fsel" ); 
        var idpdfSelection = stringIDToTypeID(

"pdfSelection" ); 

        var idpage = stringIDToTypeID( "page" ); 
        desc5.putEnumerated( idfsel, idpdfSelection, idpage

); 

        var idPgNm = charIDToTypeID( "PgNm" ); 
        desc5.putInteger( idPgNm, 1 ); 
        var idCrop = charIDToTypeID( "Crop" ); 
        var idcropTo = stringIDToTypeID( "cropTo" ); 
        var idtrimBox = stringIDToTypeID( "trimBox" ); 
        desc5.putEnumerated( idCrop, idcropTo, idtrimBox );

    var idPDFG = charIDToTypeID( "PDFG" ); 
    desc4.putObject( idAs, idPDFG, desc5 ); 
    var idnull = charIDToTypeID( "null" ); 
    desc4.putPath( idnull, new File( thisFile ) ); 
    var idFTcs = charIDToTypeID( "FTcs" ); 
    var idQCSt = charIDToTypeID( "QCSt" ); 
    var idQcsa = charIDToTypeID( "Qcsa" ); 
    desc4.putEnumerated( idFTcs, idQCSt, idQcsa ); 
    var idOfst = charIDToTypeID( "Ofst" ); 
        var desc6 = new ActionDescriptor(); 
        var idHrzn = charIDToTypeID( "Hrzn" ); 
        var idRlt = charIDToTypeID( "#Rlt" ); 
        desc6.putUnitDouble( idHrzn, idRlt, 0.000000 ); 
        var idVrtc = charIDToTypeID( "Vrtc" ); 
        var idRlt = charIDToTypeID( "#Rlt" ); 
        desc6.putUnitDouble( idVrtc, idRlt, 0.000000 ); 
    var idOfst = charIDToTypeID( "Ofst" ); 
    desc4.putObject( idOfst, idOfst, desc6 ); 
    var idAntA = charIDToTypeID( "AntA" ); 
    desc4.putBoolean( idAntA, true ); 
executeAction( idPlc, desc4, DialogModes.NO ); 
// toggle visibility of others; 
// ======================================================= 
var idShw = charIDToTypeID( "Shw " ); 
    var desc10 = new ActionDescriptor(); 
    var idnull = charIDToTypeID( "null" ); 
        var list4 = new ActionList(); 
            var ref7 = new ActionReference(); 
            var idLyr = charIDToTypeID( "Lyr " ); 
            var idOrdn = charIDToTypeID( "Ordn" ); 
            var idTrgt = charIDToTypeID( "Trgt" ); 
            ref7.putEnumerated( idLyr, idOrdn, idTrgt ); 
        list4.putReference( ref7 ); 
    desc10.putList( idnull, list4 ); 
    var idTglO = charIDToTypeID( "TglO" ); 
    desc10.putBoolean( idTglO, true ); 
executeAction( idShw, desc10, DialogModes.NO ); 
// reset resolution; 
activeDocument.resizeImage(undefined, undefined,

theResolution, ResampleMethod.NONE); 

// close; 
app.activeDocument.close(SaveOptions.SAVECHANGES) 
}; 
}; 
////// get psds, tifs and jpgs from files ////// 
function getFiles (theFile) { 
if (theFile.name.match(/\.(pdf|ai|jpg|tif|psb|psd)$/i)) { 
return true 
}; 
}; 
d1g1talphyre
Inspiring
July 6, 2015

ah, this is something I actually do all day. 

So,  in photoshop start a new action, tab back to bridge, open the file.  Then go to File > Scripts > browse.  Select the script you want to run.  Let it run.  I didn't pay much attention if your script saves the files or not.  If not, save the file to the location you want.

Stop recording the action.

Go to bridge, go to Tools > Photoshop > batch.  Now you can select the action you just recorded in photoshop.  A new window will pop up,  very important you select the check boxes correctly on this. 

in the area named play, select the action you want to run.  In the "Source" area directly under it, check Override Action "Open Commands"  Suppress File open Options Dialogs, and Suppress Color Profile Warnings.  Do not check include all subfolders unless you are running this on subfolders   next in Destination: leave it to none.  Its easier to use photoshops actions or script to tell it where you want files to go. 

Also make sure Errors: Stop for errors is selected. 

Now, click okay, and sit back and watch.  I process hundreds or RAW phase one camera images every day, and this is by far the easiest way to size, color balance, and contrast folder after folder of images.  I hope this helps.

July 7, 2015

I don't fully understand what you mean. I have several issues; for me, Bridge opens in a seperate window. I have Photoshop CC 2015. You say 'open a file in Bridge' - does that mean editting the smart object and then placing a file from Bridge into that smart object? I tried that and recorded it as an action but it didn't work - the batch didn't select the other designs I have in the same folder.

Chuck Uebele
Community Expert
Community Expert
July 6, 2015

If I'm understanding correctly, you can assign your script to an action then run the action using the batch script in Bridge or PS. You can also script something that gets all the files from a folder and loops through them to apply your script.

July 6, 2015

Chuck Uebele wrote:

You can also script something that gets all the files from a folder and loops through them to apply your script.

That's great. How would I go about writing a script that gets all files from a folder and puts them into a smart layer and runs my default script in the previous post?

It's hard to explain sometimes exactly what you want to do in a script so it makes it harder to find on the internet.

c.pfaffenbichler
Community Expert
Community Expert
July 6, 2015

Maybe this can be useful for your case:

Re: Batch replace smart objects