6 Replies Latest reply on Apr 24, 2015 7:19 AM by cwodwrth

    Run an action on a file before inserting into a Smart Object

    cwodwrth Level 1

      I'm programmatically replacing the content of smart object using the following code (c.pfaffenbichler):

      ..

      if ($.os.search(/windows/i) != -1)

      {

          var folder = new Folder("F:/Test Folder);

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

      }

       

      if (theFiles) {

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

           theLayer = replaceContents (theFiles[m], theLayer);

      ..

      I want to be able to run an action on theFiles[m] right before inserting it, ideally without saving it off, how do I go about doing that?

       

      Thanks for the help!

        • 1. Re: Run an action on a file before inserting into a Smart Object
          c.pfaffenbichler Level 9

          As far as I understand what you want is impossible in Photoshop.

          It can not perform operations on files without the files being open in Photoshop and an open, unsaved file can not be placed as a Smart Object.

          Work-arounds might be

          • saving the file as a new file, replacing the contents with it, removing the new file automatically

          • replacing the contents, then opening the SO, running the Action and saving it

          • …

          • 2. Re: Run an action on a file before inserting into a Smart Object
            cwodwrth Level 1

            OK, sounds like it might be a bit more trouble than it's worth in that case since I was just trying to prevent a whole bunch of psd files from hanging around after each run, but not the end of the world. Thanks for your help

            • 3. Re: Run an action on a file before inserting into a Smart Object
              c.pfaffenbichler Level 9

              Scripts can remove files, so if you provide more details someone might be able to help you with customising yours.

              • 4. Re: Run an action on a file before inserting into a Smart Object
                cwodwrth Level 1

                Well I think the issue I would run into would be having two active documents though. So let's say I have the file list with all the names, I'm guessing I need to just do the commented out steps? Any code you could offer would be great as my entire script it a mashup of about 7 different posts I've found here.

                 

                 

                if ($.os.search(/windows/i) != -1)

                {

                    var folder = new Folder("F:/Test Folder);

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

                }

                 

                if (theFiles) {

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

                 

                //Open File

                //Run Action  ACTION_SET/ACTION_NAME

                //Close File

                //Replace using New File    

                //theLayer = replaceContents ([NEW FILE], theLayer);

                //Delete New File

                 

                 

                ..

                • 5. Re: Run an action on a file before inserting into a Smart Object
                  c.pfaffenbichler Level 9

                  If you don’t want to post the code trying to help you with it seems difficult.


                   

                  Well I think the issue I would run into would be having two active documents though.

                   

                  The file that’s to be switched in for the SO can be closed after saveAs (to a new location), so by the time replaceContents is supposed to happen the activeDocument can be the original one again.

                  • 6. Re: Run an action on a file before inserting into a Smart Object
                    cwodwrth Level 1

                    Sorry about that, here is the current code:

                     

                    //WHEN SAVING JPG

                    var jpgopts = new JPEGSaveOptions();

                    jpgopts.embedProfile = true;

                    jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;

                    jpgopts.matte = MatteType.NONE;

                    jpgopts.quality = 10;

                     

                    //WHEN SAVING PSD

                    psdOpts = new PhotoshopSaveOptions();

                    psdOpts.embedColorProfile = true;

                    psdOpts.alphaChannels = true;

                    psdOpts.layers = true;

                     

                    if (app.documents.length > 0) {

                     

                    var myDocument = app.activeDocument;

                     

                    var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];

                     

                    var thePath = myDocument.path;

                     

                    var theLayer = myDocument.layerSets["Sizes"].artLayers.getByName ("11x11");

                     

                     

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

                       

                        var folder = new Folder("F:/Input");

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

                    }

                     

                    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];

                     

                    myDocument.saveAs((new File("F:/Output/" + theNewName + ".jpg")),jpgopts,true);

                     

                    }

                    }