SOLVED! Smart Image Replace + Text Layer Amendment

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

MBE4645
Posts: 3
Joined: Sun Aug 07, 2022 10:27 pm

SOLVED! Smart Image Replace + Text Layer Amendment

Post by MBE4645 »

Hi all.

Hoping one of you gurus out there can help me - and please be gentle I am learning....

Challenge: I have an image template that I have been using the variable data sets to amend images and text layers on quite successfully. Now I want to take it to the next level and automate the changing of a smart object (working code below - credit to the late JJMack) and a specific named text layer in the main image. Both the amended text and the smart object image are unique to the resulting output file. Issue is that if I run Export Data Sets as Files I have unique files with amended text but not image. If I run script, I have unique files with correct image but original text.

In an ideal world, I would run a single script and change both the smart object and the text field before creating the output file.

I'd appreciate any guidance you can give.

Thanks

The script I am currently developing is:

// Replace SmartObjects Content and Save as PSD
// 2017, use it at your own risk - CREDIT TO JJMACK
#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;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;

// Check if layer is SmartObject;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else {
// Select Files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)
} else {
//DIALOG var theFiles = File.openDialog("please select files", getFiles, true)
var theFiles = Folder(thePath +"/Discs/").getFiles(/\.(jpg|tif|psd)$/i);
};
};
if (theFiles) {
for (var m = 0; m < theFiles.length; m++) {
// Replace SmartObject
theLayer = replaceContents(theFiles[m], theLayer);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
myDocument.saveAs((new File(thePath + "/_output/" + theName + "." + theNewName + ".psd")), psdOpts, true);
}
}
// }
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
// Replace SmartObject Contents
function replaceContents(newFile, theSO) {
app.activeDocument.activeLayer = theSO;
// =======================================================
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(newFile));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
return app.activeDocument.activeLayer
};
Last edited by MBE4645 on Wed Aug 10, 2022 1:27 am, edited 1 time in total.

User avatar
Kukurykus
Posts: 483
Joined: Mon Jul 25, 2016 12:36 pm

Re: Smart Image Replace + Text Layer Amendment

Post by Kukurykus »

Attach some of your images & your .csv file you use with a script & DataSets, so I'll see what I can do...

MBE4645
Posts: 3
Joined: Sun Aug 07, 2022 10:27 pm

Re: Smart Image Replace + Text Layer Amendment

Post by MBE4645 »

Thanks. Not allowing me to attach either PSD or CSV files. Am mailing to you directly in zip file.

User avatar
Kukurykus
Posts: 483
Joined: Mon Jul 25, 2016 12:36 pm

Re: Smart Image Replace + Text Layer Amendment

Post by Kukurykus »

Loll, you almost answered yourself. That's the trick, to attach the .zip file ;)

Everyting is almost done. You run the script when the smart object is active, and the .csv file is contained in the same folder as open .psd. Beside extension they both use same name. The names in .csv are same as names of .psd files (to be placed in smart object). So the only thing you need to do (beside keeping same named .csv and .psd files ie. 'Breaking Bad (2008).S01') is to replace:

Code: Select all

if (theFiles) {
for (var m = 0; m < theFiles.length; m++) {
// Replace SmartObject
theLayer = replaceContents(theFiles[m], theLayer);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
myDocument.saveAs((new File(thePath + "/_output/" + theName + "." + theNewName + ".psd")), psdOpts, true);
}
}
to:

Code: Select all

if (theFiles) {
	sTT = stringIDToTypeID; for(m = 0; m < theFiles.length; m++) {
		theNewName = decodeURI(theFiles[m].name.match(/(.*)\.[^\.]+$/)[1])
		myDocument.activeLayer = myDocument.artLayers.getByName('episode_title');
		(ref = new ActionReference()).putName(sTT('dataSetClass'), theNewName);
		(dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
		executeAction(sTT('apply'), dsc)

		// Replace SmartObject
		myDocument.activeLayer = theLayer
		theLayer = replaceContents(theFiles[m], theLayer)
		myDocument.saveAs((File(thePath + '/_output/' +
		theName + '.' + theNewName + '.psd')), psdOpts, true)
	}
}

MBE4645
Posts: 3
Joined: Sun Aug 07, 2022 10:27 pm

Re: Smart Image Replace + Text Layer Amendment

Post by MBE4645 »

Many, many thanks. Can confirm this is working exactly as required.