Copy link to clipboard
Copied
I'm aware that it's possible to use scripts to batch replace smart objects on a template.
I'm also aware there is a way to batch replace text and images(but not smart objects) using data variables.
I'm after a way to do both in the same action/script. Is this possible?
Thanks
edited
// select folder, replace smart object content and use part of their name to create new files;
// 2024, use it at your own risk;
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|psb)$/i);
if (theFiles.length > 0) {
var myDocument = app.activeDocument;
var docName = myDocument.name;
try {
var basename = docName.match(/(.*)\.[^\.]+$/)[1];
var origDocPath = myDocument.fullName;
var docPath = myDocument.path;
}
...
Copy link to clipboard
Copied
Yes.
Copy link to clipboard
Copied
Great to know thanks.
Next question, how is this possible?
Copy link to clipboard
Copied
You omitted to provide a meaningful description of the actual process (ideally with sample images, at the very least with screenshots illustrating the file and layer structures).
Copy link to clipboard
Copied
Good idea
I'm trying to produce an art effect on various pet images (see screenshots) whereby the pet photo is copied onto the smart object layer "YOUR IMAGE' and the pets name is copied into the text layer "NAME HERE".
The image and pet name could be on a csv file or, if it were easier, the pet name could be the 'name' of the image being copied onto the smart object (assuming a script could copy the file name into a text layer).
Cheers
Thanks
Copy link to clipboard
Copied
Do all the replacement images have the same size and resolution as the original SO?
How do you want to trigger the processing – selecting the files, selecting a Folder, always just using the Files in a specific Folder, …?
What is the endproduct supposed to be – jpgs, layered psds, …?
It would naturally be easiest if the filename would be (or contain) the name that is supposed to go into the Type Layer.
Please provide sample files (the layered image and a few replacement images).
Copy link to clipboard
Copied
You can also use the script DIY Metadata by Paul Riggot to bulk load the names into an unused metadata field and read that in the script. I use that method when processing product photos.
Copy link to clipboard
Copied
The files are too big to upload, I will try to re-size them.
Copy link to clipboard
Copied
Hi, I've made the files lo-res. It's ruined the effect but you'll get the idea.
Replacement images should be same size/resolution.
Ideally I'd like to choose a folder of images whereby the last word of the file name is the dog name for the text layer.
I'd need to make the end images psd's just in case I need to adjust them (appreciate they will probably be BIG files).
Cheers for your help on this.
Copy link to clipboard
Copied
Edited
As you have refused to provide the requested sample replacement images images I don’t know what the names are like; are the words in the files’ names separated by spaces, underscores, …?
I do apologize; it should have been: »As the requested sample replacement images images didn’t upload …«
Copy link to clipboard
Copied
Sorry, did the replacement images not upload? I'll try them again.
Yes the names are preceded by underscores. 🙂
Copy link to clipboard
Copied
Looks like it's not uploaded them again. I've done them as psb's as they have a transparent background but it doesn't seem to like them. Is there another file type you'd recommend that supports transparent backgrounds?
Copy link to clipboard
Copied
I updated the Script in the previous post to fix an issue with the Type Layer-content.
Copy link to clipboard
Copied
edited
// select folder, replace smart object content and use part of their name to create new files;
// 2024, use it at your own risk;
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|psb)$/i);
if (theFiles.length > 0) {
var myDocument = app.activeDocument;
var docName = myDocument.name;
try {
var basename = docName.match(/(.*)\.[^\.]+$/)[1];
var origDocPath = myDocument.fullName;
var docPath = myDocument.path;
}
catch (e) {
var basename = myDocument.name;
var docPath = "~/Desktop"
};
// get layers;
var theSO = collectLayersByName (/YOUR IMAGE/i);
var theTypeLayer = collectLayersByName (/NAME HERE/i);
// process files;
for (var m = 0; m < theFiles.length; m++) {
var theName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
theName = theName.split("_");
selectLayerByID(theSO[0][1], false);
replaceContents (theFiles[m]);
selectLayerByID(theTypeLayer[0][1], false);
myDocument.activeLayer.textItem.contents = theName[theName.length-1];
savePsd (myDocument, docPath, basename+"_"+theName[theName.length-1], true, true, false)
}
}
};
////////////////////////////////////
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(jpg|tif|psd|psb|pdf)$/i)) {
return true
};
};
////// collect layers //////
function collectLayersByName (theReg) {
// get number of layers;
var ref = new ActionReference();
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'));
if (theName.match (theReg) != null) {theLayers.push([theName, theID,])};
};
}
catch (e) {};
};
return theLayers
};
// 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);
}
};
////// replace SmartObject contents //////
function replaceContents (newFile) {
var desc3 = new ActionDescriptor();
desc3.putPath(charIDToTypeID("null"), new File(newFile));
desc3.putInteger(charIDToTypeID("PgNm"), 1);
executeAction(stringIDToTypeID("placedLayerReplaceContents"), desc3, DialogModes.NO);
};
////// save psd //////
function savePsd (theCopy, docPath, basename, layers, alpha, overwrite) {
/* check for existing file */
if (overwrite == false && File(docPath+'/'+basename+'.psd').exists == true) {
var theConfirm = confirm("overwrite existing file");
if (theConfirm == false) {return false}
};
/* make copy and save */
if (app.documents.length > 0) {
try {
/* psd options */
saveOpts = new PhotoshopSaveOptions();
saveOpts.embedColorProfile = true;
saveOpts.alphaChannels = false;
saveOpts.layers = true;
saveOpts.spotColors = true;
/* duplicate */
//var theCopy = thedoc.duplicate("thecopy", true);
if (theCopy.bitsPerChannel != BitsPerChannelType.ONE) {theCopy.bitsPerChannel = BitsPerChannelType.EIGHT};
/* remove paths */
for (var x = theCopy.pathItems.length - 1; x >= 0; x--) {
var thePath = theCopy.pathItems[x];
if (thePath.kind != PathKind.CLIPPINGPATH) {thePath.remove()}
};
/* save */
theCopy.saveAs((new File(docPath+'/'+basename+'.psd')),saveOpts,false);
//theCopy.close(SaveOptions.DONOTSAVECHANGES)
}
catch (e) {docName+" failed"}
}
};
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The function getFiles should also accept psb … not sure what the issue is.
Copy link to clipboard
Copied
Please change the line
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
to
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|psb)$/i);
If that solves the issue please marke the thread as answered.
Copy link to clipboard
Copied
That now works with the psb's! 🙂
Thanks for all your help!
Copy link to clipboard
Copied
You’re welcome!