9 Replies Latest reply on Nov 9, 2017 2:48 AM by nicolew49222393

    Smart Object Automation Script

    rikjackson7

      Hi,

       

      I'm currently trying to make a couple of tweaks to this script, (the script currently grabs a number of files from a folder and replaces the content of a smart object and saves individual jpgs):

       

      #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|png)$/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 need to change the script to select a folder of images, rather than specific files using this:

       

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

      if (theFolder) {

      var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i)

      };

       

      However my lack of JS experience is showing and i can't figure out where to insert the new lines of code.

       

      It would also be ideal if I could incorporate a way to open up a dialogue which would allow me to select a destination folder for the saved files.

       

      Hope this makes sense!

       

      Thanks in advance,

       

      Rik

        • 1. Re: Smart Object Automation Script
          JJMack Most Valuable Participant

          A quick look at that script I see If  there is a document open the script checks to see if the targeted layer is a smart object and if  it is will prompt you with a file selection dialog to select files.

           

          You would replace that dialog with a select folder dialog then process the image file types in that folder.  They should all be exactly the same size and resolution as the original smart object so that the smart object layer's associated transform will transform the image correctly for the layers transform is not replaced.

          • 2. Re: Smart Object Automation Script
            rikjackson7 Level 1

            Thanks JJMack! I figured out how to get the script to load a folder in. But when it comes to setting a save destination i'm still at a bit of a loss.

             

            Rik

            • 3. Re: Smart Object Automation Script
              Tomas Sinkunas Adobe Community Professional

              Hei Rik.

              One way for you to select destination folder is to call SelectDialog method

              var destinationFolder = Folder.selectDialog("Please select a destination folder");
              

               

              then once you have that, you can pass this to your saveAs function

              myDocument.saveAs(new File(destinationFolder + "/" + theName + "_" + theNewName + ".jpg"), jpgopts, true);
              
              1 person found this helpful
              • 4. Re: Smart Object Automation Script
                Tomas Sinkunas Adobe Community Professional

                Took a liberty to add previous lines to code. Check if it works.

                #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;
                        var theFolder = Folder.selectDialog("select folder");
                        if (theFolder) {
                            var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|png)$/i);
                            // Select destination folder
                            var destinationFolder = Folder.selectDialog("Please select a destination folder");
                            // 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(destinationFolder.toString() + "/" + theName + "_" + theNewName + ".jpg")), jpgopts, 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
                };
                
                2 people found this helpful
                • 5. Re: Smart Object Automation Script
                  rikjackson7 Level 1

                  Thanks so much Tomas!

                   

                  I got about half way there, but was still struggling with getting the dialogue to communicate with the save part ( lots to learn ).

                   

                  Really, really appreciate the help!!

                  • 6. Re: Smart Object Automation Script
                    Kropotkin1886 Level 1

                    Hey guys,

                     

                    thanks for your input! This script comes in well handy!

                     

                    I do have an issue though. I changed the script to save .PSD's instead of .JPG's in order to keep the "smart object layer" intact.

                    But now the new PSD's smart object has been transformed into a locked background layer, prohibiting me from editing said smart object. 

                     

                    Is there anything I can add to the "psd options" to stop this from happening?

                    • 7. Re: Smart Object Automation Script
                      nicolew49222393 Level 1

                      jpgopts.quality = 8;

                       

                      Just wondering if there is a way to save the jpg as a percentage value. So I can input 50% as the value for quality

                      • 8. Re: Smart Object Automation Script
                        geppettol66959005 Level 2

                        for value you can enter 6 which is equivalent to 50%.

                        • 9. Re: Smart Object Automation Script
                          nicolew49222393 Level 1

                          Hi

                           

                          Thanks for the reply. Just wondering if there is any way to put it in as a percentage?