Skip to main content
Participating Frequently
March 7, 2017
Answered

Smart Object Automation Script

  • March 7, 2017
  • 1 reply
  • 4095 views

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

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

//save jpg;

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

                    }

          }

}

};

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

     if (theFile.name.match(/\.(psd|tif|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

This topic has been closed for replies.
Correct answer Tomas Sinkunas

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

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

};

1 reply

JJMack
Community Expert
Community Expert
March 8, 2017

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.

JJMack
Participating Frequently
March 8, 2017

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

Tomas Sinkunas
Legend
March 9, 2017

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