Batch replace smart objects with existing script
Hello hello!
I have a great script that I've been using that replaces smart objects and exports the results as jpegs but would like to be able to use it without the two dialogue boxes to automate the process further. The first box comes up to select the image folder to pull content from and the second is to select where to leave the exports.
Question:
Is there any way to do this so that I insert the location of the folder into the script itself and bypass the folder selection? I've been using other examples in forums to replace parts of the script but nothing is working.
The script also doesn't work if the smart object is inside a folder - is this avoidable, as it would be helpful to use it in a document with artboards?
Thank you for your help!
_____________________________________________________________
var extension = '.jpg'
// Function that saves a JPEG of the current active document with the name '{name}.jpg'
function saveJpeg(path, templateName, fileName){
var file = new File(path + '/' + templateName + '_' + fileName + '.jpg')
var opts = new JPEGSaveOptions();
opts.quality = 10;
templateDocument.saveAs(file, opts, true)
}
var templateDocument = app.activeDocument
var templateName = templateDocument.name.replace('.psd', '')
// From a selected folder, store the filenames of all JPG images in an array so we can loop over them later on
var inFolder = Folder(app.activeDocument.path).selectDlg("Please select folder of images to process");
if (inFolder != null)
{
var fileList = inFolder.getFiles('*' + extension);
}
// Select a folder to store all the output images
var outFolder = Folder(app.activeDocument.path).selectDlg("Please select folder to export processed images");
// Loop over all the images that need to be processed, and store each result in '{outFolder}/{templateName}_{fileList[i]}.jpg'
for(var i = 0; i < fileList.length; i++){
// Inside the template document, look for the smart layer called 'content'. Open the smart layer so it can be edited
var contentLayer = templateDocument.layers.getByName('DataMerge-Content')
app.activeDocument.activeLayer = contentLayer;
executeAction(stringIDToTypeID("placedLayerEditContents"));
// Create a variable to store the environment of the smart layer
var smartObject = app.activeDocument
// Open the file fileList[i] in a new windows, duplicate it to the smart layer
var openedFile = open(fileList[i]);
var imageSource = app.activeDocument
imageSource.artLayers[0].duplicate(smartObject);
// Get the name from the image so we can use it in our export filename, and close fileList[i] again (without saving etc)
var imageSourceName = imageSource.name.replace(extension, '')
imageSource.close();
// Remove the previously applied image at the bottom of the stack of layers inside the smart layer, to prevent it from accumulating more and more images
smartObject.artLayers[1].remove()
// Save and close the smart layer
smartObject.save();
smartObject.close();
// Export a jpg from the original template document with our newly replaced layer
saveJpeg(outFolder, templateName, imageSourceName)
}
