You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Photoshop文本颜色批量导出脚本异常问题求助

脚本颜色导出异常修复

我有一个Photoshop脚本,功能是将活动图层的文本依次设置为黑、白、粉、蓝、绿、红六种颜色,并分别导出为PNG文件。目前黑色、蓝色、绿色、红色导出正常,但白色被导出为浅蓝色,粉色文件的颜色和蓝色完全一致。

原脚本代码如下:

var textColor = new SolidColor();
textColor.rgb.hexValue = “000000”;
app.activeDocument.activeLayer.textItem.color = textColor;

var textLayerName = app.activeDocument.activeLayer.name;

var newName = prompt(“Enter a new name for the export”, “black_” + textLayerName);

var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.quality = 100;
var file = new File("black " + newName + “.png”);
app.activeDocument.exportDocument(file, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “FFFFFF”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file2 = new File("white " + newName + “.png”);
app.activeDocument.exportDocument(file2, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “fc82d8”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file3 = new File("pink " + newName + “.png”);
app.activeDocument.exportDocument(file3, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “63a8e7”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file4 = new File("blue " + newName + “.png”);
app.activeDocument.exportDocument(file4, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = “0c6a27”;
app.activeDocument.activeLayer.textItem.color = textColor;

var file5 = new File("green " + newName + “.png”);
app.activeDocument.exportDocument(file5, ExportType.SAVEFORWEB, exportOptions);

var textColor = new SolidColor();
textColor.rgb.hexValue = " a80000";
app.activeDocument.activeLayer.textItem.color = textColor;

var file6 = new File("red " + newName + “.png”);
app.activeDocument.exportDocument(file6, ExportType.SAVEFORWEB, exportOptions);

问题原因

  1. 全角引号错误:脚本中所有引号(包括hexValue的颜色值、prompt参数、File路径中的引号)都用了中文全角引号(“”),但JavaScript只识别英文半角引号("")。这会导致白色、粉色的颜色值无法被正确解析,最终沿用了之前的蓝色值。
  2. 红色Hex格式错误:红色的hexValue前面多了一个空格(" a80000"),虽当前红色导出正常,但不符合标准Hex格式,可能在其他环境下出错。

修复后的脚本

// 封装颜色设置与导出函数,减少重复代码
function setTextColorAndExport(colorHex, colorName, baseName) {
  var textColor = new SolidColor();
  textColor.rgb.hexValue = colorHex;
  app.activeDocument.activeLayer.textItem.color = textColor;
  
  var exportPath = colorName + " " + baseName + ".png";
  var exportFile = new File(exportPath);
  app.activeDocument.exportDocument(exportFile, ExportType.SAVEFORWEB, exportOptions);
}

// 初始化导出配置
var textLayerName = app.activeDocument.activeLayer.name;
var newName = prompt("Enter a new name for the export", "black_" + textLayerName);

var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.quality = 100;

// 依次设置颜色并导出
setTextColorAndExport("000000", "black", newName);
setTextColorAndExport("FFFFFF", "white", newName);
setTextColorAndExport("fc82d8", "pink", newName);
setTextColorAndExport("63a8e7", "blue", newName);
setTextColorAndExport("0c6a27", "green", newName);
setTextColorAndExport("a80000", "red", newName);

修复说明

  • 替换所有中文全角引号为英文半角引号,确保颜色值和字符串能被正确解析。
  • 移除红色Hex值前的空格,符合标准Hex颜色格式。
  • 新增封装函数setTextColorAndExport,减少重复代码,让脚本结构更清晰,后续添加颜色也更方便。

内容的提问来源于stack exchange,提问作者G S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:01:27