Copy link to clipboard
Copied
Hello,
I have very minimal scripting knowledge, and every script I've found for this workflow is partial or results in "undefined". I am on a mac running Photoshop CC (latest update).
This is what I have:
This is what I would like a script to do (if possible):
We have to create over 40 book, CD, DVD, and Blu-Ray mockups for each flyer we do. Smart Objects help streamline it a little but I'm just going through and using an action right now, which I know is less than ideal. I don't mind if the file names aren't what I need them to be, so incremental file naming would be fine because I could rename them all later.
Any suggestions on how I could streamline this process without having to manually replace and save?
Please try this:
...
// replace smart object’s content and save psd;
// 2011, use it at your own risk;
#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;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is smart object;
Copy link to clipboard
Copied
Have you found a Scripting solution yet?
Copy link to clipboard
Copied
Yes - sort of - thank you! My ultimate goal in all this is automating each type of mockup we need to create (CD, DVD, Blu-Ray, Book, all different facings, etc).
I am using a script (that I believe you wrote) but I can only use it for certain circumstances (and for paperback right facing books only) because sometimes I would need to create the spine of the DVD, book, or side panel of the CD, which would involve more than one smart object and creating those elements would take just as much work on my end creating the folder of files than scripting would help. I replaced what you suggested in the other thread and it works now with selecting items in a folder.
For a hardcover book facing right, There is only ONE more step I would need to add to the script to make this type of mockup fully automated and that is to have the option to change the color of the little bit of back cover that's showing (the highlighted layer in the example below) Which would involve just a dialog to modify that one layer. I would need to be to select the new color (to match the cover, obviously), and then the script can continue on with saving the new PSD as normal.
Any suggestions on how i can add to this script to be able to change that layer? I don't want to get too complicated with all of this, but we do this EVERY MONTH so it would be of great use to us.
Thanks in advance for sharing your knowledge with the Adobe community!
Copy link to clipboard
Copied
What are the unmistakeable characteristics of that Layer?
It seems to share its name with another one, but is it always the lowermost Layer?
Or at least the lowermost Solid Color Layer?
Copy link to clipboard
Copied
It is always the lowermost layer and always a solid color.
Here are it's properties:
Thank you for your time 🙂
Copy link to clipboard
Copied
This is pretty crude but it might work for raising the Sold Color dialog for the lowermost layer.
try {
app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];
//
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
var theRed = theColors.getUnitDoubleValue(theColors.getKey(0));
var theGreen = theColors.getUnitDoubleValue(theColors.getKey(1));
var theBlue = theColors.getUnitDoubleValue(theColors.getKey(2));
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );
desc7.putReference( idnull, ref2 );
var idT = charIDToTypeID( "T " );
var desc8 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc7.putObject( idT, idsolidColorLayer, desc8 );
executeAction( idsetd, desc7, DialogModes.ALL );
} catch (e) {};
Have a good weekend!
Copy link to clipboard
Copied
Where do I paste the above in the existing script? and do I have to change the names of anything to match the name of my layer? Here's what I'm using:
#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;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is smart object;
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", "*.png;*.jpg;*.psd;*.tif", true)}
else {var theFiles = File.openDialog ("please select files", getFiles, true)};
if (theFiles) {
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
theLayer = replaceContents (theFiles
var theNewName = theFiles
//save PSD;
myDocument.saveAs((new File(thePath+"/"+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 contents //////
function replaceContents (newFile) {
// =======================================================
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
};
Sorry for the basic questions..
Copy link to clipboard
Copied
Where do I paste the above in the existing script?
Inserting it before the line
//save PSD;
should raise the Color Picker window before saving the new file/s.
Copy link to clipboard
Copied
I tried pasting it there and it stops after it replaces the smart object and does not even go on to the saving, unfortunately. Here's the script below:
/ replace smart object’s content and save psd;
// 2011, use it at your own risk;
#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;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is smart object;
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", "*.png;*.jpg;*.psd;*.tif", true)}
else {var theFiles = File.openDialog ("please select files", getFiles, true)};
if (theFiles) {
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
theLayer = replaceContents (theFiles
var theNewName = theFiles
//Raise color picker for Back cover
try {
app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];
//
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
var theRed = theColors.getUnitDoubleValue(theColors.getKey(0));
var theGreen = theColors.getUnitDoubleValue(theColors.getKey(1));
var theBlue = theColors.getUnitDoubleValue(theColors.getKey(2));
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );
desc7.putReference( idnull, ref2 );
var idT = charIDToTypeID( "T " );
var desc8 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc7.putObject( idT, idsolidColorLayer, desc8 );
executeAction( idsetd, desc7, DialogModes.ALL );
} catch (e) {};
//save PSD;
myDocument.saveAs((new File(thePath+"/"+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 contents //////
function replaceContents (newFile) {
// =======================================================
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
};
Sorry, I wish I was a scripter so I could troubleshoot myself! I am in the process of teaching myself, however.
Thanks for your time!
Copy link to clipboard
Copied
actually it saved it, but just didn't raise the color picker.
Copy link to clipboard
Copied
Please try this:
// replace smart object’s content and save psd;
// 2011, use it at your own risk;
#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;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is smart object;
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", true)}
else {var theFiles = File.openDialog ("please select files", getFiles, true)};
if (theFiles) {
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
theLayer = replaceContents (theFiles
, theLayer); var theNewName = theFiles
.name.match(/(.*)\.[^\.]+$/)[1]; //Raise color picker for Back cover;
try {
app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );
desc7.putReference( idnull, ref2 );
var idT = charIDToTypeID( "T " );
var desc8 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc7.putObject( idT, idsolidColorLayer, desc8 );
executeAction( idsetd, desc7, DialogModes.ALL );
} catch (e) {};
//save jpg;
myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".psd")),psdOpts,true);
}
}
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(psd|tif)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
////// replace 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
};
Copy link to clipboard
Copied
This worked after I ungrouped the layers. Thank you so much for all your help, it is really appreciated!
Copy link to clipboard
Copied
Hi, thank you for your solution provided here. I tried to use the script and it takes me to the "Choose files" folder and I can't choose anything. This is what I did.
I copied the scirpt into a text doc and named it ending with .js
I go to File > Scripts > Browse and select the script.
It first gave me an error that I wasn't selecting layer with the smart object so I choose the layer with the smart object.
After that it let me get to the "Choose files" but won't let me actually select the folder.
Here's what I need to accompliish.
I have a psd with a smart layer in 4 different layers. I want to replace the image in that smart layer so it will replace the instances in all 4 layers. I have 500 different images I need done so I'd like to be able to batch switch them.
Can you help?
Copy link to clipboard
Copied
Do you work on Mac or Windows?
Did you use the Script from post 14?
After that it let me get to the "Choose files" but won't let me actually select the folder.
It won’t let you select a Folder but Files.
The try-clause after
//Raise color picker for Back cover;
probably makes no sense for you.
Copy link to clipboard
Copied
I have an identical requirement as www.impactcolorprint.com.
Could a folder be selected instead of individual files (selecting several hundred would be difficult)?
If our starting PSD has multiple layers of the same smart object, replacing the contents of that smart object would update them all, correct?
Lastly, I would like to see the updated PSD file saved as jpg with the originating file name from the folder we selected.
template.psd (containing smart object)
Chose...
Images_Folder >
image_1.jpg
image_2.jpg
image_3.jpg
......
template.psd (with updated smart object visisilbe on all layers)
Ouput:
Output_Folder >
image_1.jpg
image_2.jpg
image_3.jpg
....
I still have trouble with some of the javascript syntax so any assistance would be greatly appreciated.
Thanks in advance.
Copy link to clipboard
Copied
When replacing a Smart Object all its instances should update indeed.
Selecting a Folder instead of Files can be done by changing the corresponding lines to
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i)
};
Saving jpgs can be done directly or with a function like this:
function SaveJPG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality; //1-12
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}
but you need to use the correct path as the »saveFile«-argument.
Copy link to clipboard
Copied
Thanks for the great feedback. I have it working beautifully.
One last question...
My images vary in width and height. I would like them to be maintain their aspect ratio, while fitting the shortest side (width or height) of the smart object it is replacing (which is a square).
Ideally I'd like the new images to have white space on top and bottom or the sides but not exceed the bounds of the original smart object in the template.
Is this possible? I'm assuming any manipulation would be done right after the "executeAction(idplacedLayerReplaceContents... " command.
Is there a way to do this?
Copy link to clipboard
Copied
luis_mendes wrote:
My images vary in width and height. I would like them to be maintain their aspect ratio, while fitting the shortest side (width or height) of the smart object it is replacing (which is a square).
I think there is a basic problem here with your understanding of how smart object layers work.
The layer embedded object what ever it may be. Has pixels rendered for it for the smart objects layers content and there is and associated transform for the layer. These pixels are protected from change you can not directly alter them. When you change the layer size you do it by altering the layers transform. You can also attach smart filters to the layers content and mask the layer. You can not use tool on the layers that alter pixels no paint, erase, lines etc. If you use layer duplicate to dupe the layer the dup layer shares the embedded object. You can transform layers that share an embedded object to form a picture package the changing each layers associated transform. To different areas over the documents canvas. When you replace the shared embedded smart object all the layers that share the object change. The associated transforms are not changed. If you do not replace the current embedded object with an object that is the same size the smart object layers associated transforms will not work the way you would expect then to work. For the were set for a different size object.
If you know the output area size and its location you should be able to script what you want. I would think after you replace the embedded content. You could set the layers transform to 100% width and 100% height get the layers bounds and calculate the required transform. for the output area. I do something like that in my collage package. However I resize the image to fill the output area and mask off any excess image so the image get aspect ratio of the output area and looks like a centered crop.
However from the example template in this thread there look to be a perspective transform involved which may have some rotation. I have never tried to program in the ability to handle rotation and prospective placement of images. I just tweak the populated collage manually if an image or two need that type of transform.
Copy link to clipboard
Copied
Could you please post examples of the image with the original SO and examples for portrait and landscape replacement images as you want them to look?
Copy link to clipboard
Copied
I have managed to solve for this by evaluating each smart object after it is replaced and scaling it in the parent PSD. Works perfectly)! Thanks again for the feedback.
What I had hoped for was the ability to manipulate the new smart object contents to fit the starting size of the initial, embedded smart object.
Here's an example PSD file — On it, multiple layers of the same smart object (in green).
The smart object is originally 200 x 200 px.
Now, using a folder of images, I swap out the contents of the green smart object.
The logos vary in width, height, and pixel size - so when the script is run they can sometimes exceed the bounds of the green boxes in the native document.
Again, I have solved for this by evaluating each layer after the SO has been updated with each logo, and back in the parent PSD.
But, what I was most curious about was if it was possible to do this during the manipulation of the smart object and immediately after/during "executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );" Thus placing a logo which is already "fitted" to the green area, without stretching the logo (beyond the transform of the parent PSD).
Copy link to clipboard
Copied
I would recommend a different approach:
Open the SO, place and if necessary scale the replacement files there, hide the background and save/close the SO.
Copy link to clipboard
Copied
That was my initial though. But can that be done? How? I havent been able to target the SO after it is replaced.
Copy link to clipboard
Copied
You’d have to foregoe the replacing and instead open the SO, then place the file.
Something like this – if you have the Preference set to scale placed SOs (»Resize Image During Place«) this might suffice, otherwise you may have to scale the newly placed SO in the SO.
////// open smart object //////
function placeSmartObjectInSmartObject (theLayer, thisFile) {
if (theLayer.kind == "LayerKind.SMARTOBJECT") {
// =======================================================
var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );
var desc2 = new ActionDescriptor();
executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );
// place so;
// =======================================================
var idPlc = charIDToTypeID( "Plc " );
var desc4 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc5 = new ActionDescriptor();
var idfsel = charIDToTypeID( "fsel" );
var idpdfSelection = stringIDToTypeID( "pdfSelection" );
var idpage = stringIDToTypeID( "page" );
desc5.putEnumerated( idfsel, idpdfSelection, idpage );
var idPgNm = charIDToTypeID( "PgNm" );
desc5.putInteger( idPgNm, 1 );
var idCrop = charIDToTypeID( "Crop" );
var idcropTo = stringIDToTypeID( "cropTo" );
var idtrimBox = stringIDToTypeID( "trimBox" );
desc5.putEnumerated( idCrop, idcropTo, idtrimBox );
var idPDFG = charIDToTypeID( "PDFG" );
desc4.putObject( idAs, idPDFG, desc5 );
var idnull = charIDToTypeID( "null" );
desc4.putPath( idnull, new File( thisFile ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc4.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc6 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idRlt = charIDToTypeID( "#Rlt" );
desc6.putUnitDouble( idHrzn, idRlt, 0.000000 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idRlt = charIDToTypeID( "#Rlt" );
desc6.putUnitDouble( idVrtc, idRlt, 0.000000 );
var idOfst = charIDToTypeID( "Ofst" );
desc4.putObject( idOfst, idOfst, desc6 );
var idAntA = charIDToTypeID( "AntA" );
desc4.putBoolean( idAntA, true );
executeAction( idPlc, desc4, DialogModes.NO );
// toggle visibility of others;
// =======================================================
var idShw = charIDToTypeID( "Shw " );
var desc10 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list4 = new ActionList();
var ref7 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref7.putEnumerated( idLyr, idOrdn, idTrgt );
list4.putReference( ref7 );
desc10.putList( idnull, list4 );
var idTglO = charIDToTypeID( "TglO" );
desc10.putBoolean( idTglO, true );
executeAction( idShw, desc10, DialogModes.NO );
// close;
app.activeDocument.close(SaveOptions.SAVECHANGES)
};
};
edited
Copy link to clipboard
Copied
Hi one more question. Currently to import a jpeg the file has to say .jpg. I have a bunch of files that don’t have .jpg at the end just names like name, name1, name2. For the script to work I have to have name1.jpg is there away to get around this?
Copy link to clipboard
Copied
You could remove the the name checks.
But if you save image files without their proper file extensions you may want to rethink your work-flow.