Javascript 以不同尺寸调整图像大小,其中只有宽度很重要

Javascript resize image in different sizes where only the width matters

我正在尝试为 Photoshop 制作一个脚本,它可以将打开的图像调整为不同的大小,其中只有宽度很重要。目标是在每次调整大小后,它应该恢复图像的原始状态和 运行 不同的宽度。网上有一些脚本几乎可以做到这一点,但我只会出错。我得到的错误是 "undefined is not an object"。

我目前有以下脚本,但卡住了:

// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;


// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 940;


// our web export optionsvar options = new ExportOptionsSaveForWeb();

function SaveJPEG(saveFile, quality) {
  var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb();
  exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;
  exportOptionsSaveForWeb.includeProfile = false;
  exportOptionsSaveForWeb.interlaced = false;
  exportOptionsSaveForWeb.optimized = true;
  exportOptionsSaveForWeb.quality = 80;


}


var newName = 'F' + doc.name + '.jpg';

doc.exportDocument(File(doc.path + '/' + newName), ExportType.SAVEFORWEB, options);

doc.activeHistoryState = doc.historyStates.index(0);

未定义的错误是在 var newName 行停止脚本。我必须补充一点,它适用于 Photoshop CS6。

非常感谢任何帮助。

如果问题是它破坏了你的脚本,你应该尝试使用 try..catch 语句

...

var newName;

try {
  newName = 'F' + doc.name + '.jpg';
} catch (e) {
  newName = 'F' + 'name-error' + '.jpg';
  console.log('doc.name is not accessible: ' + e.message;);
}

...