Files
DP/PSMARK代码块/44222.jsx

226 lines
7.9 KiB
JavaScript

贴图换图()
function 贴图换图() {
建立快照();
var folder = Folder.selectDialog("请选择一个文件夹");
var 模特文档 = app.activeDocument;
if (folder) {
// 在选择的文件夹中创建一个新的子文件夹
var targetFolder = new Folder(folder.fullName + "/贴图生成");
if (!targetFolder.exists) {
targetFolder.create();
}
var fileNames = [];
var files = folder.getFiles();
// 获取文件夹中的所有文件名
for (var i = 0; i < files.length; i++) {
if (files[i] instanceof File) {
fileNames.push(files[i].name);
}
}
// 获取“贴图位置”图层组中的所有图层名称
var layerSet = 模特文档.layerSets.getByName("贴图位置");
var layerNames = [];
for (var j = 0; j < layerSet.artLayers.length; j++) {
layerNames.push(layerSet.artLayers[j].name);
}
// 遍历“贴图位置”图层组中的每个图层
for (var k = 0; k < layerNames.length; k++) {
var layname = layerNames[k];
var 贴图位置 = app.activeDocument.layerSets.getByName("贴图位置").layers.getByName(layname);
app.activeDocument.activeLayer = 贴图位置;
// 依次调取文件夹中的文件
if (k < fileNames.length) {
var file = new File(folder + "/" + fileNames[k]);
if (file.exists) {
try {
app.open(file);
app.activeDocument.flatten();
var 当前文档 = app.activeDocument;
var 当前文档名称 = 当前文档.name;
预设图案(当前文档名称); // 需要自定义实现
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = 模特文档;
载入选区(); // 需要自定义实现
填充图案(当前文档名称); // 需要自定义实现
} catch (e) {
alert("无法打开文件: " + fileNames[k]);
}
// 保存文件为 TIF 格式
}
}
}
var 当前文档 = app.activeDocument; // 获取当前文档
var 当前文档名称 = 当前文档.name; // 获取当前文档名称(包含扩展名)
// 去除文件扩展名并添加 ".tif" 后缀
var 文件名 = 当前文档名称.replace(/\.[^\.]+$/, "") + ".tif";
// 创建保存路径的 File 对象
var saveFile = new File(当前文档.path + "/" + 文件名);
// 设置 Tiff 保存选项
var tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.imageCompression = TIFFEncoding.NONE; // 无压缩
tiffSaveOptions.alphaChannels = true; // 保留 alpha 通道
tiffSaveOptions.layers = true; // 保留图层
// 另存为 TIF 格式
当前文档.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);
alert("换图完成");
}
}
// 其他函数保持不变
function generateRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function 图像大小() //图像大小
{
var d = new ActionDescriptor();
d.putUnitDouble(stringIDToTypeID("resolution"), stringIDToTypeID("densityUnit"), 150);
d.putBoolean(stringIDToTypeID("scaleStyles"), true);
d.putBoolean(stringIDToTypeID("constrainProportions"), true);
d.putEnumerated(charIDToTypeID("Intr"), stringIDToTypeID("interpolationType"), stringIDToTypeID("nearestNeighbor"));
executeAction(stringIDToTypeID("imageSize"), d, DialogModes.NO);
}
function 取消链接蒙版() //取消链接蒙版
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
d1.putBoolean(stringIDToTypeID("userMaskLinked"), false);
d.putObject(stringIDToTypeID("to"), stringIDToTypeID("layer"), d1);
executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
}
function 栅格化图层() //栅格化图层
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
executeAction(stringIDToTypeID("rasterizeLayer"), d, DialogModes.NO);
}
function 预设图案(当前文档名称) //
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("pattern"));
d.putReference(stringIDToTypeID("null"), r);
var r1 = new ActionReference();
r1.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
r1.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("using"), r1);
d.putString(stringIDToTypeID("name"), 当前文档名称);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
}
function 载入选区() //载入选区
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
d.putReference(stringIDToTypeID("null"), r);
var r1 = new ActionReference();
r1.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("transparencyEnum"));
d.putReference(stringIDToTypeID("to"), r1);
executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
}
function 填充图案(当前文档名称) //
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("contentLayer"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
var d2 = new ActionDescriptor();
var d3 = new ActionDescriptor();
d3.putString(stringIDToTypeID("name"), 当前文档名称);
d2.putObject(stringIDToTypeID("pattern"), stringIDToTypeID("pattern"), d3);
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("patternLayer"), d2);
d.putObject(stringIDToTypeID("using"), stringIDToTypeID("contentLayer"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
}
function 位移(水平,垂直) //位移
{
var d = new ActionDescriptor();
d.putInteger(stringIDToTypeID("horizontal"), 水平);
d.putInteger(stringIDToTypeID("vertical"), 垂直);
d.putEnumerated(stringIDToTypeID("fill"), stringIDToTypeID("fillMode"), stringIDToTypeID("wrap"));
executeAction(stringIDToTypeID("offset"), d, DialogModes.NO);
}
function 历史记录回退到快照1() //
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putName(stringIDToTypeID("snapshotClass"), "快照 1");
d.putReference(stringIDToTypeID("null"), r);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);
}
function 建立快照() //打开
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("snapshotClass"));
d.putReference(stringIDToTypeID("null"), r);
var r1 = new ActionReference();
r1.putProperty(stringIDToTypeID("historyState"), stringIDToTypeID("currentHistoryState"));
d.putReference(stringIDToTypeID("from"), r1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
}