我正在尝试弄清楚如何在 Photoshop 中使用 javascript,但尽管我没有在代码中发现逻辑错误,但它无法正常工作。
我有一个包含 1000 多个图像/.ai 文件的文件夹,这些文件的尺寸各不相同。我需要将这些图像放在枕头上并保存为 .jpeg。 我选择 smartlayer 并运行脚本来选择图像,它会正确保存它们。唯一的问题是,图像大小的调整和定位无法正常工作。 如果我手动放入图像,它可以正常工作,但不能使用脚本。
如果宽度大于高度,则应将宽度设置为 1200 px,并据此计算高度。 (反之亦然)并放置在层的中间。
有人知道这段代码的问题出在哪里吗? 我很感激任何帮助!
// 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;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.ai", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
};
(function (){
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var bounds = activeDocument.activeLayer.bounds;
var height = bounds[3].value - bounds[1].value;
var width = bounds[2].value - bounds[0].value;
if (height > width){
var newSize1 = (100 / width) * 800;
activeDocument.activeLayer.resize(newSize1, newSize1, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = startRulerUnits;
}
else{
var newSize2 = (100 / height) * 800;
activeDocument.activeLayer.resize(newSize2, newSize2, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = startRulerUnits;
}
})();
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
};
您可能需要一个函数来递归遍历文档中的所有图层
function browseLayer(layer, fn) {
if (layer.length) {
for (var i = 0; i < layer.length; ++i) {
browseLayer(layer[i], fn)
}
return;
}
if (layer.layers) {
for (var j = 0; j < layer.layers.length; ++j) {
browseLayer(layer.layers[j], fn);
}
return;
}
//apply this function for every layer
fn(layer)
}
获取文档中所有智能对象
const smartObjects = [];
//The smart objects can be visit twice or more
//use this object to mark the visiting status
const docNameIndex = {};
const doc = app.open(new File("/path/to/psd/file"));
browseLayer(doc.layers, function (layer) {
//You cannot replace smart object with position is locked
if (layer.kind == LayerKind.SMARTOBJECT && layer.positionLocked == false) {
smartLayers.push(layer);
doc.activeLayer = layer;
//open the smart object
executeAction(stringIDToTypeID("placedLayerEditContents"), new ActionDescriptor(), DialogModes.NO);
//activeDocument is now the smart object
const docName = app.activeDocument.name;
if (!docNameIndex[docName]) {
docNameIndex[docName] = true;
smartObjects.push({
id: layer.id,
name: layer.name,
docName: docName,
width : app.activeDocument.width.as('pixels'),
height : app.activeDocument.height.as('pixels'),
resolution : app.activeDocument.resolution //important
});
}
//reactive the main document
app.activeDocument = doc;
}
});
我假设你有两个智能对象需要替换,替换的图像存储在具有相同名称的不同文件夹中。
smartObjects[0].replaceFolderPath = "/path/to/folder/1";
smartObjects[1].replaceFolderPath = "/path/to/folder/2";
//we need temp folder to store the resize images
smartObjects[0].tempFolderPath = "/path/to/temp/folder/1";
smartObjects[1].tempFolderPath = "/path/to/temp/folder/2";
例如:第一次迭代将用
smartObjects[0]
替换 "/path/to/folder/1/image1.jpg"
,用 smartObjects[1]
替换
"/path/to/folder/image1.jpg"
现在根据智能对象的属性调整所有图像的大小
smartObjects.forEach(function(smartObject){
//Get all files in the folder
var files = new Folder(smartObject.replaceFolderPath).getFiles();
//Resize all the image files
files.forEach(function (file) {
var doc = app.open(file);
doc.resizeImage(
new UnitValue(smartObject.width + ' pixels'),
new UnitValue(smartObject.height + ' pixels'),
smartObject.resolution
);
//save to temp folder
doc.saveAs(
new File(smartObject.tempFolderPath + "/" + file.name),
new PNGSaveOptions(),
true
);
doc.close(SaveOptions.DONOTSAVECHANGES)
});
});
最后更换智能对象
//get list of file again
var files = new Folder(smartObject.replaceFolderPath).getFiles();
files.forEach(function(file){
var fileName = file.name;
smartObjects.forEach(function(smartObject){
//active the window opening the smart object
app.activeDocument = app.documents.getByName(args.documentName);
var desc = new ActionDescriptor();
desc.putPath(charIDToTypeID("null"), new File(smartObject.tempFolderPath + "/" + fileName));
executeAction(stringIDToTypeID( "placedLayerReplaceContents" ), desc, DialogModes.NO);
});
//Now export document
var webOptions = new ExportOptionsSaveForWeb();
webOptions.format = SaveDocumentType.PNG; // SaveDocumentType.JPEG
webOptions.optimized = true;
webOptions.quality = 100;
doc.exportDocument(new File("/path/to/result/folder" + file.name), ExportType.SAVEFORWEB, webOptions);
});
现在您可以关闭所有打开的智能对象
smartObjects.forEach(function (s) {
app.documents.getByName(r.docName).close(SaveOptions.DONOTSAVECHANGES);
});
我使用三种不同的方法解决了这个问题:
解决方案 1) 进行预处理步骤,批量调整所有图像的大小,使其达到完美适合智能对象所需的精确大小。例如,您可以通过记录一个操作来做到这一点,该操作将其大小调整为特定尺寸,以所需的 DPI,然后将其作为批处理运行。 (如果您不熟悉如何在 Photoshop 中记录操作或运行批处理操作,请参阅 Adobe 官方网站上的此操作指南和此批处理指南。)然后在预处理图像后,您需要更新代码以使用调整大小后的图像最终所在的任何输出文件夹中的图像。然后您应该能够运行此 .jsx 代码,通过 Photoshop 脚本自动执行智能对象批量替换。
解决方案 2) 有一个名为 批量替换智能对象 的 Photoshop 插件,它可以自动执行此过程 - 并且您还可以检查一个选项,该选项将“拉伸以适合智能对象”。该插件的工作方式是,您指定要使用的 Photoshop 文档、图像的输入文件夹以及要导出到的输出文件夹。如果您选择“拉伸以适合智能对象”选项,这将调整所有图像的大小以完全适合智能对象的尺寸。 (请注意,当您通过此插件自动执行此操作时,它不是将其作为预处理步骤执行,而是在操作运行时实时调整图像大小。无论哪种方式都并不重要,因为最终结果是相同 - 使用此插件的事件顺序略有不同。)因此,基本上,使用此插件,您可以自动执行批量智能对象替换操作本身 - 但对于每个操作,还可以选择拉伸所有图像都适合智能对象的尺寸。
解决方案 3) 您可以修改 .jsx 代码/Photoshop 脚本来实际运行所需的批处理操作,而不是自己手动运行批处理操作,但只需将其编码到脚本中即可以编程方式执行此操作。当我需要将其自动化作为更大的 Photoshop 工作流程的一部分时,我就使用了这种方法,其中所有命令都需要按顺序运行。我会检查 Adobe 社区论坛,看看您是否可以找到一些有关如何通过 .jsx 代码而不是通过 Photoshop 界面执行此操作的指南。