1 2 Previous Next 54 Replies Latest reply on Feb 23, 2018 8:32 AM by JJMack

    Batch replace smart objects in mockup file

    kittyk93933155 Level 1

      Hi there,

       

      I want to know if it's possible to import x-number of png images into a mockup template with a smart object and then all saved in jpgs? The following script is not working for me but it's probably close to what I need:

       

      Batch replace smart objects

       

      The script must do the following:

       

      Search in folder for PNG files

      Place the PNG image into the Smart Object (top center)

      Save file as JPG into the source folder with the same name as the source file

       

      For example:

       

      Before the script

       

      \Images\Image 1\PNG\Design1.png

      \Images\Image 2\PNG\Design2.png

      \Images\Image 3\PNG\Design3.png

      \Images\Image 4\PNG\Design4.png

       

      After the script

       

      \Images\Image 1\PNG\Design1.png

      \Images\Image 1\PNG\Mockup.jpg

       

      \Images\Image 2\PNG\Design2.png

      \Images\Image 2\PNG\Mockup.jpg

       

      \Images\Image 3\PNG\Design3.png

      \Images\Image 3\PNG\Mockup.jpg

       

      \Images\Image 4\PNG\Design4.png

      \Images\Image 4\PNG\Mockup.jpg

       

      I hope this makes sense. I will appreciate if someone can give me an advise what is the best way to do that.

       

      Kind regards,

       

      Katt

       

       

       

       

        • 1. Re: Batch replace smart objects in mockup file
          JJMack Most Valuable Participant

          My script would save Populated template as PSD you can adjust populated images and save out a PNG.  You need to take care when you want to place is png image the transparent borders around an item.   Place and Paste will most likely trim transparent borders to the subject item bounds.  To prevent that when I create the PNG file a put a 1% opaque pixel in the top left and bottom right corners of the documents canvas.  You will never see these pixels amt the will be not transparent borders the Photoshop will trim. If you do not have me scripts search for Photo Collage toolkit https://forums.adobe.com/search.jspa?q=Photo+Collage+toolkit

          My scripts will not filter for PNG they will place in any type of image file from a source image folder.  It would be easy to change the extension list to be png only.

           

          To place in images in a particular groups in  order requires you to rename the files with a sort order prefix  which will not be included if you use file nams stamping on placed images.

           

          My Batch Scripts will save PSD files and optionally also save a additional JPG version.  The one image batch script will name save  files with a name that reflects the template and image name.   The Batch multi image script  save files with the a name that reflect the template name a suffix number.  The number is update 1 after a populated template is saved its a count of populated templates.    Many of my scripts do not save a file instead the populated template copy will remain open in Photoshop layered to allow you the tweak the populated template and save out the files you want with the names you want.

           

          Capture.jpg

          • 2. Re: Batch replace smart objects in mockup file
            c.pfaffenbichler Level 9

            switchOutSmartObjectsSaveJpg2018Scr.jpg

            // place png files in selected smart object and save jpg;

            // 2018, use it at your own risk;

            #target photoshop

            if (app.documents.length > 0) {

            var myDocument = app.activeDocument;

            var basename = 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 = 10;

            // check if layer is smart object;

            if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")}

            else {

            // select files;

            var theFolder = Folder.selectDialog ("select folder");

            var theFiles = retrievePngFiles (theFolder, []);

            if (theFiles) {

            // work through the array;

            for (var m = 0; m < theFiles.length; m++) {

            var thePath = File(theFiles[m]).path;

            // open smart object;

            var smartObject = openSmartObject (theLayer);

            var theLayer2 = smartObject.activeLayer;

            // place file and fit to convas;

            var theNewOne = placeScaleRotateFile (theFiles[m], 0, 0, 100, 100, 0);

            scaleLayerToFitCanvas ();

            hideOthers ();

            // close;

            smartObject.close(SaveOptions.SAVECHANGES);

            //save jpg;

            myDocument.saveAs((new File(thePath+"/"+theFiles[m].name.match(/(.*)\.[^\.]+$/)[1]+"_"+basename+".jpg")),jpgopts ,true);

            // undo;

            myDocument.activeHistoryState = myDocument.historyStates[myDocument.historyStates.length - 1];

            }

            }

            }

            };

            ////// get ong from subfolders //////

            function retrievePngFiles (theFolder, theFiles) {

              if (!theFiles) {var theFiles = []};

              var theContent = theFolder.getFiles();

              for (var n = 0; n < theContent.length; n++) {

              var theObject = theContent[n];

              if (theObject.constructor.name == "Folder") {

              theFiles = retrievePngFiles(theObject, theFiles)

              };

              if (theObject.name.slice(-4).match(/\.(png)$/i) ) {

              theFiles.push(theObject)

              }

              };

              return theFiles

              };

            ////// place //////

            function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {

            // =======================================================

            var idPlc = charIDToTypeID( "Plc " );

                var desc5 = new ActionDescriptor();

                var idnull = charIDToTypeID( "null" );

                desc5.putPath( idnull, new File( file ) );

                var idFTcs = charIDToTypeID( "FTcs" );

                var idQCSt = charIDToTypeID( "QCSt" );

                var idQcsa = charIDToTypeID( "Qcsa" );

                desc5.putEnumerated( idFTcs, idQCSt, idQcsa );

                var idOfst = charIDToTypeID( "Ofst" );

                    var desc6 = new ActionDescriptor();

                    var idHrzn = charIDToTypeID( "Hrzn" );

                    var idPxl = charIDToTypeID( "#Pxl" );

                    desc6.putUnitDouble( idHrzn, idPxl, xOffset );

                    var idVrtc = charIDToTypeID( "Vrtc" );

                    var idPxl = charIDToTypeID( "#Pxl" );

                    desc6.putUnitDouble( idVrtc, idPxl, yOffset );

                var idOfst = charIDToTypeID( "Ofst" );

                desc5.putObject( idOfst, idOfst, desc6 );

                var idWdth = charIDToTypeID( "Wdth" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc5.putUnitDouble( idWdth, idPrc, theYScale );

                var idHght = charIDToTypeID( "Hght" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc5.putUnitDouble( idHght, idPrc, theXScale );

                var idAngl = charIDToTypeID( "Angl" );

                var idAng = charIDToTypeID( "#Ang" );

                desc5.putUnitDouble( idAngl, idAng,theAngle );

                var idLnkd = charIDToTypeID( "Lnkd" );

                desc5.putBoolean( idLnkd, true );

            executeAction( idPlc, desc5, DialogModes.NO );

            return app.activeDocument.activeLayer;

            };

            ////// scale layer to fit canvas //////

            function scaleLayerToFitCanvas () {

            var ref = new ActionReference();

            ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("bounds"));

            ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

            var layerDesc = executeActionGet(ref);

            var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));

            var layerX = theBounds.getUnitDoubleValue(stringIDToTypeID("left"));

            var layerY = theBounds.getUnitDoubleValue(stringIDToTypeID("top"));

            var layerWidth = theBounds.getUnitDoubleValue(stringIDToTypeID("right"))-layerX;

            var layerHeight = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))-layerY;

            var ref1 = new ActionReference();

            ref1.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

            var docDesc = executeActionGet(ref1);

            var docWidth = docDesc.getUnitDoubleValue(stringIDToTypeID("width"));

            var docHeight = docDesc.getUnitDoubleValue(stringIDToTypeID("height"));

            var docRes = docDesc.getInteger(stringIDToTypeID("resolution"));

            var scaleX = docWidth/layerWidth*docRes/72*100;

            var scaleY = docHeight/layerHeight*docRes/72*100;

            var theScale = Math.min(scaleX, scaleY);

            layerX = ((docWidth*docRes/144) - (layerX+layerWidth/2));

            layerY = ((docHeight*docRes/144) - (layerY+layerHeight/2));

            // =======================================================

            var idTrnf = charIDToTypeID( "Trnf" );

                var desc24 = new ActionDescriptor();

            //    desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcs0" ) );

                desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );

                var idOfst = charIDToTypeID( "Ofst" );

                    var desc25 = new ActionDescriptor();

                    var idHrzn = charIDToTypeID( "Hrzn" );

                    var idPxl = charIDToTypeID( "#Pxl" );

                    desc25.putUnitDouble( idHrzn, idPxl, layerX);

                    var idVrtc = charIDToTypeID( "Vrtc" );

                    desc25.putUnitDouble( idVrtc, idPxl, layerY);

                desc24.putObject( idOfst, idOfst, desc25 );

                var idWdth = charIDToTypeID( "Wdth" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc24.putUnitDouble( idWdth, idPrc, theScale );

                var idHght = charIDToTypeID( "Hght" );

                desc24.putUnitDouble( idHght, idPrc, theScale );

            executeAction( idTrnf, desc24, DialogModes.NO );

            };

            ////// open smart object //////

            function openSmartObject (theLayer) {

            if (theLayer.kind == "LayerKind.SMARTOBJECT") {

            // =======================================================

            var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );

                var desc2 = new ActionDescriptor();

            executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );

            };

            return app.activeDocument

            };

            ////// hide others //////

            function hideOthers () {

            // hide others;

            // =======================================================

            var idShw = charIDToTypeID( "Shw " );

                var desc2 = new ActionDescriptor();

                var idnull = charIDToTypeID( "null" );

                    var list1 = new ActionList();

                        var ref1 = new ActionReference();

                        var idLyr = charIDToTypeID( "Lyr " );

                        var idOrdn = charIDToTypeID( "Ordn" );

                        var idTrgt = charIDToTypeID( "Trgt" );

                        ref1.putEnumerated( idLyr, idOrdn, idTrgt );

                    list1.putReference( ref1 );

                desc2.putList( idnull, list1 );

                var idTglO = charIDToTypeID( "TglO" );

                desc2.putBoolean( idTglO, true );

            executeAction( idShw, desc2, DialogModes.NO );

            };

            • 3. Re: Batch replace smart objects in mockup file
              JJMack Most Valuable Participant

              Question I see that your script does not replace the current Smart object layer content instead it update the layers  object  opens and edits the object.  It places in the png  and scaled it to fit the object canvas and hides the other content in the object. 

               

              // open smart object;

              var smartObject = openSmartObject (theLayer);

              var theLayer2 = smartObject.activeLayer;

              // place file and fit to convas;

              var theNewOne = placeScaleRotateFile (theFiles[m], 0, 0, 100, 100, 0);

              scaleLayerToFitCanvas ();

              hideOthers ();

              // close;

              smartObject.close(SaveOptions.SAVECHANGES);

               

              I think there may be some problems there.  I have never tried to update a smart object layer because of what I though might happen.  If the png image has a transparent borders I think place may trim these.  Additionally the png's file aspect ratio may be different than the work document canvas's aspect ratio so when the place layer is fitted for the canvas size there may be a border because of the aspect ratio mismatch.  Then there is this if the  template's current smart object was a Placed file it may be a raw file or ai file the object may open in ACR or Illustrator not Photoshop.  Are my concerns ill founded?

              • 4. Re: Batch replace smart objects in mockup file
                c.pfaffenbichler Level 9
                Additionally the png's file aspect ratio may be different than the work document canvas's aspect ratio so when the place layer is fitted for the canvas size there may be a border because of the aspect ratio mismatch.

                The current SO is square, so Replace Contents to some file with another aspect Ratio might cause more issues with the Warp for example.

                The Script places the png in the SO and scales to fit according to its longest side.

                This seems to make the most sense to me considering that I don’t know if all designs will have the same aspect ratio.

                • 5. Re: Batch replace smart objects in mockup file
                  kittyk93933155 Level 1

                  The script works fine for me but I have one issue here - I need the png placed in the top center of the Smart Object, so if the image is shorter it will be still on top.Untitled-8.jpg

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

                    You can either include an additional Alignment step in the Script or try to change the line

                    layerY = ((docHeight*docRes/144) - (layerY+layerHeight/2));

                    to provide the result you want to achieve.

                    • 7. Re: Batch replace smart objects in mockup file
                      kittyk93933155 Level 1

                      To be honest I have no idea how to do it. I don't know anything about scripting

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

                        Do your PNG files have transparent border?

                        Are all your PNG files the same size and resolution as the object in your mockup?

                         

                        If your PNG has transparent borders these may be trimmed during place the smart object layer object size may not be the size of the PNG file canvas.

                         

                        If your png files do not have the same aspect ratio as your mockup object when they are resize to fit the mockup object canvas size a border or two most like will be introduced.  The resize to fit must be a constrained resize otherwise the image will distort.

                         

                        If the png contains text if the png files is not the same size as the mockup object the resize text may not be acceptable.

                         

                         

                        It there is no transparent borders in your png files and all the png are the same size as the mockup object and the object type is associated with Photoshop I think the script will work for you,

                        • 9. Re: Batch replace smart objects in mockup file
                          c.pfaffenbichler Level 9

                          To be honest I have no idea how to do it. I don't know anything about scripting

                          If you want to use Scripts you should familiarise yourself with Scripting basics.

                          You can insert this line but you should figure out where yourself:

                          activeDocument.activeLayer.translate(0, activeDocument.activeLayer.bounds[1] * (-1))

                          • 10. Re: Batch replace smart objects in mockup file
                            kittyk93933155 Level 1

                            Thank you for your help regarding this script. I placed the lane and it works fine but a got an error on one of the mockups. I guess it is the problem with the file. I will do more tests with different mockups. Thanks again

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

                              If you need help trouble-shooting that issue please state the exact error message and provide the file.

                              • 13. Re: Batch replace smart objects in mockup file
                                c.pfaffenbichler Level 9

                                Is a Smart Object selected when you run the Script?

                                • 15. Re: Batch replace smart objects in mockup file
                                  kittyk93933155 Level 1

                                  Yes, it is. This error appears after the first image is placed onto the mockup.

                                  • 16. Re: Batch replace smart objects in mockup file
                                    c.pfaffenbichler Level 9

                                    The message state that »Edit Contents« cannot be run, most likely because the selected Layer is not a Smart Object.

                                    Please post a screenshot at error-time including the Layers Panel.

                                    • 17. Re: Batch replace smart objects in mockup file
                                      kittyk93933155 Level 1

                                      error1.jpg

                                       

                                      You are right! For some reason it moves the selection to the layer under the SO. When I run the script if it's not selected the SO the script replies that the selected layer is not SO, therefore I can't run the script if it's on any other layer. Weird!

                                      • 18. Re: Batch replace smart objects in mockup file
                                        c.pfaffenbichler Level 9

                                        That does not seem to make sense as the Script includes no step to change the selected Layer in the containing document.

                                        You could reselect the originally selected Layer with

                                        myDocument.activeLayer = theLayer;

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

                                          Please provide the image and the code you use at current.

                                          • 20. Re: Batch replace smart objects in mockup file
                                            c.pfaffenbichler Level 9

                                            I think I figured the issue out – setting back the HistoryState can result in changing the Layer selection.

                                            Inserting

                                            myDocument.activeLayer = theLayer;

                                            after the HistoryState-line should address that.

                                            • 21. Re: Batch replace smart objects in mockup file
                                              kittyk93933155 Level 1

                                              I found that I must save the psd with the SO selected, otherwise the script resets the file and then the selected layer may be different, that's why I got the error. Tried several times with different mockups and it works great. Thanks again for your cooperation regarding this script

                                              • 22. Re: Batch replace smart objects in mockup file
                                                c.pfaffenbichler Level 9
                                                I found that I must save the psd with the SO selected

                                                If that is workable for you so much the better.

                                                Good to read it worked out for you.

                                                • 23. Re: Batch replace smart objects in mockup file
                                                  kittyk93933155 Level 1

                                                  The only "problem" I've got now is with the saved name because it contains both the PNG and the Mockup names. I tried to modify the line //save jpg; few times but I've got different results and not the one I want. The jpg file must take the mockup name only. Would you be able to help with this as I've got everything else but not this? Thanks

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

                                                    Without testing I guess you would have to change

                                                    myDocument.saveAs((new File(thePath+"/"+theFiles[m].name.match(/(.*)\.[^\.]+$/)[1]+"_"+basename+".jpg")),jpgopts ,true);

                                                    to

                                                    myDocument.saveAs((new File(thePath+"/"+theFiles[m].name.match(/(.*)\.[^\.]+$/)[1]+".jpg")),jpgopts ,true);

                                                    • 25. Re: Batch replace smart objects in mockup file
                                                      kittyk93933155 Level 1

                                                      I already tried the same line but the JPG is saved with the PNG name, not the Mockup name.

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

                                                        Sorry I had misunderstood, try

                                                        myDocument.saveAs((new File(thePath+"/"+basename+".jpg")),jpgopts ,true);

                                                        • 27. Re: Batch replace smart objects in mockup file
                                                          kittyk93933155 Level 1

                                                          That's the line! Thanks again

                                                          • 28. Re: Batch replace smart objects in mockup file
                                                            kittyk93933155 Level 1

                                                            Hi again I thought "This is it!" but I was wrong. Now some of my designs are too short, as a result they are placed too high and they look ridiculous. Apparently I have to place them manually in the correct place, so I removed the line which saves them to jpgs. After I finish with all the designs I need to export them to PNGs with size of the SO (12.5x16) keeping the transparent area. That's how I can run the script and it will place those designs correctly onto the mockups. Now do I have to open another thread for the new script which exports the PNGs from the SO? Thanks

                                                            • 29. Re: Batch replace smart objects in mockup file
                                                              c.pfaffenbichler Level 9
                                                              That's how I can run the script and it will place those designs correctly onto the mockups.

                                                              What exactly is »correct«? What are the rules determining the position?

                                                               

                                                              Apparently I have to place them manually in the correct place, so I removed the line which saves them to jpgs. After I finish with all the designs I need to export them to PNGs with size of the SO (12.5x16) keeping the transparent area.

                                                              What exactly are you describing here?

                                                              • 30. Re: Batch replace smart objects in mockup file
                                                                kittyk93933155 Level 1

                                                                designtest.jpg

                                                                Sorry that I wasn't clear with the previous post. This is one example with a design which is not 12.5" x 16" (full design) in size and it's scaled to 12'5" width. With the last script the design is too high (top) on the garment and with the previous script (centered) alignment it's too low, so I need something in between. The position depends from the design due to the fact that some of these designs are completely different in length. Most of my designs are 12.5" x 16" so they fill the grey area (SO) and they look great.

                                                                So what's the Idea? The idea is to have all of them into the PSD file, checked one by one, positioned correctly if they are not (as the above example) and then exported to 12.5"x16" PNG files. The idea behind the 12.5" x 16" format is that next time when I run the script the design will be exactly on the right place and even if I change the mockup with different one as long as the SO is 12.5" x 16" in size they will be positioned correctly. I know it sounds complicated but for me at this point that's the only way to do it. The other way is to export all these PNGs one by one which will take more time. From now on I will save the designs in the 12.5" x 16" format so I guess it will be an one-off job to fix the position now. Thanks

                                                                • 31. Re: Batch replace smart objects in mockup file
                                                                  kittyk93933155 Level 1

                                                                  To summarize:

                                                                   

                                                                  1. I removed the command line which exports the mockup to jpgs. Now I have a PSD file with all the designs already imported into the SO, so I don't have to do that one by one when I fix the position of every design design, I will simply jump to the next layer.

                                                                  2. Once they are fixed I have to export all the SO layers to PNGs with the size of the SO (12.5" x 16") - at this point nothing depends from the visible area - the design can be full (12.5" x 16"), half (12.5" x 8") or quarter (12.5" x 4") they will be all exported as 12.5" x 16" PNGs.

                                                                  3. Once I have all the 12.5" x 16" PNGs I can run the script again with the command line which saves the PSD to jpgs.

                                                                  Done!

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

                                                                    So is there still a question or have you mastered the issue by yourself anyway?

                                                                    • 33. Re: Batch replace smart objects in mockup file
                                                                      kittyk93933155 Level 1

                                                                      The question is how to export the layers to their folders? Is it possible? How Photoshop will find out which layer from which folder is?

                                                                      • 34. Re: Batch replace smart objects in mockup file
                                                                        kittyk93933155 Level 1

                                                                        Now I've noticed that the script at some point started placing the top of the PNGs from the center instead from the top and it cuts the design in the middle if it's full size. What's wrong?

                                                                        • 35. Re: Batch replace smart objects in mockup file
                                                                          c.pfaffenbichler Level 9
                                                                          Now I've noticed that the script at some point started placing the top of the PNGs from the center instead from the top and it cuts the design in the middle if it's full size. What's wrong?

                                                                          I don’t know, I don’t have neither the files nor the final Script you are using.

                                                                          Also didn’t you say you placed the images manually?

                                                                           

                                                                          Quite frankly your process seems to have gone a bit wild.

                                                                          When the Script was supposed to align the images to the top I posted the line

                                                                          activeDocument.activeLayer.translate(0, activeDocument.activeLayer.bounds[1] * (-1))

                                                                          which uses the Layer’s bounds values to move it to the top, so I am not sure what exactly is the purpose and the problem now.

                                                                           

                                                                          If you want to manually determine the placement of the individual images without disrupting the rest of the process you could change

                                                                          executeAction( idTrnf, desc24, DialogModes.NO );

                                                                          to

                                                                          executeAction( idTrnf, desc24, DialogModes.ALL );

                                                                          and you should get the Transformation controls for each places png, but the Script should simply process after each one is OK-ed.

                                                                          • 36. Re: Batch replace smart objects in mockup file
                                                                            kittyk93933155 Level 1

                                                                            Probably I am doing it wrong. I removed the line which saves the PSD to JPG so I have:

                                                                             

                                                                            // place png files in selected smart object and save jpg;

                                                                             

                                                                            // 2018, use it at your own risk;

                                                                             

                                                                            #target photoshop

                                                                             

                                                                            if (app.documents.length > 0) {

                                                                             

                                                                            var myDocument = app.activeDocument;

                                                                             

                                                                            var basename = 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 = 10;

                                                                             

                                                                            // check if layer is smart object;

                                                                             

                                                                            if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")}

                                                                             

                                                                            else {

                                                                             

                                                                            // select files;

                                                                             

                                                                            var theFolder = Folder.selectDialog ("select folder");

                                                                             

                                                                            var theFiles = retrievePngFiles (theFolder, []);

                                                                             

                                                                            if (theFiles) {

                                                                             

                                                                            // work through the array;

                                                                             

                                                                            for (var m = 0; m < theFiles.length; m++) {

                                                                             

                                                                            var thePath = File(theFiles[m]).path;

                                                                             

                                                                            // open smart object;

                                                                             

                                                                            var smartObject = openSmartObject (theLayer);

                                                                             

                                                                            var theLayer2 = smartObject.activeLayer;

                                                                             

                                                                            // place file and fit to convas;

                                                                             

                                                                            var theNewOne = placeScaleRotateFile (theFiles[m], 0, 0, 100, 100, 0);

                                                                             

                                                                            scaleLayerToFitCanvas ();

                                                                             

                                                                            hideOthers ();

                                                                             

                                                                            // close;

                                                                             

                                                                            smartObject.close(SaveOptions.SAVECHANGES);

                                                                             

                                                                            // undo;

                                                                             

                                                                            myDocument.activeHistoryState = myDocument.historyStates[myDocument.historyStates.length - 1];

                                                                             

                                                                            }

                                                                             

                                                                            }

                                                                             

                                                                            }

                                                                             

                                                                            };

                                                                             

                                                                            ////// get png from subfolders //////

                                                                             

                                                                            function retrievePngFiles (theFolder, theFiles) {

                                                                             

                                                                              if (!theFiles) {var theFiles = []};

                                                                             

                                                                              var theContent = theFolder.getFiles();

                                                                             

                                                                              for (var n = 0; n < theContent.length; n++) {

                                                                             

                                                                              var theObject = theContent[n];

                                                                             

                                                                              if (theObject.constructor.name == "Folder") {

                                                                             

                                                                              theFiles = retrievePngFiles(theObject, theFiles)

                                                                             

                                                                              };

                                                                             

                                                                              if (theObject.name.slice(-4).match(/\.(png)$/i) ) {

                                                                             

                                                                              theFiles.push(theObject)

                                                                             

                                                                              }

                                                                             

                                                                              };

                                                                             

                                                                              return theFiles

                                                                             

                                                                              };

                                                                             

                                                                            ////// place //////

                                                                             

                                                                            function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {

                                                                             

                                                                            // =======================================================

                                                                             

                                                                            var idPlc = charIDToTypeID( "Plc " );

                                                                             

                                                                                var desc5 = new ActionDescriptor();

                                                                             

                                                                                var idnull = charIDToTypeID( "null" );

                                                                             

                                                                                desc5.putPath( idnull, new File( file ) );

                                                                             

                                                                                var idFTcs = charIDToTypeID( "FTcs" );

                                                                             

                                                                                var idQCSt = charIDToTypeID( "QCSt" );

                                                                             

                                                                                var idQcsa = charIDToTypeID( "Qcsa" );

                                                                             

                                                                                desc5.putEnumerated( idFTcs, idQCSt, idQcsa );

                                                                             

                                                                                var idOfst = charIDToTypeID( "Ofst" );

                                                                             

                                                                                    var desc6 = new ActionDescriptor();

                                                                             

                                                                                    var idHrzn = charIDToTypeID( "Hrzn" );

                                                                             

                                                                                    var idPxl = charIDToTypeID( "#Pxl" );

                                                                             

                                                                                    desc6.putUnitDouble( idHrzn, idPxl, xOffset );

                                                                             

                                                                                    var idVrtc = charIDToTypeID( "Vrtc" );

                                                                             

                                                                                    var idPxl = charIDToTypeID( "#Pxl" );

                                                                             

                                                                                    desc6.putUnitDouble( idVrtc, idPxl, yOffset );

                                                                             

                                                                                var idOfst = charIDToTypeID( "Ofst" );

                                                                             

                                                                                desc5.putObject( idOfst, idOfst, desc6 );

                                                                             

                                                                                var idWdth = charIDToTypeID( "Wdth" );

                                                                             

                                                                                var idPrc = charIDToTypeID( "#Prc" );

                                                                             

                                                                                desc5.putUnitDouble( idWdth, idPrc, theYScale );

                                                                             

                                                                                var idHght = charIDToTypeID( "Hght" );

                                                                             

                                                                                var idPrc = charIDToTypeID( "#Prc" );

                                                                             

                                                                                desc5.putUnitDouble( idHght, idPrc, theXScale );

                                                                             

                                                                                var idAngl = charIDToTypeID( "Angl" );

                                                                             

                                                                                var idAng = charIDToTypeID( "#Ang" );

                                                                             

                                                                                desc5.putUnitDouble( idAngl, idAng,theAngle );

                                                                             

                                                                                var idLnkd = charIDToTypeID( "Lnkd" );

                                                                             

                                                                                desc5.putBoolean( idLnkd, true );

                                                                             

                                                                            executeAction( idPlc, desc5, DialogModes.NO );

                                                                             

                                                                            return app.activeDocument.activeLayer;

                                                                             

                                                                            };

                                                                             

                                                                            ////// scale layer to fit canvas //////

                                                                             

                                                                            function scaleLayerToFitCanvas () {

                                                                             

                                                                            var ref = new ActionReference();

                                                                             

                                                                            ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("bounds"));

                                                                             

                                                                            ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                                                                             

                                                                            var layerDesc = executeActionGet(ref);

                                                                             

                                                                            var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));

                                                                             

                                                                            var layerX = theBounds.getUnitDoubleValue(stringIDToTypeID("left"));

                                                                             

                                                                            var layerY = theBounds.getUnitDoubleValue(stringIDToTypeID("top"));

                                                                             

                                                                            var layerWidth = theBounds.getUnitDoubleValue(stringIDToTypeID("right"))-layerX;

                                                                             

                                                                            var layerHeight = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))-layerY;

                                                                             

                                                                            var ref1 = new ActionReference();

                                                                             

                                                                            ref1.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                                                                             

                                                                            var docDesc = executeActionGet(ref1);

                                                                             

                                                                            var docWidth = docDesc.getUnitDoubleValue(stringIDToTypeID("width"));

                                                                             

                                                                            var docHeight = docDesc.getUnitDoubleValue(stringIDToTypeID("height"));

                                                                             

                                                                            var docRes = docDesc.getInteger(stringIDToTypeID("resolution"));

                                                                             

                                                                            var scaleX = docWidth/layerWidth*docRes/72*100;

                                                                             

                                                                            var scaleY = docHeight/layerHeight*docRes/72*100;

                                                                             

                                                                            var theScale = Math.min(scaleX, scaleY);

                                                                             

                                                                            layerX = ((docWidth*docRes/144) - (layerX+layerWidth/2));

                                                                             

                                                                            layerY = ((docHeight*docRes/144) - (layerY+layerHeight/2));

                                                                             

                                                                            activeDocument.activeLayer.translate(0, activeDocument.activeLayer.bounds[1] * (-1))

                                                                             

                                                                            // =======================================================

                                                                             

                                                                            var idTrnf = charIDToTypeID( "Trnf" );

                                                                             

                                                                                var desc24 = new ActionDescriptor();

                                                                             

                                                                            //    desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ),

                                                                             

                                                                            charIDToTypeID( "Qcs0" ) );

                                                                             

                                                                                desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID(

                                                                             

                                                                            "Qcsa" ) );

                                                                             

                                                                                var idOfst = charIDToTypeID( "Ofst" );

                                                                             

                                                                                    var desc25 = new ActionDescriptor();

                                                                             

                                                                                    var idHrzn = charIDToTypeID( "Hrzn" );

                                                                             

                                                                                    var idPxl = charIDToTypeID( "#Pxl" );

                                                                             

                                                                                    desc25.putUnitDouble( idHrzn, idPxl, layerX);

                                                                             

                                                                                    var idVrtc = charIDToTypeID( "Vrtc" );

                                                                             

                                                                                    desc25.putUnitDouble( idVrtc, idPxl, layerY);

                                                                             

                                                                                desc24.putObject( idOfst, idOfst, desc25 );

                                                                             

                                                                                var idWdth = charIDToTypeID( "Wdth" );

                                                                             

                                                                                var idPrc = charIDToTypeID( "#Prc" );

                                                                             

                                                                                desc24.putUnitDouble( idWdth, idPrc, theScale );

                                                                             

                                                                                var idHght = charIDToTypeID( "Hght" );

                                                                             

                                                                                desc24.putUnitDouble( idHght, idPrc, theScale );

                                                                             

                                                                            executeAction( idTrnf, desc24, DialogModes.NO );

                                                                             

                                                                            };

                                                                             

                                                                            ////// open smart object //////

                                                                             

                                                                            function openSmartObject (theLayer) {

                                                                             

                                                                            if (theLayer.kind == "LayerKind.SMARTOBJECT") {

                                                                             

                                                                            // =======================================================

                                                                             

                                                                            var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );

                                                                             

                                                                                var desc2 = new ActionDescriptor();

                                                                             

                                                                            executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );

                                                                             

                                                                            };

                                                                             

                                                                            return app.activeDocument

                                                                             

                                                                            };

                                                                             

                                                                            ////// hide others //////

                                                                             

                                                                            function hideOthers () {

                                                                             

                                                                            // hide others;

                                                                             

                                                                            // =======================================================

                                                                             

                                                                            var idShw = charIDToTypeID( "Shw " );

                                                                             

                                                                                var desc2 = new ActionDescriptor();

                                                                             

                                                                                var idnull = charIDToTypeID( "null" );

                                                                             

                                                                                    var list1 = new ActionList();

                                                                             

                                                                                        var ref1 = new ActionReference();

                                                                             

                                                                                        var idLyr = charIDToTypeID( "Lyr " );

                                                                             

                                                                                        var idOrdn = charIDToTypeID( "Ordn" );

                                                                             

                                                                                        var idTrgt = charIDToTypeID( "Trgt" );

                                                                             

                                                                                        ref1.putEnumerated( idLyr, idOrdn, idTrgt );

                                                                             

                                                                                    list1.putReference( ref1 );

                                                                             

                                                                                desc2.putList( idnull, list1 );

                                                                             

                                                                                var idTglO = charIDToTypeID( "TglO" );

                                                                             

                                                                                desc2.putBoolean( idTglO, true );

                                                                             

                                                                            executeAction( idShw, desc2, DialogModes.NO );

                                                                             

                                                                            };

                                                                             

                                                                            This gives me a Mockup template with all the PNGs into the SO and nothing else. Once I have the correct PNGs as I explained prevoiusly I will add the line which saves the PSD to JPG. But before that I need to export all the PNGs from the SO into their folders with the new size 12.5" x 16". I don't know if the script has an issue with the quantity of the PNGs imported into the SO but for me should be fine. If the script can import 1 png it can import a 1000 too because it repeats the same steps again and again. Weird!

                                                                            • 37. Re: Batch replace smart objects in mockup file
                                                                              kittyk93933155 Level 1

                                                                              I guess I am doing it wrong by importing the PNGs into the SO because I don't need the SO for this. I simply need the PNG imported and scaled into a PSD file with dimensions 12.5"x16". The question is if I have one PSD file and too scripts (one to import and one to export the PNGs) how Photoshop will know where to export the PNGs with the second script?

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

                                                                                It seems you had not thought the process through in the beginning.

                                                                                 

                                                                                If you had prepared all the pngs to fit the SO’s dimensions from the start (and said as much) the whole alignment issue would have been moot. And I would not have included the function to scale the placed png to fit the opened SO’s canvas.

                                                                                But instead of manually placing the images within the Script-run you seem to be trying to break the task into two separate Scripts.

                                                                                 

                                                                                how Photoshop will know where to export the PNGs with the second script?

                                                                                The placed pngs should have the files’ names, so at worst selecting the Folder again should allow identifying the original png-location.

                                                                                 

                                                                                You may want to start a new thread so someone else may help you.

                                                                                Good luck!

                                                                                • 39. Re: Batch replace smart objects in mockup file
                                                                                  kittyk93933155 Level 1

                                                                                  Now all PNGs are saved as 12.5"x16" and still the script puts them randomly - too high too low. What's wrong? Why it's not working the same way for every file? I tried with few different mockups and the results are different.

                                                                                  1 2 Previous Next