What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Scripting Batch replace smart objects and save the image - mockup


markopolos

New Member
Messages
3
Likes
0
Hi everyone!



I have a frame mockup design in .psd. I want to batch create mockups in the form of .jpegs for all images from a folder (thousands of files). I am trying to get a script to work but continually get errors. I am saving the script as .jsx.



The process should go as follows:

1. My images are located in the folder Images in New folder on my desktop. The mockups should go to folder Images mockup in the New folder on my desktop. I have a mockup design called 0001 mockup glavna slika.psd

2. The images should be placed inside the YOUR DESIGN smart object layer, on top of the white background.

3. The images are saved as .psd or .jpg in the Images mockup folder



I tried to write a script but it isn't working.



#target photoshop

var sourceFolder = new Folder("C:/Users/poste/Desktop/New folder/Images");
var outputFolder = new Folder("C:/Users/poste/Desktop/New folder/Images mockup");
var templatePath = new File("C:/Users/poste/Desktop/New folder/0001 mockup glavna slika.psd");

if (!sourceFolder.exists || !templatePath.exists || !outputFolder.exists) {
alert("One of the folders or the template file does not exist. Please check the paths.");
exit();
}

var files = sourceFolder.getFiles(function(file) {
return (file instanceof File && file.name.match(/\.(jpg|jpeg|png|tif|tiff|psd)$/i));
});

for (var i = 0; i < files.length; i++) {
var currentFile = files;
placeOnTopInTemplate(currentFile, templatePath);
}

function placeOnTopInTemplate(imageFile, template) {
var doc = app.open(template);

var smartObjectLayer = findLayerByName(doc, "YOUR DESIGN");
if (!smartObjectLayer) {
alert("Could not find the layer named 'YOUR DESIGN'.");
doc.close(SaveOptions.DONOTSAVECHANGES);
return;
}

var tempPSB = new File(Folder.temp + '/temp.psb');
smartObjectLayer.smartObject.exportContents(tempPSB);
var tempDoc = app.open(tempPSB);

var imageDoc = app.open(imageFile);
imageDoc.activeLayer.copy();
imageDoc.close(SaveOptions.DONOTSAVECHANGES);
tempDoc.paste();

tempDoc.save();
tempDoc.close(SaveOptions.SAVECHANGES);

smartObjectLayer.smartObject.replaceContents(tempPSB);
tempPSB.remove();

var saveName = outputFolder + "/mockup_" + imageFile.name;
var saveFile = new File(saveName);
var saveOptions = new PhotoshopSaveOptions();
doc.saveAs(saveFile, saveOptions, true);

doc.close(SaveOptions.DONOTSAVECHANGES);
}

function findLayerByName(doc, name) {
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers;
if (layer.name === name) {
return layer;
}
if (layer.typename === "LayerSet") {
var foundLayer = findLayerByName(layer, name); // Recursive search in groups
if (foundLayer) return foundLayer;
}
}
return null;
}



I would be really grateful for help. Thank you so much.
 
There are many reasons why your code cannot not work:
- unuseful recursion;
- non-existent functions in Javascript for Photoshop;
- structural defects due to the lack of indentation in your code;
- and so on.

If you have to answer, give us the structure of your PSD template (the layers).
This will be my last post if you are not interested.
 

Back
Top