Skip to main content
Known Participant
October 26, 2021
Answered

How to modify Photoshop Javascript code to pre-select a folder (vs manually navigating to it?)

  • October 26, 2021
  • 1 reply
  • 944 views

Hello,

 

I am currently trying to speed up my Photoshop workflow, and a current step uses Javascript code to batch-replace a smart object with a group of images inside of a folder.

 

As currently written, when the code is executed, it requires you to locate, navigate to, and then select the folder to be used. However this is always the same folder each time in the workflow, as everything is standardized -- and I have to do this dozens of times over -- so I am wondering: How can I instead re-write and modify the code so that a specific folder is pre-selected, and it saves me the step of having to navigate there + find it + then select it?

 

For example let's say the folder to be used is as follows:

 

C:\users\anton\Pictures\Photoshop Files\18x24 - 1

 

How could I tweak the code so that, instead of prompting me to locate and select the folder, it just automatically runs + executes the program with this above folder pre-specified as the one to use?

 

The Javascript code in question is written as follows:

 

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

jpgSaveOptions = new JPEGSaveOptions();

jpgSaveOptions.embedColorProfile = true;

jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

jpgSaveOptions.matte = MatteType.NONE;

jpgSaveOptions.quality = 8;

// Check if layer is SmartObject;

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

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

// Replace SmartObject

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

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

// Save JPG

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

}

}

}

};

// Get PSDs, TIFs and JPGs from files

function getFiles(theFile) {

if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {

return true

};

};

// Replace SmartObject 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 know it's something to do with that "GetFiles" section, but I'm a Javascript noob and don't know how to re-write it so it runs the way I'd like it to.

 

Thanks!

This topic has been closed for replies.
Correct answer Inventsable

Try this:

 

var theFiles = new Folder("C:\\users\\anton\\Pictures\\Photoshop Files\\18x24 - 1").getFiles(getFiles);

 

P.S. - Good to see that you took my advice on reddit to come here instead. The forums here are always active and there's never a shortage of very talented people willing to help, it's the only place I bother asking about scripting since it's a very unique environment and non-scripters will often give you a lengthy runaround of convoluted answers that aren't really relevant if not posting code that's incompatible due to differences in ECMA spec.

1 reply

Braniac
October 26, 2021

это

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)

};

заменить на это

var theFiles = (new Folder(C:\\users\\anton\\Pictures\\Photoshop Files\\18x24 - 1)).getFiles(getFiles);

 

p.s. не проверял...

Known Participant
October 26, 2021

Gave me an error message, looks like just a minor syntax thing. What is written incorrectly here?:

 

 

// Replace SmartObject’s Content and Save as JPG
// 2017, use it at your own risk
// Via @10354895 B: https://graphicdesign.stackexchange.com/questions/92796/replacing-a-smart-object-in-bulk-with-photoshops-variable-data-or-scripts/93359
// JPG code from here: https://forums.adobe.com/thread/737789

#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;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 8;   
    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT") {
        alert("selected layer is not a smart object")
    } else {
        // Select Files;
var theFiles = (new Folder(C:\\users\\anton\\Pictures\\Next-Level Artwork\\AUTOMATION ASSETS\\PHOTOSHOP FILES\\REMAKES 2)).getFiles(getFiles);
        if (theFiles) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
                // Save JPG
                myDocument.saveAs((new File(thePath + "/" + theName + "_" + theNewName + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
            }
for (var m = 0; m < theFiles.length; m++) {
    theFiles[m].close();
}
        }
    }
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
    if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
        return true
    };
};
// Replace SmartObject 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
};
Inventsable
InventsableCorrect answer
Braniac
October 26, 2021

Try this:

 

var theFiles = new Folder("C:\\users\\anton\\Pictures\\Photoshop Files\\18x24 - 1").getFiles(getFiles);

 

P.S. - Good to see that you took my advice on reddit to come here instead. The forums here are always active and there's never a shortage of very talented people willing to help, it's the only place I bother asking about scripting since it's a very unique environment and non-scripters will often give you a lengthy runaround of convoluted answers that aren't really relevant if not posting code that's incompatible due to differences in ECMA spec.