• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Batch replace smart objects

Community Beginner ,
Jan 30, 2014 Jan 30, 2014

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:

  • a mockup file of a book with the cover image set as a transformed smart object
  • a folder of images resized to fit said smart object

This is what I would like a script to do (if possible):

  • for every image in said folder
  • replace smart object with images
  • save each new mockup (one for each new image) as a PSD file.

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?

TOPICS
Actions and scripting

Views

26.3K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 04, 2014 Feb 04, 2014

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;

...

Likes

Translate

Translate
Community Beginner ,
Jan 25, 2017 Jan 25, 2017

Copy link to clipboard

Copied

Hello at all,
I tried the script posted above, Unfortunately the script only replacec one Picture and i do not get it running on a folder!

i got my psd. and in the same directory there is a Subfolder containing about 30000 pictures.
My job is to replace all 30000 pictures to the SO and save it to a new file (Jpg)

So, how can i get following script to run automatic through the subfolder and save a jpg for every image in the subfolder?

I guess i have to change following lines, but i do not know hoe to do it...
Maybe someone can help me out?

// 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) {

#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 = 12;

// 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(/\.(jpg|png|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

};

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2017 Jan 25, 2017

Copy link to clipboard

Copied

You may want to start a new thread.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 11, 2019 Jan 11, 2019

Copy link to clipboard

Copied

LATEST

I WANT TO SAVE THIS AS PNG PLEASE MAKE CHANGES

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2014 Jan 31, 2014

Copy link to clipboard

Copied

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.

There are some options for this, I guess.

Proper 3D would be one – but I’m not sure how well that scripts.

If you have the full dustcover (including back and spine) one could employ two clipped SOs – one for the cover and one for the spine and place the full object in those.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 31, 2014 Jan 31, 2014

Copy link to clipboard

Copied

SamanthaLee706 wrote:

Hello,

This is what I have:

  • a mockup file of a book with the cover image set as a transformed smart object
  • a folder of images resized to fit said smart object

This is what I would like a script to do (if possible):

  • for every image in said folder
  • replace smart object with images
  • save each new mockup (one for each new image) as a PSD file.

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?

You should be able to do what you want using Photoshop Data driven graphics with a PSD template with defined variables and a CSV file.  If you don't want to use that or create CSV files.  A simple template psd file and a script can also do what you want. 

If you look at my Photo Collage Toolkit for Photoshop you will find it contains a some batch scripts. BatchOneImageCollage, BatchMultiImageCollage and BatchPicturePackage to batch populate collage templates.

Photo Collage Toolkit.

Photoshop scripting is powerful and I believe this package demonstrates this.

The package includes four simple rules to follow when making Photo Collage Template PSD files so they will be compatible with my Photoshop scripts.

There are twelve scripts in this package they provide the following functions:

  1. TestCollageTemplate.jsx - Used to test a Photo Collage Template while you are making it with Photoshop.
  2. CollageTemplateBuilder.jsx - Can build Templates compatible with this toolkit's scripts.
  3. LayerToAlphaChan.jsx - Used to convert a Prototype Image Layer stack into a template document.
  4. InteractivePopulateCollage.jsx - Used to interactively populate Any Photo Collage template. Offers most user control inserting pictures and text.
  5. ReplaceCollageImage.jsx - use to replace a populated collage image Smart Object layer with an other image correctly resized and positioned.
  6. ChangeTextSize.jsx - This script can be used to change Image stamps text size when the size used by the populating did not work well.
  7. PopulateCollageTemplate.jsx - Used to Automatically populate a Photo Collage template and leave the populated copy open in Photoshop.
  8. BatchOneImageCollage.jsx - Used to Automatically Batch Populate Collage templates that only have one image inserted. The Collage or Image may be stamped with text.
  9. BatchMultiImageCollage.jsx - Used to Automatically Batch Populate Any Photo Collage template with images in a source image folder. Easier to use than the interactive script. Saved collages can be tweaked.
  10. BatchPicturePackage.jsx - Used to Automatically Batch Populate Any Photo Collage template with an image in a source image folder
  11. PasteImageRoll.jsx - Paste Images into a document to be print on roll paper.
  12. PCTpreferences.jsx - Edit This File to Customize Collage Populating scripts default setting and add your own Layer styles.


Documentation and Examples

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 31, 2014 Jan 31, 2014

Copy link to clipboard

Copied

Tried 'BatchOneImageCollage" before and this is what popped up:

Screen Shot 2014-01-31 at 10.44.35 AM.jpg

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 31, 2014 Jan 31, 2014

Copy link to clipboard

Copied

Try running the script from Photoshop instead of the extendscript toolkit it has some issues or comment out "#target photoshop".  Also note my Scripts place in files from a straight on perspective.  Image areas can have any shape and appear to have a perspective like your covers.  However to achieve the correct perspective the image needs to have it to begin with or the placed smart object layer needs to have their associated transform tweaked to have the corners matched with the area.  If the cover perspective is not extreme the placed images may look good without tweaking the transform.. Any size image will be resized to fit the image location.  The script work best when image and image areas have the same aspect ratios,  However they don't need to be.

Since you have only a single image and all your source images are the same size and resolution.  You can write a Batch script that would replace the smart object layers embedded object in a template with a placed and transformed image the script just needs to know the name of the template's smart object layer.  All source images would need to be exactly the same size and resolution. You would need a template psd file for each image size you want to use.

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

Hello there,

I have a question to this script.

#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

};

I´m not a coder and would like to know if someone know how to replace the following part of the code:

// 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) {

I would like to have the script running without asking to select the files. I would like to define a folder directly within the script. For example, C:/documents/images/...

so that the script immediatly starts without asking for anything.

Can anybody help me out ?

Best wishes

Olli

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

You could try

var theFiles = Folder(/* insert the correct path here */).getFiles(/\.(jpg|tif|psd)$/i);

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

Thank you so much for your reply.

I tried it but it did not work

.......

// select files;

if ($.os.search(/windows/i) != -1) {var theFiles = Folder(/E:\images\).getFiles(/\.(jpg|tif|psd)$/i)}

else {var theFiles = File.openDialog ("please select files", getFiles, true)};

if (theFiles) {

// work through the array;

.......

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

You have not marked the String as a String by putting it in inverted commas.

var theFiles = Folder"(/E:\images\").getFiles(/\.(jpg|tif|psd)$/i)

and as I am no Windows user I am not sure about the slashes you used.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

var theFiles = Folder("/e/images").getFiles(/\.(jpg|tif|psd)$/i);

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

THANK YOU !!! It worked

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

And I even typo-ed the first inverted comma before the bracket …

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

Hello, when i try to apply the script i got error, how to fix this, thanks2016-12-11 23_47_12-Error.jpg

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

The file needs to be saved before running the script.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

Thank You!!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2016 Dec 08, 2016

Copy link to clipboard

Copied

How would I modify this script to replace multiple (and separate) smart objects in one PSD before it saves out each time.  I'm thinking it would ask for the user to define which files will replace each smart object as it cycles through. But, I do not know how to make the code prompt for all smart objects before it begins saving out.  Thanks for any help!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 13, 2017 Jan 13, 2017

Copy link to clipboard

Copied

How Can I Use This Script Just Copy Paste Or Edit Something Can Please Recommend Some Tutorial

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 26, 2017 Aug 26, 2017

Copy link to clipboard

Copied

Hi  all,

I have working script which replaces smart objects with the new image, and it is working as expected.

function someFunction(){

     //some code

     changeSOContent();

}

function changeSOContent(){

     var myDocument = currentDocument;

     // some code

     var imageGroup = myDocument.layerSets.getByName('images-group');

     var imageSmartObj = imageGroup.artLayers.getByName('image-so');

     var imageFiles = Folder(imgDirPath).getFiles(/\.(jpg|psd|png)$/i);

     for (var i = 0; i < imageFiles.length; i++) {

          imageSmartObj = replaceContents(imageFiles, imageSmartObj);

          myDocument.saveAs((new File(destinationFolder + "/" + theName + ".jpg")), jpgopts, true);

     }

}

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;

}

But I want to add progress bar while script is doing the job. So I try like this

function someFunction(){

     app.doForcedProgress("Progress bar start.", "changeSOContent()");

}

function changeSOContent(){

     var myDocument = currentDocument;

     //some code

    

     var imageGroup = myDocument.layerSets.getByName('images-group');

     var imageSmartObj = imageGroup.artLayers.getByName('image-so');

     for (var i = 0; i < imageFiles.length; i++) {

         

          var canContinue = app.doProgressSubTask(i, imageFiles.length, "updateProgress()");

          if (!canContinue) return;

         

          function updateProgress(){

               app.changeProgressText("Current image: " + i + "");

          }

     

          imageSmartObj = replaceContents(imageFiles, imageSmartObj);

          myDocument.saveAs((new File(destinationFolder + "/" + theName + ".jpg")), jpgopts, true);

     }

}

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;

}

But I've got error that said

General Photoshop error Occured. This functionality may not be available in this version of Photoshop.

for the executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);

error.png

This is happening only when I add function through doProgrees();

I've try to figure it out for days, but with no success, because there is no enough search results for doProgress().

Photoshop version is CC 2017

I've also tried solution with WIndow palette with win.update(); but I've got flickering and is not working as expected.

Can anybody help me about this?

Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 27, 2017 Aug 27, 2017

Copy link to clipboard

Copied

svemoguciyou may want to start a new thread discuss the issue.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 27, 2017 Aug 27, 2017

Copy link to clipboard

Copied

Have you perused this thread in particular post 5 by Tom Ruark?

Re: How to close a document if I press Esc key while running a script. Please help me

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 27, 2017 Aug 27, 2017

Copy link to clipboard

Copied

OK, I will start a new thread.

I've seen this thread, and that example is working. Problem occurs only when I have executeAction() in function.

Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

This is brilliant, saving me so much time!

I have a couple of questions.

1. Do you have a donation PayPal account? I would have gladly paid for this!

2. Is there a way of closing the image layers in the smart object once they are saved as jpg?

Its probably a limitation of my system but it gets to 50 images real fast and then really slows up. I'm guessing its all the open layers in the smart object? The images being pasted into the smart object are all around 15mb per file.

Many thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 11, 2018 Mar 11, 2018

Copy link to clipboard

Copied

I think you should make dotation for c.pfaffenbichler who made it works, not to SarahFaithMorris1600 who asked question.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines