保存目标文件夹 Photoshop Javascript

问题描述 投票:0回答:2

我目前正在尝试对此 Photoshop 脚本进行添加,(该脚本当前从文件夹中获取多个图像文件并替换智能对象的内容并保存单个 jpg):

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

我想要做的是采用一种打开对话框的方法,该对话框允许我为保存的文件选择目标文件夹,目前它将文件保存在与打开的 psd 相同的文件夹中。

希望这是有道理的!

提前致谢,

瑞克

javascript photoshop photoshop-script
2个回答
3
投票

Stack Overflow 不是脚本编写服务。但如果您想学习,那么您只需添加文件夹对话框的选项即可

var thePath = myDocument.path;
// manually save det destination folder
var outputFolder = Folder.selectDialog("Choose folder to save files to");

然后在保存文件时进行设置

// myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpgopts,true);
myDocument.saveAs((new File(outputFolder+"/"+theName+"_"+theNewName+".jpg")),jpgopts,true);

0
投票

您的脚本当前似乎需要您每次手动选择文件夹位置?通过简单地对工作流程中使用的特定文件夹路径进行硬编码,您可以在自动化此过程中节省更多时间。这可以让您只需运行脚本并执行它,而不是要求您在启动脚本后手动执行更多工作。请参阅此 Adobe 社区帖子,其中对类似的 Photoshop 脚本进行了类似的更改。

另请注意 - 如果您不太懂技术,您也可以使用 Photoshop 插件,它可以为您自动执行相同的过程。例如,这个插件允许您保存批量智能对象替换操作,可以根据需要运行,一键完成。如果您的输入和输出文件夹是标准化的,您可以将它们作为保存的操作运行以节省一些时间 - 而不是每次都需要指定所有这些参数。

特别是如果您的工作流程中有多个步骤(例如,如果您需要循环遍历整个系列的 Photoshop 文档,批量替换智能对象 - 也许您正在创建许多不同的模型图像),我'建议对文件夹/文件路径进行硬编码,或使用插件来创建可按需运行的保存的多步骤操作。这样,您就不需要坐在计算机前,每次为每个操作都指定这一点 - 您只需单击“运行”,去做其他事情,整个事情就会从头到尾执行。

© www.soinside.com 2019 - 2024. All rights reserved.