1 2 Previous Next 74 Replies Latest reply on Mar 11, 2018 5:06 AM by inter247

    Batch replace smart objects

    SarahFaithMorris1600 Level 1

      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?

        • 1. Re: Batch replace smart objects
          c.pfaffenbichler Level 9

          Have you found a Scripting solution yet?

          • 2. Re: Batch replace smart objects
            SarahFaithMorris1600 Level 1

            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.

             

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

            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!

            • 3. Re: Batch replace smart objects
              c.pfaffenbichler Level 9

              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?

              • 4. Re: Batch replace smart objects
                SarahFaithMorris1600 Level 1

                It is always the lowermost layer and always a solid color.

                 

                Here are it's properties:

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

                Thank you for your time :-)

                • 5. Re: Batch replace smart objects
                  c.pfaffenbichler Level 9

                  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.

                  • 6. Re: Batch replace smart objects
                    c.pfaffenbichler Level 9

                    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!

                    • 7. Re: Batch replace smart objects
                      SarahFaithMorris1600 Level 1

                      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[m]);

                                          var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];

                       

                      //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..

                      • 8. Re: Batch replace smart objects
                        JJMack Most Valuable Participant

                        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

                        • 9. Re: Batch replace smart objects
                          SarahFaithMorris1600 Level 1

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

                           

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

                          • 10. Re: Batch replace smart objects
                            JJMack Most Valuable Participant

                            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.

                            • 11. Re: Batch replace smart objects
                              c.pfaffenbichler Level 9

                              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.

                              • 12. Re: Batch replace smart objects
                                SarahFaithMorris1600 Level 1

                                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[m]);

                                                    var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];

                                                   

                                //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!

                                • 13. Re: Batch replace smart objects
                                  SarahFaithMorris1600 Level 1

                                  actually it saved it, but just didn't raise the color picker.

                                  • 14. Re: Batch replace smart objects
                                    c.pfaffenbichler Level 9

                                    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[m], theLayer);

                                                        var theNewName = theFiles[m].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

                                    };

                                    1 person found this helpful
                                    • 15. Re: Batch replace smart objects
                                      SarahFaithMorris1600 Level 1

                                      This worked after I ungrouped the layers.  Thank you so much for all your help, it is really appreciated!

                                      • 16. Re: Batch replace smart objects
                                        www.impactcolorprint.com Level 1

                                        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?

                                        • 17. Re: Batch replace smart objects
                                          c.pfaffenbichler Level 9

                                          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.

                                          • 18. Re: Batch replace smart objects
                                            luis_mendes Level 1

                                            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.

                                            • 19. Re: Batch replace smart objects
                                              c.pfaffenbichler Level 9

                                              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.

                                              • 20. Re: Batch replace smart objects
                                                luis_mendes Level 1

                                                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?

                                                • 21. Re: Batch replace smart objects
                                                  JJMack Most Valuable Participant

                                                  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.

                                                  • 22. Re: Batch replace smart objects
                                                    c.pfaffenbichler Level 9

                                                    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? 

                                                    • 23. Re: Batch replace smart objects
                                                      luis_mendes Level 1

                                                      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.

                                                      depositphotos_10254962-Leaflet-notebook-textbook-white-blank-paper-template.jpg

                                                       

                                                      Now, using a folder of images, I swap out the contents of the green smart object.

                                                       

                                                      american-clothing-logos-74108.jpg

                                                       

                                                      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).

                                                       

                                                       

                                                      • 24. Re: Batch replace smart objects
                                                        c.pfaffenbichler Level 9

                                                        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.

                                                        • 25. Re: Batch replace smart objects
                                                          luis_mendes Level 1

                                                          That was my initial though. But can that be done? How? I havent been able to target the SO after it is replaced.

                                                          • 26. Re: Batch replace smart objects
                                                            c.pfaffenbichler Level 9

                                                            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

                                                            • 27. Re: Batch replace smart objects
                                                              www.impactcolorprint.com Level 1

                                                              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.

                                                              • 28. Re: Batch replace smart objects
                                                                c.pfaffenbichler Level 9

                                                                What code are you using and trying to combine exactly?

                                                                • 29. Re: Batch replace smart objects
                                                                  www.impactcolorprint.com Level 1

                                                                  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[m], theLayer);

                                                                                      var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];

                                                                  //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!!!!

                                                                  • 30. Re: Batch replace smart objects
                                                                    c.pfaffenbichler Level 9

                                                                    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[m], theLayer);

                                                                                        var theNewName = theFiles[m].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

                                                                    };

                                                                    1 person found this helpful
                                                                    • 31. Re: Batch replace smart objects
                                                                      www.impactcolorprint.com Level 1

                                                                      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!

                                                                      • 32. Re: Batch replace smart objects
                                                                        c.pfaffenbichler Level 9

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

                                                                        • 33. Re: Batch replace smart objects
                                                                          www.impactcolorprint.com Level 1

                                                                          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?

                                                                          • 34. Re: Batch replace smart objects
                                                                            c.pfaffenbichler Level 9

                                                                            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.

                                                                            • 35. Re: Batch replace smart objects
                                                                              ollib15506457 Level 1

                                                                              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[m], theLayer);

                                                                                                  var theNewName = theFiles[m].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

                                                                              • 36. Re: Batch replace smart objects
                                                                                c.pfaffenbichler Level 9

                                                                                You could try

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

                                                                                • 37. Re: Batch replace smart objects
                                                                                  ollib15506457 Level 1

                                                                                  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;

                                                                                  .......

                                                                                  • 38. Re: Batch replace smart objects
                                                                                    c.pfaffenbichler Level 9

                                                                                    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.

                                                                                    2 people found this helpful
                                                                                    • 39. Re: Batch replace smart objects
                                                                                      SuperMerlin Level 5

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

                                                                                      2 people found this helpful
                                                                                      1 2 Previous Next