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
Hi! Where exactly and how would I place the code to save for a jpeg after the process has been done? I've been using the code you posted here and it saves as a psd. I tried pasting that other jpeg code but couldn't figure out where it goes. So sorry as I'm not familiar with coding.
Copy link to clipboard
Copied
What code are you using and trying to combine exactly?
Copy link to clipboard
Copied
This one below. I copied the select to folder and that worked but the save to jpeg I couldn't figure it out love the script thou!
Only thing is instead of saving for psd I'd like it to save for jpg at "8" quality, same resolution.
// 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;*.jpg", 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 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
};
THANK YOU!!!!
Copy link to clipboard
Copied
Please try
#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;
// jpg options;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 8;
// 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;*.jpg", 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]; //save jpg;
myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpgopts,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
Awesome!
I tried pasting the folder selection for "select files" and it wasn't working. This one also doesn't let me select jpgs for some reason.
What would the code be with folder selection and allowing me to select jogs?
You're amazing! Thank you!
Copy link to clipboard
Copied
Amend the line
if (theFile.name.match(/\.(psd|tif)$/i) != null || theFile.constructor.name == "Folder") {
to
if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
Copy link to clipboard
Copied
Hi, this script is great! Just one question, how would I alter the script so the layer name is consistent across all the batched files? I.E. "background" as opposed to matching the document name.
I tried altering the following line:
var theNewName = theFiles
But I couldn't figure out what the right coding would be.
Thank you and anyone in advance for help on this!
Copy link to clipboard
Copied
What exactly are you trying to achieve (Layer structure and File structure-wise)?
And again: You may want to start a new thread.
Copy link to clipboard
Copied
I need to be able to alter the layer later via batch processing in photoshop to add a stroke around the image. So the batch would be to open the PSD file (created from the above script), select the layer (with consistent naming convention) and then add defined stroke for that particular group of images. Some will be 2 px, some 4, etc.
Copy link to clipboard
Copied
Hi dear, You are very helpful.
I have a quick question. I m using your code. I am using the code from post 14. My question is how to add images on multiply smart object and replace image on 2/3 smart object ? Where I need to change in your code ?
Here's what I need to accomplish.
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.
Plz let me know. I am using the code from post 14
Thanks
Sultan
Copy link to clipboard
Copied
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.
Just to clarify: Do you have four different Smart Objects or four instances of the same Smart Object?
Could you post a screenshot with the Layers Panel visible?
Copy link to clipboard
Copied
Hi,
Thank you for your reply.
Here is the screen shoot of the layer panel. for now I have 2 smart object Marked by Blue color. I want to replace same image on this 2 smart object using your script from post 14. Plz help me.
Thanks
Sultan
Blue mark
Copy link to clipboard
Copied
Well, based on the name it seems to be two instances of the same Smart Object anyway.
When you open one and change something does it update in both instances in saving?
Copy link to clipboard
Copied
Hi,
Thank you for your reply. Here I have included a example psd where I have 2 separate smart object.
Plz check the psd and give me a smart solution.
https://www.dropbox.com/s/cqqoiybgymvpi4m/sample.zip?dl=0
Thanks for your advance.
Sultan
Copy link to clipboard
Copied
Does this work for you?
// replace smart object’s content and save jpgs;
// 2017, 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;
// jpg options;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 9;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
// 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;*.psb;*.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]; //save jpg;
myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpegOptions,true);
}
}
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(psd|tif|psb)$/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
Hi,
Thank you for your reply. The above script does not work for me. Actually I am looking, when I update one smart object image then it should change on both smart object which I have in my layer panel and save jpg or psd. I need to change both smart object at a time.
Thanks
Sultan
Copy link to clipboard
Copied
Can you provide the file for testing?
Why are the two SOs separate and not instances of the same SO?
Copy link to clipboard
Copied
// replace all smart objects’ content and save jpgs;
// 2017, 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;
// jpg options;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 9;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
// check if layer is smart object;
var theSOs = collectSmartObjects2017 ();
if (theSOs.length == 0) {alert ("no smart objects")}
else {
// select files;
if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.psd;*.psb;*.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;
for (var n = 0; n < theSOs.length; n++) {
replaceContentsByIdentifier (theFiles
};
var theNewName = theFiles
//save jpg;
myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpegOptions,true);
}
}
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(psd|tif|psb)$/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
};
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjects2017 () {
// the file;
var myDocument = app.activeDocument;
// 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 not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {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 contents //////
function replaceContentsByIdentifier (newFile, theSO) {
selectLayerByID (theSO[1], false);
//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
Hi,
Thank you very much. I have used your code from post 65 and it is working for me which I am looking. You are really genius. I appreciate your effort.
Thanks
Sultan
Copy link to clipboard
Copied
Please Help Me With Script
This Type Of Result I Want Please If Anyone Have Knowledge Regarding Script Help Me
1.I Have Monk
2.I Change Images in monk Using Smart Object
3.Now I want to Do this Process with multiple Pictures
4.Like I want to choose folder and I want on one click I will convert I pictures into mockups and save as jpg file
This Is My Psd And I want To Do Changes in Smart Object In Bulk Like I Do In This Psd One By One And Save Image
Anyone Who Can Help Me
EDIT [private information removed by moderator]
Copy link to clipboard
Copied
I stuck with file-selection, but feel free to adapt it to folder selection.
// replace smart object’s content and save jpgs;
// 2017, 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;
// jpg options;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 9;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
// 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;*.psb;*.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]; //save jpg;
myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpegOptions,true);
}
}
}
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(psd|tif|psb)$/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 Is Nice Its Working Thankyou So Much
But Now Problem Is Image Not Coming In Perfect Size Its Not Working Perfect Then Can I Edit Image Size
Copy link to clipboard
Copied
This Is The Image Size I want In Smart Object Width 28.80% Hesight 28.47%
Copy link to clipboard
Copied
ok now its working thankyou so much for your help i really appreciate your time
Copy link to clipboard
Copied
If the placed Smart Object and the replacement files vary in size, resolution, aspect ratio you would need to set up some rules how to handle the results.
But good if it works out as it is now.