Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

New Here ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting

Views

124

Likes

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 2 Correct answers

Adobe Community Professional , Oct 26, 2021 Oct 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. не проверял...

Likes

Translate

Translate
Engaged , Oct 26, 2021 Oct 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 pos...

Likes

Translate

Translate
Adobe Community Professional ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

это

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. не проверял...

Likes

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 ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

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

 

error message 1.png

 

// Replace SmartObject’s Content and Save as JPG
// 2017, use it at your own risk
// Via @circle 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
};

Likes

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
Engaged ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

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.

Likes

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 ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

Worked like a charm! Looks like the quotation marks around the filename is what was needed.

 

My hero! 😛

 

 

I really appreciate your help with this!

 

The last main thing I need to figure out how to automate in my Photoshop workflow is the running of batches with all of the inputs pre-selected beforehand, as everything in this process is 100% standardized and predictable: How to run Photoshop batch via Javascript (vs manually selecting all attributes?)

 

The only current way I can automate this step is with a really mediocre process of literally sending mouse clicks to specific spots on the page, and then going through the process of navigating to the folders to be used in the batch. A terrible way of automating processes that I just hate to do because it's so arbitrary and error-prone.

 

I'm thinking an easier way might be some method of loading a script inside of Photoshop that functionally runs the batch with the inputs all pre-selected: What "Set" will be used, what "Action" will be used from that set, what the "Source Folder" will be, and what the "Destination Folder" will be. I see no reason why this couldn't be accomplished via a script that's loaded into Photoshop that has all of these inputs pre-written inside of it.

Likes

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
Engaged ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

LATEST

No worries. Turns out my answer on reddit was correct but it was unhelpful to tell you about replacing the entire ternary with it instead of demonstrating like r-bin did above.

 

I'm not really a PS scripter per-se, but in Illustrator you can record batch actions as singular Actions themselves and re-run them at any time. Is this not the case with PS? I'm not sure how to handle specifying batch folders externally for an action otherwise, I only know of how to encode a filepath into the Action parameter key of the rawtext action string, load and run the action, then unload it. I'm sure someone here can solve it, though.

Likes

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