How to modify Photoshop Javascript code to pre-select a folder (vs manually navigating to it?)
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!
