• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Smart Object Automation Script

Community Beginner ,
Mar 07, 2017 Mar 07, 2017

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

3.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Mar 09, 2017 Mar 09, 2017

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;

   

...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 08, 2017 Mar 08, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 08, 2017 Mar 08, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 09, 2017 Mar 09, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 09, 2017 Mar 09, 2017

Copy link to clipboard

Copied

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

};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 16, 2017 Mar 16, 2017

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Dec 29, 2020 Dec 29, 2020

Copy link to clipboard

Copied

LATEST

@Deleted User 

 

its been a while but you need to add a bool for that. see below. You said you changed it to PSD. So im not sure how you named the first part. Thats needs to be the same as the rest of your edit.

 

psdopts.layers = true;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 08, 2017 Nov 08, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 08, 2017 Nov 08, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 09, 2017 Nov 09, 2017

Copy link to clipboard

Copied

Hi

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines