2017-03-07 77 views
0

我目前正在嘗試添加到此Photoshop腳本(該腳本當前從文件夾中抓取大量圖像文件並替換智能對象的內容並保存單個jpgs):保存目標文件夾Photoshop Javascript

#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相同的文件夾。

希望這是有道理的!

由於提前,

裏克

+0

好了,和你在哪裏有問題? – Amy

+0

嗨艾米!非常感謝答覆。我的問題實際上是我缺乏JavaScript讀寫能力:S。我不確定在這段代碼中最好插入正確的代碼行來打開一個對話框,該對話框允許我爲保存的文件設置目標文件夾。 –

回答

1

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