Reputation: 41
I'm currently trying to make an addition to this Photoshop script, (the script currently grabs a number of image files from a folder and replaces the content of a smart object and saves individual jpgs out):
#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)};
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|png)$/i)
} 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[m], theLayer);
var theNewName = theFiles[m].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
};
What i'm looking to do is incorporate a way to open up a dialogue which would allow me to select a destination folder for the saved files, currently it saves the files in the same folder as the open psd.
Hope this makes sense!
Thanks in advance,
Rik
Upvotes: 2
Views: 2811
Reputation: 351
It looks like your script currently requires you to manually select the folder locations each time? You may be able to save more time, in automating this process, by simply hardcoding the specific folderpaths you use in your workflow. This could allow you to just, run the script and have it execute -- vs. requiring you to manually do more work after starting it. See this Adobe Community post where a similar change was made to a similar Photoshop script.
Also note -- if you're not very technically inclined, you could also use a Photoshop plugin which automates this same process for you. For example, this plugin allows you to save batch Smart Object replacement operations, which can be run as needed, with one click. If your input and output folders are standardized, you could just run these as saved operations to save a bit of time -- vs. needing to specify all these parameters each time.
Especially if there are several steps in your workflow (for example, if you need to loop through a whole series of Photoshop documents, to batch replace the Smart Objects -- maybe if you're creating lots of different mockup images), I'd recommend hardcoding the folder/filepaths, or using a plugin to just create a saved multiple-step operation that can be run on demand. This way, you won't need to sit there at your computer, and specify this each time for each operation -- you can just, click Run, go do something else, and the whole thing will execute from start to finish.
Upvotes: 0
Reputation: 1484
Stack Overflow is not a script writing service. But it you want to learn then you simply need to add the option of a folder dialog
var thePath = myDocument.path;
// manually save det destination folder
var outputFolder = Folder.selectDialog("Choose folder to save files to");
and then set this when you save out your files
// myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpgopts,true);
myDocument.saveAs((new File(outputFolder+"/"+theName+"_"+theNewName+".jpg")),jpgopts,true);
Upvotes: 3