Skip to main content
Inspiring
June 30, 2023
Answered

1 PSD process completes but multiple jpg files open continuously. 2 PSD open but JPG not open

  • June 30, 2023
  • 29 replies
  • 2207 views

https://youtu.be/ZrXk-J-HfEM

First PSD File open ans process completes but multiple jpg files open continuously. second process PSD File open but JPG File not open error this Line var jpgDoc = open(jpgFile);

1) Input Psd folder contains many psd files already.
All layers of Input Psd file are already renamed to HFrame and VFrame.

2) Input jpg folder already contains many jpg files.
The number of jpg files can be unlimited

2) outputPSDFolder

(Problem 1. )

A first psd file is opened from the input psd to run the script.
Then jpg are opened one by one from the input jpg folder and
clipping is done in HFrame and VFrame layers.

Work has been completed on all layers.
But jpg file should be stopped open,
psd should be saved.
But all the jpg files of the folder continue to open and do not stop.
After all the jpg files are opened, the psd file is saved in the output.

(Problem 2. )
After the first psd file process is complete,
the psd file is saved and after the next psd file is opened,
the jpg file is not opened.

 

 

// Input PSD file select folder dialog
var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");

// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");

// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");

// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);

function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getFilesInFolder(inputJPGFolder, "jpg");

for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];

// Open the PSD file
var doc = open(psdFile);

for (var j = 0; j < jpgFiles.length; j++) {
var jpgFile = jpgFiles[j];

// Open the JPG file as a separate document
var jpgDoc = open(jpgFile);

var layerName = "";
var isHorizontal = false;

// Determine the layer name based on the orientation of the JPG file
if (jpgDoc.height > jpgDoc.width) {
layerName = "VFrame";
isHorizontal = false;
} else {
layerName = "HFrame";
isHorizontal = true;
}

// Switch to the PSD document

// Check if the PSD document has the target layer
var hasTargetLayer = hasLayer(doc, layerName);

if (hasTargetLayer) {
// Perform actions when the layer exists
app.activeDocument = jpgDoc;
jpgDoc.selection.selectAll();
jpgDoc.selection.copy();

// Close the JPG document
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
Folder((fnp = (fle = (jpgDoc = activeDocument).fullName).parent + '/FINISH/')).create();
jpgDoc.saveAs(File(fnp + jpgDoc.name), new JPEGSaveOptions(JPEGSaveOptions.quality = 12));
jpgDoc.close(SaveOptions.DONOTSAVECHANGES);
File(fle).remove();

app.activeDocument = doc;

var ref = new ActionReference();
ref.putName(stringIDToTypeID('layer'), layerName);
var desc = new ActionDescriptor();
desc.putReference(stringIDToTypeID('null'), ref);
executeAction(stringIDToTypeID('select'), desc, DialogModes.NO);

// Rename the selected layer
var newName = "MM Ravi";
activeDocument.activeLayer.name = newName;

// Create a new layer and paste the contents
var newLayer = doc.paste();

// Rest of the code...
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
var oldPref = app.preferences.rulerUnits
////////////////////////////////////////////////////////////////////////////////////////////
var oldPref = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;
var doc = activeDocument;
var iLayer = doc.activeLayer;
layerDown ();
var mLayerB = doc.activeLayer.bounds;
doc.activeLayer = iLayer;
var scale = Math.max((mLayerB[2]-mLayerB[0])/(iLayer.bounds[2]-iLayer.bounds[0]),(mLayerB[3]-mLayerB[1])/(iLayer.bounds[3]-iLayer.bounds[1]));
iLayer.resize (scale*100,scale*100);
iLayer.translate((mLayerB[0]+mLayerB[2])/2-(iLayer.bounds[0]+iLayer.bounds[2])/2,(mLayerB[1]+mLayerB[3])/2-(iLayer.bounds[1]+iLayer.bounds[3])/2);
function layerDown(){
var idslct = charIDToTypeID( "slct" );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idBckw = charIDToTypeID( "Bckw" );
ref1.putEnumerated( idLyr, idOrdn, idBckw );
desc2.putReference( idnull, ref1 );
var idMkVs = charIDToTypeID( "MkVs" );
desc2.putBoolean( idMkVs, false );
var idLyrI = charIDToTypeID( "LyrI" );
var list1 = new ActionList();
list1.putInteger( 3 );
desc2.putList( idLyrI, list1 );
executeAction( idslct, desc2, DialogModes.NO );
};


} else {
// Perform actions when the layer does not exist

// Close the JPG document
jpgDoc.close(SaveOptions.DONOTSAVECHANGES);
}
}

// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}

function getFilesInFolder(folderPath, fileExtension) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];

for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && getFileExtension(file.name) === fileExtension) {
filteredFiles.push(file);
}
}

return filteredFiles;
}

function getFileExtension(fileName) {
var parts = fileName.split(".");
if (parts.length > 1) {
return parts[parts.length - 1];
}
return "";
}

function hasLayer(doc, layerName) {
var layers = doc.layers;
for (var i = 0; i < layers.length; i++) {
if (layers[i].name === layerName) {
return true;
}
}
return false;
}

This topic has been closed for replies.
Correct answer Ravi256883170yte
// Input PSD file select folder dialog
var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");
// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");
// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");
// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);
function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getJpgFilesInFolder(inputJPGFolder, "jpg");
jpgFiles.sort(reverseSort);
////// by devguru //////
function reverseSort(a, b) {
if(a > b) return -1;
if(a < b) return 1;
return 0
};
// process the template files;
for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];
// Open the PSD file
var doc = open(psdFile);
var theVerticalFrames = collectLayersByNames(["VFrame"]);
var theHorizontalFrames = collectLayersByNames(["HFrame"]);
// process the jpg files;
for (var j = jpgFiles.length-1; j >= 0; j--) {
var jpgFile = jpgFiles[j];
// place the JPG file;
var newLayer = placeScaleRotateFile (jpgFile, 0, 0, 100, 100, 0);
var newLayerStuff = getLayerIdAndIndexAndBounds();
var isHorizontal = false;
// Determine the layer name based on the orientation of the JPG file
if (newLayerStuff[5] > newLayerStuff[4]) {
isHorizontal = false;
} else {
isHorizontal = true;
};
if (isHorizontal == true) {
var theLayer = theHorizontalFrames[0];
theHorizontalFrames.splice(0,1)
}
else {
var theLayer = theVerticalFrames[0];
theVerticalFrames.splice(0,1)
};
// Check if the PSD document has the target layer;
if (theLayer != undefined) {
jpgFiles.splice(j,1);
// move the smart object;
moveLayerTo(newLayerStuff[1], getLayerIndexFromID(theLayer[1]));
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
////////////////////////////////////////////////////////////////////////////////////////////
var scale = Math.max(theLayer[4]/newLayerStuff[4],theLayer[5]/newLayerStuff[5])*100;
var offsetX = (theLayer[3][0]+theLayer[4]/2) - (newLayerStuff[3][0]+newLayerStuff[4]/2);
var offsetY = (theLayer[3][1]+theLayer[5]/2) - (newLayerStuff[3][1]+newLayerStuff[5]/2);
transformLayer(offsetX, offsetY, scale);
} else {
// Perform actions when the layer does not exist
deleteLayerByID(newLayerStuff[1]);
}
};
// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
//doc.close(SaveOptions.DONOTSAVECHANGES);
};
if (jpgFiles.length > 0) {alert ("the files\n"+jpgFiles.join("\n")+"\nhave not been placed")};
app.preferences.rulerUnits = savedRuler;
app.displayDialogs = startDisplayDialogs;
};
////////////////////////////////////////////////////////////////////////////////////////////
function getFilesInFolder(folderPath, fileExtension) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.\D{3}$/i) == "."+fileExtension) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
var desc5 = new ActionDescriptor();
desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
desc5.putEnumerated( charIDToTypeID( "FTcs" ), idQCSt = charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
desc6.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theYScale );
desc5.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );
desc5.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ),theAngle );
desc5.putBoolean( charIDToTypeID( "Lnkd" ), false );
executeAction( charIDToTypeID( "Plc " ), desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
////// move active layer in front of other layer in layer stack //////
function moveLayerTo (thisLayerId, theIndex) {
selectLayerByID(thisLayerId, false);
var idlayer = stringIDToTypeID( "layer" );
var desc58 = new ActionDescriptor();
var ref19 = new ActionReference();
ref19.putEnumerated( idlayer, stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc58.putReference( stringIDToTypeID( "null" ), ref19 );
var ref20 = new ActionReference();
ref20.putIndex( idlayer, theIndex );
desc58.putReference( stringIDToTypeID( "to" ), ref20 );
desc58.putBoolean( stringIDToTypeID( "adjustment" ), false );
desc58.putInteger( stringIDToTypeID( "version" ), 5 );
var list11 = new ActionList();
list11.putInteger(thisLayerId);
desc58.putList( stringIDToTypeID( "layerID" ), list11 );
executeAction( stringIDToTypeID( "move" ), desc58, DialogModes.NO );
};
////// get active layer’s id //////
function getLayerIdAndIndexAndBounds () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
return [theName, theID, theIndex, theseBounds, theWidth, theHeight]
};
////// get index from id //////
function getLayerIndexFromID (theID) {
try {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("itemIndex"));
ref.putIdentifier( charIDToTypeID("Lyr "), theID );
var layerDesc = executeActionGet(ref);
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
return theIndex
} catch (e) {return undefined}
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// by paul riggott //////
function deleteLayerByID(ID) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
};
////// transform active layer //////
function transformLayer (layerX, layerY, theScale) {
// =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
var desc24 = new ActionDescriptor();
// desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcs0" ) );
desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc25 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc25.putUnitDouble( idHrzn, idPxl, layerX);
var idVrtc = charIDToTypeID( "Vrtc" );
desc25.putUnitDouble( idVrtc, idPxl, layerY);
desc24.putObject( idOfst, idOfst, desc25 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc24.putUnitDouble( idWdth, idPrc, theScale );
var idHght = charIDToTypeID( "Hght" );
desc24.putUnitDouble( idHght, idPrc, theScale );
executeAction( idTrnf, desc24, DialogModes.NO );
};
////// collect layers with certain name //////
function collectLayersByNames (theNames) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theVisibility = layerDesc.getBoolean(stringIDToTypeID('visible'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
// check name;
for (var x = 0; x < theNames.length; x++) {
if (theName.match(new RegExp (theNames[x], "i")) != null) {
theLayers.push([theName, theID, theIndex, theseBounds, theWidth, theHeight])
}
}
};
}
catch (e) {};
};
return theLayers
};

Nice sir now the script works properly. Thank you sir. You are great. You are geniuses. Sir

29 replies

Ravi256883170yteAuthorCorrect answer
Inspiring
July 11, 2023
// Input PSD file select folder dialog
var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");
// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");
// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");
// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);
function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getJpgFilesInFolder(inputJPGFolder, "jpg");
jpgFiles.sort(reverseSort);
////// by devguru //////
function reverseSort(a, b) {
if(a > b) return -1;
if(a < b) return 1;
return 0
};
// process the template files;
for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];
// Open the PSD file
var doc = open(psdFile);
var theVerticalFrames = collectLayersByNames(["VFrame"]);
var theHorizontalFrames = collectLayersByNames(["HFrame"]);
// process the jpg files;
for (var j = jpgFiles.length-1; j >= 0; j--) {
var jpgFile = jpgFiles[j];
// place the JPG file;
var newLayer = placeScaleRotateFile (jpgFile, 0, 0, 100, 100, 0);
var newLayerStuff = getLayerIdAndIndexAndBounds();
var isHorizontal = false;
// Determine the layer name based on the orientation of the JPG file
if (newLayerStuff[5] > newLayerStuff[4]) {
isHorizontal = false;
} else {
isHorizontal = true;
};
if (isHorizontal == true) {
var theLayer = theHorizontalFrames[0];
theHorizontalFrames.splice(0,1)
}
else {
var theLayer = theVerticalFrames[0];
theVerticalFrames.splice(0,1)
};
// Check if the PSD document has the target layer;
if (theLayer != undefined) {
jpgFiles.splice(j,1);
// move the smart object;
moveLayerTo(newLayerStuff[1], getLayerIndexFromID(theLayer[1]));
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
////////////////////////////////////////////////////////////////////////////////////////////
var scale = Math.max(theLayer[4]/newLayerStuff[4],theLayer[5]/newLayerStuff[5])*100;
var offsetX = (theLayer[3][0]+theLayer[4]/2) - (newLayerStuff[3][0]+newLayerStuff[4]/2);
var offsetY = (theLayer[3][1]+theLayer[5]/2) - (newLayerStuff[3][1]+newLayerStuff[5]/2);
transformLayer(offsetX, offsetY, scale);
} else {
// Perform actions when the layer does not exist
deleteLayerByID(newLayerStuff[1]);
}
};
// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
//doc.close(SaveOptions.DONOTSAVECHANGES);
};
if (jpgFiles.length > 0) {alert ("the files\n"+jpgFiles.join("\n")+"\nhave not been placed")};
app.preferences.rulerUnits = savedRuler;
app.displayDialogs = startDisplayDialogs;
};
////////////////////////////////////////////////////////////////////////////////////////////
function getFilesInFolder(folderPath, fileExtension) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.\D{3}$/i) == "."+fileExtension) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
var desc5 = new ActionDescriptor();
desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
desc5.putEnumerated( charIDToTypeID( "FTcs" ), idQCSt = charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
desc6.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theYScale );
desc5.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );
desc5.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ),theAngle );
desc5.putBoolean( charIDToTypeID( "Lnkd" ), false );
executeAction( charIDToTypeID( "Plc " ), desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
////// move active layer in front of other layer in layer stack //////
function moveLayerTo (thisLayerId, theIndex) {
selectLayerByID(thisLayerId, false);
var idlayer = stringIDToTypeID( "layer" );
var desc58 = new ActionDescriptor();
var ref19 = new ActionReference();
ref19.putEnumerated( idlayer, stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc58.putReference( stringIDToTypeID( "null" ), ref19 );
var ref20 = new ActionReference();
ref20.putIndex( idlayer, theIndex );
desc58.putReference( stringIDToTypeID( "to" ), ref20 );
desc58.putBoolean( stringIDToTypeID( "adjustment" ), false );
desc58.putInteger( stringIDToTypeID( "version" ), 5 );
var list11 = new ActionList();
list11.putInteger(thisLayerId);
desc58.putList( stringIDToTypeID( "layerID" ), list11 );
executeAction( stringIDToTypeID( "move" ), desc58, DialogModes.NO );
};
////// get active layer’s id //////
function getLayerIdAndIndexAndBounds () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
return [theName, theID, theIndex, theseBounds, theWidth, theHeight]
};
////// get index from id //////
function getLayerIndexFromID (theID) {
try {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("itemIndex"));
ref.putIdentifier( charIDToTypeID("Lyr "), theID );
var layerDesc = executeActionGet(ref);
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
return theIndex
} catch (e) {return undefined}
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// by paul riggott //////
function deleteLayerByID(ID) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
};
////// transform active layer //////
function transformLayer (layerX, layerY, theScale) {
// =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
var desc24 = new ActionDescriptor();
// desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcs0" ) );
desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc25 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc25.putUnitDouble( idHrzn, idPxl, layerX);
var idVrtc = charIDToTypeID( "Vrtc" );
desc25.putUnitDouble( idVrtc, idPxl, layerY);
desc24.putObject( idOfst, idOfst, desc25 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc24.putUnitDouble( idWdth, idPrc, theScale );
var idHght = charIDToTypeID( "Hght" );
desc24.putUnitDouble( idHght, idPrc, theScale );
executeAction( idTrnf, desc24, DialogModes.NO );
};
////// collect layers with certain name //////
function collectLayersByNames (theNames) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theVisibility = layerDesc.getBoolean(stringIDToTypeID('visible'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
// check name;
for (var x = 0; x < theNames.length; x++) {
if (theName.match(new RegExp (theNames[x], "i")) != null) {
theLayers.push([theName, theID, theIndex, theseBounds, theWidth, theHeight])
}
}
};
}
catch (e) {};
};
return theLayers
};

Nice sir now the script works properly. Thank you sir. You are great. You are geniuses. Sir

c.pfaffenbichler
Community Expert
Community Expert
July 11, 2023

I don't understand what I am doing wrong. Sir I tried hard but I could not understand. Sir now you correct it and give it

 

 

 

// Input PSD file select folder dialog
var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");
// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");
// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");
// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);
function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getFilesInFolder(inputFolder, "jpg");

jpgFiles.sort(reverseSort);
////// by devguru //////
function reverseSort(a, b) {
if(a > b) return -1;
if(a < b) return 1;
return 0
};

// process the template files;
for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];
// Open the PSD file
var doc = open(psdFile);
var theVerticalFrames = collectLayersByNames(["VFrame"]);
var theHorizontalFrames = collectLayersByNames(["HFrame"]);
// process the jpg files;
for (var j = jpgFiles.length-1; j >= 0; j--) {
var jpgFile = jpgFiles[j];
// place the JPG file;
var newLayer = placeScaleRotateFile (jpgFile, 0, 0, 100, 100, 0);
var newLayerStuff = getLayerIdAndIndexAndBounds();
var isHorizontal = false;
// Determine the layer name based on the orientation of the JPG file
if (newLayerStuff[5] > newLayerStuff[4]) {
isHorizontal = false;
} else {
isHorizontal = true;
};
if (isHorizontal == true) {
var theLayer = theHorizontalFrames[0];
theHorizontalFrames.splice(0,1)
}
else {
var theLayer = theVerticalFrames[0];
theVerticalFrames.splice(0,1)
};
// Check if the PSD document has the target layer;
if (theLayer != undefined) {
jpgFiles.splice(j,1);
// move the smart object;
moveLayerTo(newLayerStuff[1], getLayerIndexFromID(theLayer[1]));
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
////////////////////////////////////////////////////////////////////////////////////////////

var scale = Math.max(theLayer[4]/newLayerStuff[4],theLayer[5]/newLayerStuff[5])*100;
var offsetX = (theLayer[3][0]+theLayer[4]/2) - (newLayerStuff[3][0]+newLayerStuff[4]/2);
var offsetY = (theLayer[3][1]+theLayer[5]/2) - (newLayerStuff[3][1]+newLayerStuff[5]/2);
transformLayer(offsetX, offsetY, scale);

} else {
// Perform actions when the layer does not exist
deleteLayerByID(newLayerStuff[1]);
}
};
// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
//doc.close(SaveOptions.DONOTSAVECHANGES);
};
if (jpgFiles.length > 0) {alert ("the files\n"+jpgFiles.join("\n")+"\nhave not been placed")};
app.preferences.rulerUnits = savedRuler;
app.displayDialogs = startDisplayDialogs;
};
////////////////////////////////////////////////////////////////////////////////////////////
function getFilesInFolder(folderPath, fileExtension) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.\D{3}$/i) == "."+fileExtension) {
filteredFiles.push(file);
}
}
return filteredFiles;
};

function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
var desc5 = new ActionDescriptor();
desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
desc5.putEnumerated( charIDToTypeID( "FTcs" ), idQCSt = charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
desc6.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theYScale );
desc5.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );
desc5.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ),theAngle );
desc5.putBoolean( charIDToTypeID( "Lnkd" ), false );
executeAction( charIDToTypeID( "Plc " ), desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
////// move active layer in front of other layer in layer stack //////
function moveLayerTo (thisLayerId, theIndex) {
selectLayerByID(thisLayerId, false);
var idlayer = stringIDToTypeID( "layer" );
var desc58 = new ActionDescriptor();
var ref19 = new ActionReference();
ref19.putEnumerated( idlayer, stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc58.putReference( stringIDToTypeID( "null" ), ref19 );
var ref20 = new ActionReference();
ref20.putIndex( idlayer, theIndex );
desc58.putReference( stringIDToTypeID( "to" ), ref20 );
desc58.putBoolean( stringIDToTypeID( "adjustment" ), false );
desc58.putInteger( stringIDToTypeID( "version" ), 5 );
var list11 = new ActionList();
list11.putInteger(thisLayerId);
desc58.putList( stringIDToTypeID( "layerID" ), list11 );
executeAction( stringIDToTypeID( "move" ), desc58, DialogModes.NO );
};
////// get active layer’s id //////
function getLayerIdAndIndexAndBounds () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
return [theName, theID, theIndex, theseBounds, theWidth, theHeight]
};
////// get index from id //////
function getLayerIndexFromID (theID) {
try {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("itemIndex"));
ref.putIdentifier( charIDToTypeID("Lyr "), theID );
var layerDesc = executeActionGet(ref);
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
return theIndex
} catch (e) {return undefined}
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// by paul riggott //////
function deleteLayerByID(ID) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
};
////// transform active layer //////
function transformLayer (layerX, layerY, theScale) {
// =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
var desc24 = new ActionDescriptor();
// desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcs0" ) );
desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc25 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc25.putUnitDouble( idHrzn, idPxl, layerX);
var idVrtc = charIDToTypeID( "Vrtc" );
desc25.putUnitDouble( idVrtc, idPxl, layerY);
desc24.putObject( idOfst, idOfst, desc25 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc24.putUnitDouble( idWdth, idPrc, theScale );
var idHght = charIDToTypeID( "Hght" );
desc24.putUnitDouble( idHght, idPrc, theScale );
executeAction( idTrnf, desc24, DialogModes.NO );
};
////// collect layers with certain name //////
function collectLayersByNames (theNames) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /&& layerSet != "layerSectionStart"/ && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theVisibility = layerDesc.getBoolean(stringIDToTypeID('visible'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
// check name;
for (var x = 0; x < theNames.length; x++) {
if (theName.match(new RegExp (theNames[x], "i")) != null) {
theLayers.push([theName, theID, theIndex, theseBounds, theWidth, theHeight])
}
}
};
}
catch (e) {};
};
return theLayers
};


// Input PSD file select folder dialog
var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");
// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");
// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");
// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);
function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getJpgFilesInFolder(inputJPGFolder, "jpg");
jpgFiles.sort(reverseSort);
////// by devguru //////
function reverseSort(a, b) {
if(a > b) return -1;
if(a < b) return 1;
return 0
};
// process the template files;
for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];
// Open the PSD file
var doc = open(psdFile);
var theVerticalFrames = collectLayersByNames(["VFrame"]);
var theHorizontalFrames = collectLayersByNames(["HFrame"]);
// process the jpg files;
for (var j = jpgFiles.length-1; j >= 0; j--) {
var jpgFile = jpgFiles[j];
// place the JPG file;
var newLayer = placeScaleRotateFile (jpgFile, 0, 0, 100, 100, 0);
var newLayerStuff = getLayerIdAndIndexAndBounds();
var isHorizontal = false;
// Determine the layer name based on the orientation of the JPG file
if (newLayerStuff[5] > newLayerStuff[4]) {
isHorizontal = false;
} else {
isHorizontal = true;
};
if (isHorizontal == true) {
var theLayer = theHorizontalFrames[0];
theHorizontalFrames.splice(0,1)
}
else {
var theLayer = theVerticalFrames[0];
theVerticalFrames.splice(0,1)
};
// Check if the PSD document has the target layer;
if (theLayer != undefined) {
jpgFiles.splice(j,1);
// move the smart object;
moveLayerTo(newLayerStuff[1], getLayerIndexFromID(theLayer[1]));
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
////////////////////////////////////////////////////////////////////////////////////////////
var scale = Math.max(theLayer[4]/newLayerStuff[4],theLayer[5]/newLayerStuff[5])*100;
var offsetX = (theLayer[3][0]+theLayer[4]/2) - (newLayerStuff[3][0]+newLayerStuff[4]/2);
var offsetY = (theLayer[3][1]+theLayer[5]/2) - (newLayerStuff[3][1]+newLayerStuff[5]/2);
transformLayer(offsetX, offsetY, scale);
} else {
// Perform actions when the layer does not exist
deleteLayerByID(newLayerStuff[1]);
}
};
// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
//doc.close(SaveOptions.DONOTSAVECHANGES);
};
if (jpgFiles.length > 0) {alert ("the files\n"+jpgFiles.join("\n")+"\nhave not been placed")};
app.preferences.rulerUnits = savedRuler;
app.displayDialogs = startDisplayDialogs;
};
////////////////////////////////////////////////////////////////////////////////////////////
function getFilesInFolder(folderPath, fileExtension) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.\D{3}$/i) == "."+fileExtension) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
var desc5 = new ActionDescriptor();
desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
desc5.putEnumerated( charIDToTypeID( "FTcs" ), idQCSt = charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
desc6.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theYScale );
desc5.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );
desc5.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ),theAngle );
desc5.putBoolean( charIDToTypeID( "Lnkd" ), false );
executeAction( charIDToTypeID( "Plc " ), desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
////// move active layer in front of other layer in layer stack //////
function moveLayerTo (thisLayerId, theIndex) {
selectLayerByID(thisLayerId, false);
var idlayer = stringIDToTypeID( "layer" );
var desc58 = new ActionDescriptor();
var ref19 = new ActionReference();
ref19.putEnumerated( idlayer, stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc58.putReference( stringIDToTypeID( "null" ), ref19 );
var ref20 = new ActionReference();
ref20.putIndex( idlayer, theIndex );
desc58.putReference( stringIDToTypeID( "to" ), ref20 );
desc58.putBoolean( stringIDToTypeID( "adjustment" ), false );
desc58.putInteger( stringIDToTypeID( "version" ), 5 );
var list11 = new ActionList();
list11.putInteger(thisLayerId);
desc58.putList( stringIDToTypeID( "layerID" ), list11 );
executeAction( stringIDToTypeID( "move" ), desc58, DialogModes.NO );
};
////// get active layer’s id //////
function getLayerIdAndIndexAndBounds () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
return [theName, theID, theIndex, theseBounds, theWidth, theHeight]
};
////// get index from id //////
function getLayerIndexFromID (theID) {
try {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("itemIndex"));
ref.putIdentifier( charIDToTypeID("Lyr "), theID );
var layerDesc = executeActionGet(ref);
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
return theIndex
} catch (e) {return undefined}
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// by paul riggott //////
function deleteLayerByID(ID) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
};
////// transform active layer //////
function transformLayer (layerX, layerY, theScale) {
// =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
var desc24 = new ActionDescriptor();
// desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcs0" ) );
desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc25 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc25.putUnitDouble( idHrzn, idPxl, layerX);
var idVrtc = charIDToTypeID( "Vrtc" );
desc25.putUnitDouble( idVrtc, idPxl, layerY);
desc24.putObject( idOfst, idOfst, desc25 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc24.putUnitDouble( idWdth, idPrc, theScale );
var idHght = charIDToTypeID( "Hght" );
desc24.putUnitDouble( idHght, idPrc, theScale );
executeAction( idTrnf, desc24, DialogModes.NO );
};
////// collect layers with certain name //////
function collectLayersByNames (theNames) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theVisibility = layerDesc.getBoolean(stringIDToTypeID('visible'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
// check name;
for (var x = 0; x < theNames.length; x++) {
if (theName.match(new RegExp (theNames[x], "i")) != null) {
theLayers.push([theName, theID, theIndex, theseBounds, theWidth, theHeight])
}
}
};
}
catch (e) {};
};
return theLayers
};
Inspiring
July 11, 2023

You seem to have removed the function »getFilesInFolder« – why? 

 

The function »getFilesInFolder« should be a second function for the jpgs, not a replacement! 


I don't understand what I am doing wrong. Sir I tried hard but I could not understand. Sir now you correct it and give it

 

 

 

// Input PSD file select folder dialog
var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");
// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");
// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");
// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);
function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getFilesInFolder(inputFolder, "jpg");

jpgFiles.sort(reverseSort);
////// by devguru //////
function reverseSort(a, b) {
if(a > b) return -1;
if(a < b) return 1;
return 0
};

// process the template files;
for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];
// Open the PSD file
var doc = open(psdFile);
var theVerticalFrames = collectLayersByNames(["VFrame"]);
var theHorizontalFrames = collectLayersByNames(["HFrame"]);
// process the jpg files;
for (var j = jpgFiles.length-1; j >= 0; j--) {
var jpgFile = jpgFiles[j];
// place the JPG file;
var newLayer = placeScaleRotateFile (jpgFile, 0, 0, 100, 100, 0);
var newLayerStuff = getLayerIdAndIndexAndBounds();
var isHorizontal = false;
// Determine the layer name based on the orientation of the JPG file
if (newLayerStuff[5] > newLayerStuff[4]) {
isHorizontal = false;
} else {
isHorizontal = true;
};
if (isHorizontal == true) {
var theLayer = theHorizontalFrames[0];
theHorizontalFrames.splice(0,1)
}
else {
var theLayer = theVerticalFrames[0];
theVerticalFrames.splice(0,1)
};
// Check if the PSD document has the target layer;
if (theLayer != undefined) {
jpgFiles.splice(j,1);
// move the smart object;
moveLayerTo(newLayerStuff[1], getLayerIndexFromID(theLayer[1]));
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
////////////////////////////////////////////////////////////////////////////////////////////

var scale = Math.max(theLayer[4]/newLayerStuff[4],theLayer[5]/newLayerStuff[5])*100;
var offsetX = (theLayer[3][0]+theLayer[4]/2) - (newLayerStuff[3][0]+newLayerStuff[4]/2);
var offsetY = (theLayer[3][1]+theLayer[5]/2) - (newLayerStuff[3][1]+newLayerStuff[5]/2);
transformLayer(offsetX, offsetY, scale);

} else {
// Perform actions when the layer does not exist
deleteLayerByID(newLayerStuff[1]);
}
};
// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
//doc.close(SaveOptions.DONOTSAVECHANGES);
};
if (jpgFiles.length > 0) {alert ("the files\n"+jpgFiles.join("\n")+"\nhave not been placed")};
app.preferences.rulerUnits = savedRuler;
app.displayDialogs = startDisplayDialogs;
};
////////////////////////////////////////////////////////////////////////////////////////////
function getFilesInFolder(folderPath, fileExtension) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.\D{3}$/i) == "."+fileExtension) {
filteredFiles.push(file);
}
}
return filteredFiles;
};

function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
var desc5 = new ActionDescriptor();
desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
desc5.putEnumerated( charIDToTypeID( "FTcs" ), idQCSt = charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idPxl = charIDToTypeID( "#Pxl" );
desc6.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, xOffset );
desc6.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, yOffset );
var idOfst = charIDToTypeID( "Ofst" );
desc5.putObject( idOfst, idOfst, desc6 );
var idPrc = charIDToTypeID( "#Prc" );
desc5.putUnitDouble( charIDToTypeID( "Wdth" ), idPrc, theYScale );
desc5.putUnitDouble( charIDToTypeID( "Hght" ), idPrc, theXScale );
desc5.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ),theAngle );
desc5.putBoolean( charIDToTypeID( "Lnkd" ), false );
executeAction( charIDToTypeID( "Plc " ), desc5, DialogModes.NO );
return app.activeDocument.activeLayer;
};
////// move active layer in front of other layer in layer stack //////
function moveLayerTo (thisLayerId, theIndex) {
selectLayerByID(thisLayerId, false);
var idlayer = stringIDToTypeID( "layer" );
var desc58 = new ActionDescriptor();
var ref19 = new ActionReference();
ref19.putEnumerated( idlayer, stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc58.putReference( stringIDToTypeID( "null" ), ref19 );
var ref20 = new ActionReference();
ref20.putIndex( idlayer, theIndex );
desc58.putReference( stringIDToTypeID( "to" ), ref20 );
desc58.putBoolean( stringIDToTypeID( "adjustment" ), false );
desc58.putInteger( stringIDToTypeID( "version" ), 5 );
var list11 = new ActionList();
list11.putInteger(thisLayerId);
desc58.putList( stringIDToTypeID( "layerID" ), list11 );
executeAction( stringIDToTypeID( "move" ), desc58, DialogModes.NO );
};
////// get active layer’s id //////
function getLayerIdAndIndexAndBounds () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
return [theName, theID, theIndex, theseBounds, theWidth, theHeight]
};
////// get index from id //////
function getLayerIndexFromID (theID) {
try {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("itemIndex"));
ref.putIdentifier( charIDToTypeID("Lyr "), theID );
var layerDesc = executeActionGet(ref);
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
return theIndex
} catch (e) {return undefined}
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// by paul riggott //////
function deleteLayerByID(ID) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
};
////// transform active layer //////
function transformLayer (layerX, layerY, theScale) {
// =======================================================
var idTrnf = charIDToTypeID( "Trnf" );
var desc24 = new ActionDescriptor();
// desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcs0" ) );
desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
var idOfst = charIDToTypeID( "Ofst" );
var desc25 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc25.putUnitDouble( idHrzn, idPxl, layerX);
var idVrtc = charIDToTypeID( "Vrtc" );
desc25.putUnitDouble( idVrtc, idPxl, layerY);
desc24.putObject( idOfst, idOfst, desc25 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc24.putUnitDouble( idWdth, idPrc, theScale );
var idHght = charIDToTypeID( "Hght" );
desc24.putUnitDouble( idHght, idPrc, theScale );
executeAction( idTrnf, desc24, DialogModes.NO );
};
////// collect layers with certain name //////
function collectLayersByNames (theNames) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /&& layerSet != "layerSectionStart"/ && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theVisibility = layerDesc.getBoolean(stringIDToTypeID('visible'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theWidth = theseBounds[2]-theseBounds[0];
var theHeight = theseBounds[3]-theseBounds[1];
// check name;
for (var x = 0; x < theNames.length; x++) {
if (theName.match(new RegExp (theNames[x], "i")) != null) {
theLayers.push([theName, theID, theIndex, theseBounds, theWidth, theHeight])
}
}
};
}
catch (e) {};
};
return theLayers
};

c.pfaffenbichler
Community Expert
Community Expert
July 11, 2023

var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");
// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");
// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");
// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);
function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getFilesInFolder(inputFolder, "jpg");

jpgFiles.sort(reverseSort);
////// by devguru //////
function reverseSort(a, b) {
if(a > b) return -1;
if(a < b) return 1;
return 0
};

// process the template files;
for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];
// Open the PSD file
var doc = open(psdFile);
var theVerticalFrames = collectLayersByNames(["VFrame"]);
var theHorizontalFrames = collectLayersByNames(["HFrame"]);
// process the jpg files;
for (var j = jpgFiles.length-1; j >= 0; j--) {
var jpgFile = jpgFiles[j];
// place the JPG file;
var newLayer = placeScaleRotateFile (jpgFile, 0, 0, 100, 100, 0);
var newLayerStuff = getLayerIdAndIndexAndBounds();
var isHorizontal = false;
// Determine the layer name based on the orientation of the JPG file
if (newLayerStuff[5] > newLayerStuff[4]) {
isHorizontal = false;
} else {
isHorizontal = true;
};
if (isHorizontal == true) {
var theLayer = theHorizontalFrames[0];
theHorizontalFrames.splice(0,1)
}
else {
var theLayer = theVerticalFrames[0];
theVerticalFrames.splice(0,1)
};
// Check if the PSD document has the target layer;
if (theLayer != undefined) {
jpgFiles.splice(j,1);
// move the smart object;
moveLayerTo(newLayerStuff[1], getLayerIndexFromID(theLayer[1]));
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
////////////////////////////////////////////////////////////////////////////////////////////

var scale = Math.max(theLayer[4]/newLayerStuff[4],theLayer[5]/newLayerStuff[5])*100;
var offsetX = (theLayer[3][0]+theLayer[4]/2) - (newLayerStuff[3][0]+newLayerStuff[4]/2);
var offsetY = (theLayer[3][1]+theLayer[5]/2) - (newLayerStuff[3][1]+newLayerStuff[5]/2);
transformLayer(offsetX, offsetY, scale);

} else {
// Perform actions when the layer does not exist
deleteLayerByID(newLayerStuff[1]);
}
};
// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
//doc.close(SaveOptions.DONOTSAVECHANGES);
};
if (jpgFiles.length > 0) {alert ("the files\n"+jpgFiles.join("\n")+"\nhave not been placed")};
app.preferences.rulerUnits = savedRuler;
app.displayDialogs = startDisplayDialogs;
};
////////////////////////////////////////////////////////////////////////////////////////////
function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
var desc5 = new ActionDescriptor();
desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
desc5.putEnumerated( charIDToTypeID( "FT…


You seem to have removed the function »getFilesInFolder« – why? 

 

The function »getFilesInFolder« should be a second function for the jpgs, not a replacement! 

Inspiring
July 11, 2023

Did you add a second function for jpgs specifically? 

Anyway, unless you post the code I cannot check it. 


var inputPSDFolder = Folder.selectDialog("Select the folder containing PSD files");
// Input JPG file select folder dialog
var inputJPGFolder = Folder.selectDialog("Select the folder containing JPG files");
// Output PSD file select folder dialog
var outputPSDFolder = Folder.selectDialog("Select the folder to save the processed PSD files");
// Process PSD and JPG files
processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder);
function processPSDAndJPGFiles(inputPSDFolder, inputJPGFolder, outputPSDFolder) {
var startDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var psdFiles = getFilesInFolder(inputPSDFolder, "psd");
var jpgFiles = getFilesInFolder(inputFolder, "jpg");

jpgFiles.sort(reverseSort);
////// by devguru //////
function reverseSort(a, b) {
if(a > b) return -1;
if(a < b) return 1;
return 0
};

// process the template files;
for (var i = 0; i < psdFiles.length; i++) {
var psdFile = psdFiles[i];
// Open the PSD file
var doc = open(psdFile);
var theVerticalFrames = collectLayersByNames(["VFrame"]);
var theHorizontalFrames = collectLayersByNames(["HFrame"]);
// process the jpg files;
for (var j = jpgFiles.length-1; j >= 0; j--) {
var jpgFile = jpgFiles[j];
// place the JPG file;
var newLayer = placeScaleRotateFile (jpgFile, 0, 0, 100, 100, 0);
var newLayerStuff = getLayerIdAndIndexAndBounds();
var isHorizontal = false;
// Determine the layer name based on the orientation of the JPG file
if (newLayerStuff[5] > newLayerStuff[4]) {
isHorizontal = false;
} else {
isHorizontal = true;
};
if (isHorizontal == true) {
var theLayer = theHorizontalFrames[0];
theHorizontalFrames.splice(0,1)
}
else {
var theLayer = theVerticalFrames[0];
theVerticalFrames.splice(0,1)
};
// Check if the PSD document has the target layer;
if (theLayer != undefined) {
jpgFiles.splice(j,1);
// move the smart object;
moveLayerTo(newLayerStuff[1], getLayerIndexFromID(theLayer[1]));
// Group the new layer
var groupRef = new ActionReference();
groupRef.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var groupDesc = new ActionDescriptor();
groupDesc.putReference(charIDToTypeID("null"), groupRef);
executeAction(charIDToTypeID("GrpL"), groupDesc, DialogModes.NO);
////////////////////////////////////////////////////////////////////////////////////////////

var scale = Math.max(theLayer[4]/newLayerStuff[4],theLayer[5]/newLayerStuff[5])*100;
var offsetX = (theLayer[3][0]+theLayer[4]/2) - (newLayerStuff[3][0]+newLayerStuff[4]/2);
var offsetY = (theLayer[3][1]+theLayer[5]/2) - (newLayerStuff[3][1]+newLayerStuff[5]/2);
transformLayer(offsetX, offsetY, scale);

} else {
// Perform actions when the layer does not exist
deleteLayerByID(newLayerStuff[1]);
}
};
// Save the processed PSD file
doc.saveAs(new File(outputPSDFolder + "/" + psdFile.name));
//doc.close(SaveOptions.DONOTSAVECHANGES);
};
if (jpgFiles.length > 0) {alert ("the files\n"+jpgFiles.join("\n")+"\nhave not been placed")};
app.preferences.rulerUnits = savedRuler;
app.displayDialogs = startDisplayDialogs;
};
////////////////////////////////////////////////////////////////////////////////////////////
function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};
////// place //////
function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {
var desc5 = new ActionDescriptor();
desc5.putPath( charIDToTypeID( "null" ), new File( file ) );
desc5.putEnumerated( charIDToTypeID( "FT…

c.pfaffenbichler
Community Expert
Community Expert
July 11, 2023

Sir, after changing the code, I have checked the script many times but still the error is coming in the same line. Once you check the script it will be good


Did you add a second function for jpgs specifically? 

Anyway, unless you post the code I cannot check it. 

Inspiring
July 11, 2023

I wrote 

»You can try using a function for jpgs specifically

This function has no bearing on psd files. 


Sir, after changing the code, I have checked the script many times but still the error is coming in the same line. Once you check the script it will be good

c.pfaffenbichler
Community Expert
Community Expert
July 11, 2023

when i added the code so there is an error in this line

var psdFiles = getFilesInFolder(inputPSDFolder, "psd");

and psd file is not opening script is stop


I wrote 

»You can try using a function for jpgs specifically

This function has no bearing on psd files. 

Inspiring
July 11, 2023

You can try using a function for jpgs specifically: 

function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};

when i added the code so there is an error in this line

var psdFiles = getFilesInFolder(inputPSDFolder, "psd");

and psd file is not opening script is stop

c.pfaffenbichler
Community Expert
Community Expert
July 11, 2023

Thank you sir .You are great.You are geniuses.Sir you were really right I was wrong .The script works properly.
Sir please give me some correction in this script. jpg files have multiple format extensions. For example "jpg"."JPG"."jpeg", this script only opens jpg file format. Is it possible to open the file in multiple formats as well?
How To Multiple File Extension Format open "Jpg", "Jpeg" ,"png","JPG"
var jpgFiles = getFilesInFolder(inputJPGFolder, "jpg");


You can try using a function for jpgs specifically: 

function getJpgFilesInFolder(folderPath) {
var folder = new Folder(folderPath);
var files = folder.getFiles();
var filteredFiles = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File && file.name.match(/\.jpe{0,1}g$/i) != null) {
filteredFiles.push(file);
}
}
return filteredFiles;
};